Skip to content

Commit

Permalink
add Session#copy_attributes_from method
Browse files Browse the repository at this point in the history
  • Loading branch information
rachel-carvalho committed Apr 12, 2024
1 parent b5d3446 commit 946a9da
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Note: For changes to the API, see https://shopify.dev/changelog?filter=api

## Unreleased

- Add `Session#copy_attributes_from` method

## 14.1.0
- [#1071](https://github.com/Shopify/shopify-api-ruby/issues/1071) Fix FulfillmentEvent class types
Expand Down
11 changes: 11 additions & 0 deletions lib/shopify_api/auth/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ def deserialize(str)
end
end

sig { params(other: Session).returns(Session) }
def copy_attributes_from(other)
JSON.parse(other.serialize).keys.each do |key|
next if key.include?("^")

variable_name = "@#{key}"
instance_variable_set(variable_name, other.instance_variable_get(variable_name))
end
self
end

sig { returns(String) }
def serialize
Oj.dump(self)
Expand Down
54 changes: 54 additions & 0 deletions test/auth/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,63 @@ def test_from_with_online_access_token_response
assert_equal(expected_session, session)
end

def test_copy_attributes_from
session_to = ShopifyAPI::Auth::Session.new(
id: "session-id",
shop: "shop",
state: "some-state",
access_token: "to-token",
scope: "read_products,read_themes",
associated_user_scope: "read_products",
expires: Time.now - 3600,
associated_user: build_user,
is_online: true,
shopify_session_id: "123",
)

session_from = ShopifyAPI::Auth::Session.new(
id: "session-id",
shop: "shop",
state: nil,
access_token: "from-token",
scope: "write_products,read_themes",
associated_user_scope: "write_products",
expires: Time.now + 24 * 3600,
associated_user: build_user,
is_online: true,
shopify_session_id: "456",
)

assert_equal(session_to, session_to.copy_attributes_from(session_from))

assert_equal(session_from.shop, session_to.shop)
assert_nil(session_to.state)
assert_equal(session_from.access_token, session_to.access_token)
assert_equal(session_from.scope, session_to.scope)
assert_equal(session_from.associated_user_scope, session_to.associated_user_scope)
assert_equal(session_from.expires, session_to.expires)
assert_equal(session_from.associated_user, session_to.associated_user)
assert_equal(session_from.shopify_session_id, session_to.shopify_session_id)
end

def teardown
ShopifyAPI::Context.deactivate_session
end

private

def build_user
ShopifyAPI::Auth::AssociatedUser.new(
id: 1,
first_name: "Hello #{Time.now}",
last_name: "World",
email: "Email",
email_verified: true,
account_owner: true,
locale: "en",
collaborator: false,
)
end
end
end
end

0 comments on commit 946a9da

Please sign in to comment.