Skip to content

Commit 63532d5

Browse files
Merge pull request #1309 from Shopify/add_session_copy_attributes_from
Add `Session#copy_attributes_from` method
2 parents b5d3446 + 946a9da commit 63532d5

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Note: For changes to the API, see https://shopify.dev/changelog?filter=api
44

55
## Unreleased
6-
6+
- Add `Session#copy_attributes_from` method
77

88
## 14.1.0
99
- [#1071](https://github.com/Shopify/shopify-api-ruby/issues/1071) Fix FulfillmentEvent class types

lib/shopify_api/auth/session.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ def deserialize(str)
121121
end
122122
end
123123

124+
sig { params(other: Session).returns(Session) }
125+
def copy_attributes_from(other)
126+
JSON.parse(other.serialize).keys.each do |key|
127+
next if key.include?("^")
128+
129+
variable_name = "@#{key}"
130+
instance_variable_set(variable_name, other.instance_variable_get(variable_name))
131+
end
132+
self
133+
end
134+
124135
sig { returns(String) }
125136
def serialize
126137
Oj.dump(self)

test/auth/session_test.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,63 @@ def test_from_with_online_access_token_response
143143
assert_equal(expected_session, session)
144144
end
145145

146+
def test_copy_attributes_from
147+
session_to = ShopifyAPI::Auth::Session.new(
148+
id: "session-id",
149+
shop: "shop",
150+
state: "some-state",
151+
access_token: "to-token",
152+
scope: "read_products,read_themes",
153+
associated_user_scope: "read_products",
154+
expires: Time.now - 3600,
155+
associated_user: build_user,
156+
is_online: true,
157+
shopify_session_id: "123",
158+
)
159+
160+
session_from = ShopifyAPI::Auth::Session.new(
161+
id: "session-id",
162+
shop: "shop",
163+
state: nil,
164+
access_token: "from-token",
165+
scope: "write_products,read_themes",
166+
associated_user_scope: "write_products",
167+
expires: Time.now + 24 * 3600,
168+
associated_user: build_user,
169+
is_online: true,
170+
shopify_session_id: "456",
171+
)
172+
173+
assert_equal(session_to, session_to.copy_attributes_from(session_from))
174+
175+
assert_equal(session_from.shop, session_to.shop)
176+
assert_nil(session_to.state)
177+
assert_equal(session_from.access_token, session_to.access_token)
178+
assert_equal(session_from.scope, session_to.scope)
179+
assert_equal(session_from.associated_user_scope, session_to.associated_user_scope)
180+
assert_equal(session_from.expires, session_to.expires)
181+
assert_equal(session_from.associated_user, session_to.associated_user)
182+
assert_equal(session_from.shopify_session_id, session_to.shopify_session_id)
183+
end
184+
146185
def teardown
147186
ShopifyAPI::Context.deactivate_session
148187
end
188+
189+
private
190+
191+
def build_user
192+
ShopifyAPI::Auth::AssociatedUser.new(
193+
id: 1,
194+
first_name: "Hello #{Time.now}",
195+
last_name: "World",
196+
email: "Email",
197+
email_verified: true,
198+
account_owner: true,
199+
locale: "en",
200+
collaborator: false,
201+
)
202+
end
149203
end
150204
end
151205
end

0 commit comments

Comments
 (0)