gecko-ruby 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28ddc0bdac8932398a7129cd657ae2b80b5614ad
4
- data.tar.gz: f2d47820b607c3d89fdffe2ad0958f031abbd2cb
3
+ metadata.gz: c85728687e6bc4e59c11d5214181af686b350033
4
+ data.tar.gz: 18cc56bd3df2520071e796eb191eb8c341bc4666
5
5
  SHA512:
6
- metadata.gz: 8b88ce008228e17b33da355a96bad95572dc1b8df0231c2b9a36340cdf97ac34c3e4f137e4659b83a0d3da62162f0ee3f1eb21fd1594ae9cd56b6014f1d19a9b
7
- data.tar.gz: 08f58c8995d09445c69688d072790687a82b35effbf16c0679e402aea548958abbde4ad84b1fae25f654365183767e20b82f2a7239c1b792e3103584a4ab881c
6
+ metadata.gz: 14c2cd95c537a35fb6a574c8991703333bd856a882f93bbca0f08922f7d2804ce0c512c7e91c40522be97fbe5d15e0e7fc40042705393c3c1a3e0dc818301aa5
7
+ data.tar.gz: 94114cbea7468c93308b60a8a68904874cd30f12588a2c801a27346f6767ccf3644911411ebecb52c94959d6be94d726c16c76894779c5e51ee2b348ff23d0f7
@@ -6,7 +6,7 @@ AllCops:
6
6
  # private
7
7
  # ...
8
8
  # end
9
- Style/AccessModifierIndentation:
9
+ Layout/AccessModifierIndentation:
10
10
  EnforcedStyle: outdent
11
11
 
12
12
  Style/Blocks:
@@ -16,26 +16,26 @@ Style/Blocks:
16
16
  Style/BracesAroundHashParameters:
17
17
  Enabled: false
18
18
 
19
- Style/DotPosition:
19
+ Layout/DotPosition:
20
20
  EnforcedStyle: leading
21
21
 
22
- Style/IndentHash:
22
+ Layout/IndentHash:
23
23
  EnforcedStyle: consistent
24
24
 
25
- Style/LineLength:
25
+ Metrics/LineLength:
26
26
  Max: 120
27
27
 
28
28
  Style/RedundantSelf:
29
29
  Enabled: false
30
30
 
31
31
  # We like neatly-aligned code
32
- Style/SingleSpaceBeforeFirstArg:
32
+ Layout/SpaceBeforeFirstArg:
33
33
  Enabled: false
34
34
 
35
- Style/SpaceAroundEqualsInParameterDefault:
35
+ Layout/SpaceAroundEqualsInParameterDefault:
36
36
  Enabled: false
37
37
 
38
- Style/SpaceInsideHashLiteralBraces:
38
+ Layout/SpaceInsideHashLiteralBraces:
39
39
  Enabled: false
40
40
 
41
41
  # Allow single or double quotes
@@ -44,7 +44,7 @@ Style/StringLiterals:
44
44
 
45
45
  # Disable this until we can work out how to allow aligning of assignments
46
46
  # https://github.com/bbatsov/rubocop/blob/master/spec/rubocop/cop/style/extra_spacing_spec.rb#L23
47
- Style/ExtraSpacing:
47
+ Layout/ExtraSpacing:
48
48
  Enabled: false
49
49
 
50
50
  Style/WordArray:
@@ -59,7 +59,7 @@ Style/SignalException:
59
59
  Metrics/ClassLength:
60
60
  Enabled: false
61
61
 
62
- Style/MultilineOperationIndentation:
62
+ Layout/MultilineOperationIndentation:
63
63
  Enabled: false
64
64
 
65
65
  Style/ClassAndModuleChildren:
@@ -1,5 +1,10 @@
1
+ ## 0.2.3 (2017-08-14)
2
+ - Add support for API idempotency @client.Record.save(idempotency_key: 'ABCDEF123456')
3
+ - Marked a couple of fields as readonly that weren't correctly marked so
4
+
1
5
  ## 0.2.2 (2016-06-06)
2
6
  - Add `@client.Record.peek_all` to return all items currently in identity map
7
+
3
8
  ## 0.2.1 (2016-06-06) (Yanked)
4
9
  ## 0.2.0 (2016-06-06)
5
10
  - Support `writeable_on :create` for attributes
