Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Commit 1e83efd

Browse files
alegrriasfroehler
authored andcommitted
Adds option to set affiliate per request (#38)
Each request will accept an optional request ID. If provided this affiliate ID is used. Otherwise the affiliate ID is used from the initializer or the default affiliate ID (current behaviour).
1 parent d621d64 commit 1e83efd

19 files changed

Lines changed: 300 additions & 86 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## [Unreleased]
2+
- Add the possibility to specify custom affiliate_id on every request
23
### Added
34

45
### Changed

lib/shipcloud.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def self.configure
5252

5353
def self.api_headers
5454
API_HEADERS.merge(
55-
"Affiliate-ID" => configuration.affiliate_id || DEFAULT_AFFILIATE_ID
55+
"Affiliate-ID" => affiliate_id,
5656
)
5757
end
5858

@@ -81,6 +81,10 @@ def self.api_key=(api_key)
8181
configuration.api_key = api_key
8282
end
8383

84+
def self.affiliate_id
85+
configuration.affiliate_id || DEFAULT_AFFILIATE_ID
86+
end
87+
8488
# Makes a request against the shipcloud API
8589
#
8690
# @param [Symbol] http_method The http method to use, must be one of :get, :post, :put and :delete
@@ -89,9 +93,10 @@ def self.api_key=(api_key)
8993
# @param [String] optional api_key The api key. If no api key is given, Shipcloud.api_key will
9094
# be used for the request
9195
# @return [Array] The parsed JSON response.
92-
def self.request(http_method, api_url, data, api_key: nil)
96+
def self.request(http_method, api_url, data, api_key: nil, affiliate_id: nil)
9397
api_key ||= Shipcloud.api_key
94-
info = Request::Info.new(http_method, api_url, api_key, data)
98+
affiliate_id ||= Shipcloud.affiliate_id
99+
info = Request::Info.new(http_method, api_url, api_key, data, affiliate_id)
95100
Request::Base.new(info).perform
96101
end
97102
end

lib/shipcloud/operations/all.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ module ClassMethods
77
# creteria
88
# @param [String] optional api_key The api key. If no api key is given, Shipcloud.api_key
99
# will be used for the request
10-
def all(filter = {}, api_key: nil)
11-
response = Shipcloud.request(:get, base_url, filter, api_key: api_key)
10+
def all(filter = {}, api_key: nil, affiliate_id: nil)
11+
response = Shipcloud.request(
12+
:get,
13+
base_url,
14+
filter,
15+
api_key: api_key,
16+
affiliate_id: affiliate_id,
17+
)
1218
if index_response_root
1319
response = response.fetch(index_response_root, [])
1420
end

lib/shipcloud/operations/create.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ module ClassMethods
77
# @param [Hash] attributes The attributes of the created object
88
# @param \[String\] optional api_key The api key. If no api key is given, Shipcloud.api_key
99
# will be used for the request
10-
def create(attributes, api_key: nil)
11-
response = Shipcloud.request(:post, base_url, attributes, api_key: api_key)
10+
def create(attributes, api_key: nil, affiliate_id: nil)
11+
response = Shipcloud.request(
12+
:post,
13+
base_url,
14+
attributes,
15+
api_key: api_key,
16+
affiliate_id: affiliate_id,
17+
)
1218
if create_response_root
1319
response = response.fetch(create_response_root, {})
1420
end

lib/shipcloud/operations/delete.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ module ClassMethods
77
# @param [String] id The id of the object that gets deleted
88
# @param \[String\] optional api_key The api key. If no api key is given, Shipcloud.api_key
99
# will be used for the request
10-
def delete(id, api_key: nil)
11-
Shipcloud.request(:delete, "#{base_url}/#{id}", {}, api_key: api_key)
10+
def delete(id, api_key: nil, affiliate_id: nil)
11+
Shipcloud.request(
12+
:delete,
13+
"#{base_url}/#{id}",
14+
{},
15+
api_key: api_key,
16+
affiliate_id: affiliate_id,
17+
)
1218
true
1319
end
1420
end

lib/shipcloud/operations/find.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ module ClassMethods
88
# @param \[String\] optional api_key The api key. If no api key is given, Shipcloud.api_key
99
# will be used for the request
1010
# @return [Shipcloud::Base] The found object
11-
def find(id, api_key: nil)
12-
response = Shipcloud.request(:get, "#{base_url}/#{id}", {}, api_key: api_key)
11+
def find(id, api_key: nil, affiliate_id: nil)
12+
response = Shipcloud.request(
13+
:get,
14+
"#{base_url}/#{id}",
15+
{},
16+
api_key: api_key,
17+
affiliate_id: affiliate_id,
18+
)
1319
self.new(response)
1420
end
1521
end

lib/shipcloud/operations/update.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ module ClassMethods
88
# @param [Hash] attributes The attributes that should be updated
99
# @param \[String\] optional api_key The api key. If no api key is given, Shipcloud.api_key
1010
# will be used for the request
11-
def update(id, attributes, api_key: nil)
12-
response = Shipcloud.request(:put, "#{base_url}/#{id}", attributes, api_key: api_key)
11+
def update(id, attributes, api_key: nil, affiliate_id: nil)
12+
response = Shipcloud.request(
13+
:put, "#{base_url}/#{id}", attributes, api_key: api_key, affiliate_id: affiliate_id
14+
)
1315
self.new(response)
1416
end
1517
end
@@ -23,8 +25,10 @@ def self.included(base)
2325
# @param [Hash] attributes The attributes that should be updated
2426
# @param \[String\] optional api_key The api key. If no api key is given, Shipcloud.api_key
2527
# will be used for the request
26-
def update(attributes, api_key: nil)
27-
response = Shipcloud.request(:put, "#{base_url}/#{id}", attributes, api_key: api_key)
28+
def update(attributes, api_key: nil, affiliate_id: nil)
29+
response = Shipcloud.request(
30+
:put, "#{base_url}/#{id}", attributes, api_key: api_key, affiliate_id: affiliate_id
31+
)
2832
set_attributes(response)
2933
end
3034
end

lib/shipcloud/request/connection.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,27 @@ def setup_https
2020
end
2121

2222
def request
23-
https.start do |connection|
23+
https.start do
2424
https.request(https_request)
2525
end
2626
end
2727

2828
protected
2929

3030
def https_request
31+
headers = Shipcloud.api_headers.merge("Affiliate-ID" => @info.affiliate_id)
3132
https_request =
3233
case @info.http_method
3334
when :post
34-
Net::HTTP::Post.new(@info.url, Shipcloud.api_headers)
35+
Net::HTTP::Post.new(@info.url, headers)
3536
when :put
36-
Net::HTTP::Put.new(@info.url, Shipcloud.api_headers)
37+
Net::HTTP::Put.new(@info.url, headers)
3738
when :delete
38-
Net::HTTP::Delete.new(@info.url, Shipcloud.api_headers)
39+
Net::HTTP::Delete.new(@info.url, headers)
3940
else
4041
Net::HTTP::Get.new(
4142
@info.path_with_params(@info.url, @info.data),
42-
Shipcloud.api_headers,
43+
headers,
4344
)
4445
end
4546

lib/shipcloud/request/info.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module Shipcloud
22
module Request
33
class Info
4-
attr_accessor :http_method, :api_url, :api_key, :data
4+
attr_accessor :http_method, :api_url, :api_key, :data, :affiliate_id
55

6-
def initialize(http_method, api_url, api_key, data)
7-
@api_key = api_key
8-
@http_method = http_method
9-
@api_url = api_url
10-
@data = data
6+
def initialize(http_method, api_url, api_key, data, affiliate_id)
7+
@api_key = api_key
8+
@http_method = http_method
9+
@api_url = api_url
10+
@data = data
11+
@affiliate_id = affiliate_id
1112
end
1213

1314
def url

spec/shipcloud/address_spec.rb

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,44 +36,72 @@
3636
describe '.create' do
3737
it 'makes a new POST request using the correct API endpoint' do
3838
expect(Shipcloud).to receive(:request).
39-
with(:post, "addresses", valid_attributes, api_key: nil).and_return("data" => {})
39+
with(:post, "addresses", valid_attributes, api_key: nil, affiliate_id: nil).
40+
and_return("data" => {})
4041

4142
Shipcloud::Address.create(valid_attributes)
4243
end
4344

4445
it "returns an address containing an id" do
4546
expect(Shipcloud).to receive(:request).
46-
with(:post, "addresses", valid_attributes, api_key: nil).
47+
with(:post, "addresses", valid_attributes, api_key: nil, affiliate_id: nil).
4748
and_return(returned_address)
4849

4950
address = Shipcloud::Address.create(valid_attributes)
5051

5152
expect(address.id).to eq("1c81efb7-9b95-4dd8-92e3-cac1bca3df6f")
5253
end
54+
55+
it "use the affiliate ID provided for the request" do
56+
expect(Shipcloud).to receive(:request).
57+
with(:post, "addresses", valid_attributes, api_key: nil, affiliate_id: "affiliate_id").
58+
and_return(returned_address)
59+
60+
address = Shipcloud::Address.create(valid_attributes, affiliate_id: "affiliate_id")
61+
62+
expect(address.id).to eq("1c81efb7-9b95-4dd8-92e3-cac1bca3df6f")
63+
end
5364
end
5465

5566
describe '.find' do
5667
it 'makes a new GET request using the correct API endpoint to receive a specific address' do
5768
expect(Shipcloud).to receive(:request).with(
58-
:get, "addresses/123", {}, api_key: nil).and_return("id" => "123")
69+
:get, "addresses/123", {}, api_key: nil, affiliate_id: nil).and_return("id" => "123")
5970