data/README.md CHANGED
@@ -11,7 +11,7 @@ If you are unfamiliar with the TradeGecko API, you can read the documentation lo
11
11
 
12
12
  Add this line to your application's Gemfile:
13
13
 
14
- gem 'gecko-ruby', '~> 0.0.9'
14
+ gem 'gecko-ruby'
15
15
 
16
16
  And then execute:
17
17
 
@@ -43,11 +43,14 @@ module Gecko
43
43
 
44
44
  # Save a record
45
45
  #
46
+ # @param [Hash] opts the options to save the record with
47
+ # @option opts [Hash] :idempotency_key A unique identifier for this action
48
+ #
46
49
  # @return <Gecko::Record::Base>
47
50
  #
48
51
  # @api public
49
- def save
50
- @client.adapter_for(self.class.demodulized_name).save(self)
52
+ def save(opts = {})
53
+ @client.adapter_for(self.class.demodulized_name).save(self, opts)
51
54
  end
52
55
 
53
56
  # Return the demodulized class name
@@ -185,7 +185,7 @@ module Gecko
185
185
  #
186
186
  # @api public
187
187
  def size
188
- (@pagination && @pagination['total_records']) || count
188
+ (defined?(@pagination) && @pagination['total_records']) || count
189
189
  end
190
190
 
191
191
  # Fetch a record via API, regardless of whether it is already in identity map.
@@ -264,16 +264,18 @@ module Gecko
264
264
  # Save a record
265
265
  #
266
266
  # @params [Object] :record A Gecko::Record object
267
+ # @param [Hash] opts the options to make the request with
268
+ # @option opts [Hash] :idempotency_key A unique identifier for this action
267
269
  #
268
270
  # @return [Boolean] whether the save was successful.
269
271
  # If false the record will contain an errors hash
270
272
  #
271
273
  # @api private
272
- def save(record)
274
+ def save(record, opts = {})
273
275
  if record.persisted?
274
- update_record(record)
276
+ update_record(record, opts)
275
277
  else
276
- create_record(record)
278
+ create_record(record, opts)
277
279
  end
278
280
  end
279
281
 
@@ -338,11 +340,11 @@ module Gecko
338
340
  # @return [OAuth2::Response]
339
341
  #
340
342
  # @api private
341
- def create_record(record)
343
+ def create_record(record, opts = {})
342
344
  response = request(:post, plural_path, {
343
345
  body: record.as_json,
344
346
  raise_errors: false
345
- })
347
+ }.merge(headers: headers_from_opts(opts)))
346
348
  handle_response(record, response)
347
349
  end
348
350
 
@@ -351,11 +353,11 @@ module Gecko
351
353
  # @return [OAuth2::Response]
352
354
  #
353
355
  # @api private
354
- def update_record(record)
356
+ def update_record(record, opts = {})
355
357
  response = request(:put, plural_path + "/" + record.id.to_s, {
356
358
  body: record.as_json,
357
359
  raise_errors: false
358
- })
360
+ }.merge(headers: headers_from_opts(opts)))
359
361
  handle_response(record, response)
360
362
  end
361
363
 
@@ -389,6 +391,15 @@ module Gecko
389
391
  @pagination = JSON.parse(headers["x-pagination"]) if headers["x-pagination"]
390
392
  end
391
393
 
394
+ # Applies an idempotency key to the request if provided
395
+ #
396
+ # @api private
397
+ def headers_from_opts(opts)
398
+ headers = {}
399
+ headers['Idempotency-Key'] = opts[:idempotency_key] if opts[:idempotency_key]
400
+ headers
401
+ end
402
+
392
403
  # Parse and instantiate sideloaded records
393
404
  #
394
405
  # @api private
@@ -29,8 +29,8 @@ module Gecko
29
29
 
30
30
  attribute :tax_number, String
31
31
 
32
- attribute :default_tax_rate, BigDecimal
33
32
  attribute :default_discount_rate, BigDecimal
33
+ attribute :default_tax_rate, BigDecimal, readonly: true
34
34
  end
35
35
 
36
36
  class CompanyAdapter < BaseAdapter
@@ -11,7 +11,6 @@ module Gecko
11
11
  belongs_to :contact
12
12
  belongs_to :shipping_address, class_name: 'Address'
13
13
  belongs_to :billing_address, class_name: 'Address'
14
- belongs_to :contact, class_name: 'Contact'
15
14
  belongs_to :user, readonly: true
16
15
  belongs_to :assignee, class_name: 'User'
17
16
  belongs_to :stock_location, class_name: 'Location'
@@ -1,3 +1,3 @@
1
1
  module Gecko
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -13,7 +13,7 @@ class ClientTest < Minitest::Test
13
13
  def test_custom_user_agent
14
14
  client = Gecko::Client.new("ABC", "DEF")
15
15
  agent = client.oauth_client.connection.headers["User-Agent"]
16
- assert_match(%r|Gecko/#{Gecko::VERSION} OAuth2/\d.\d.\d Faraday/\d.\d.\d Ruby/\d.\d.\d|, agent)
16
+ assert_match(%r|Gecko/#{Gecko::VERSION} OAuth2/\d\.\d\.\d Faraday/\d\.\d+\.\d Ruby/\d\.\d\.\d|, agent)
17
17
  end
18
18
 
19
19
  def test_allows_test_URLs
@@ -76,7 +76,7 @@ class Gecko::Helpers::SerializationHelperTest < Minitest::Test
76
76
  end
77
77
 
78
78
  def test_root_key
79
- record = Gecko::Record::OrderLineItem.new(@client, @json)
79
+ record = Gecko::Record::OrderLineItem.new(@client, {})
80
80
  assert_equal(:order_line_item, record.root)
81
81
  end
82
82
 
@@ -10,6 +10,7 @@ class Gecko::Record::AccountAdapterTest < Minitest::Test
10
10
  undef :test_build_with_attributes
11
11
  undef :test_saving_new_record
12
12
  undef :test_saving_new_invalid_record
13
+ undef :test_saving_record_with_idempotency_key
13
14
 
14
15
  let(:adapter) { @client.Account }
15
16
  let(:plural_name) { 'accounts' }
@@ -17,6 +17,7 @@ class Gecko::Record::UserAdapterTest < Minitest::Test
17
17
  undef :test_build_with_attributes
18
18
  undef :test_saving_new_record
19
19
  undef :test_saving_new_invalid_record
20
+ undef :test_saving_record_with_idempotency_key
20
21
 
21
22
  def test_current
22
23
  VCR.use_cassette('users#current') do
@@ -66,7 +66,7 @@ module SharedAdapterExamples
66
66
  end
67
67
 
68
68
  def test_fetch_miss
69
- request_stub = stub_request(:get, /#{plural_name}\/\d+/)
69
+ stub_request(:get, /#{plural_name}\/\d+/)
70
70
  .to_return({
71
71
  status: 404,
72
72
  headers: {"Content-Type" => "application/json"},
@@ -145,6 +145,23 @@ module SharedAdapterExamples
145
145
  assert(record.errors[:title].any?)
146
146
  end
147
147
 
148
+ def test_saving_record_with_idempotency_key
149
+ record = adapter.build
150
+ mock_token = mock
151
+ mock_response = mock(status: 200, parsed: {plural_name.singularize => {id: 123}})
152
+ mock_token.expects(:request)
153
+ .with(:post, plural_name, {
154
+ body: record.as_json.to_json,
155
+ raise_errors: false,
156
+ headers: {
157
+ 'Content-Type' => 'application/json',
158
+ 'Idempotency-Key' => 'abcdefghijkl'
159
+ }
160
+ }).returns(mock_response)
161
+ adapter.client.access_token = mock_token
162
+ adapter.save(record, idempotency_key: 'abcdefghijkl')
163
+ end
164
+
148
165
  private
149
166
  def mock_api_request(record, request, response)
150
167
  mock_token = mock
@@ -11,7 +11,7 @@ module SharedRecordExamples
11
11
  end
12
12
 
13
13
  def test_saving
14
- @client.adapter_for(record_class.demodulized_name).expects(:save).with(@record)
14
+ @client.adapter_for(record_class.demodulized_name).expects(:save).with(@record, {})
15
15
  @record.save
16
16
  end
17
17
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gecko-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bradley Priest
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-06 00:00:00.000000000 Z
11
+ date: 2017-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -357,7 +357,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
357
357
  version: '0'
358
358
  requirements: []
359
359
  rubyforge_project:
360
- rubygems_version: 2.6.4
360
+ rubygems_version: 2.5.2
361
361
  signing_key:
362
362
  specification_version: 4
363
363
  summary: A Ruby interface to the TradeGecko API.