6071
Shipcloud::Address.find("123")
6172
end
73+
74+
it "use the affiliate ID provided for the request" do
75+
expect(Shipcloud).to receive(:request).with(
76+
:get, "addresses/123", {}, api_key: nil, affiliate_id: "affiliate_id"
77+
).and_return("id" => "123")
78+
79+
Shipcloud::Address.find("123", affiliate_id: "affiliate_id")
80+
end
6281
end
6382

6483
describe '.update' do
6584
it 'makes a new PUT request using the correct API endpoint' do
6685
expect(Shipcloud).to receive(:request).with(
67-
:put, "addresses/123", { street: "Mittelweg" }, api_key: nil).and_return("data" => {})
86+
:put, "addresses/123", { street: "Mittelweg" }, api_key: nil, affiliate_id: nil
87+
).and_return("data" => {})
6888

6989
Shipcloud::Address.update("123", street: "Mittelweg")
7090
end
91+
92+
it "use the affiliate ID provided for the request" do
93+
expect(Shipcloud).to receive(:request).with(
94+
:put, "addresses/123", { street: "Mittelweg" }, api_key: nil, affiliate_id: "affiliate_id"
95+
).and_return("data" => {})
96+
97+
Shipcloud::Address.update("123", { street: "Mittelweg" }, affiliate_id: "affiliate_id")
98+
end
7199
end
72100

73101
describe '.all' do
74102
it 'makes a new Get request using the correct API endpoint' do
75103
expect(Shipcloud).to receive(:request).
76-
with(:get, "addresses", {}, api_key: nil).and_return([])
104+
with(:get, "addresses", {}, api_key: nil, affiliate_id: nil).and_return([])
77105

78106
Shipcloud::Address.all
79107
end
@@ -87,11 +115,21 @@
87115
expect(address).to be_a Shipcloud::Address
88116
end
89117
end
118+
119+
it "use the affiliate ID provided for the request" do
120+
stub_addresses_request(affiliate_id: "affiliate_id")
121+
122+
addresses = Shipcloud::Address.all(affiliate_id: "affiliate_id")
123+
124+
addresses.each do |address|
125+
expect(address).to be_a Shipcloud::Address
126+
end
127+
end
90128
end
91129

92-
def stub_addresses_request
130+
def stub_addresses_request(affiliate_id: nil)
93131
allow(Shipcloud).to receive(:request).
94-
with(:get, "addresses", {}, api_key: nil).
132+
with(:get, "addresses", {}, api_key: nil, affiliate_id: affiliate_id).
95133
and_return(
96134
[
97135
{

0 commit comments

Comments
 (0)