lhs 15.2.0 → 15.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13a9d25e1f79ac50a05c978ee5f2ab7f7ed7abeb
4
- data.tar.gz: 2dde05c8df403fdf57199f8cbeade1a024eec3ae
3
+ metadata.gz: b22ab9cad00592e285064f9e5423a0068cf0924c
4
+ data.tar.gz: '079d61d3f7a12423f689e64de210bb67243ec239'
5
5
  SHA512:
6
- metadata.gz: decf3316e125e44703d005608fa43541b70160d861f7a11d8db2a7ee47af36e10bf5ecedb6eebf5b63c6c72401bc47acb97716b99a09c57af54d8e09b207d3d7
7
- data.tar.gz: b9492d724ee365fa68183d959c2f245a3cd995ab26f62e5d1209a2b0ba58d472fecdb02a192063e222d796e2760b7d7e8d961cf1d1e2c9df5db12dcbff7b5705
6
+ metadata.gz: 94b77bb8a7a211acdf05d62f21138442a8d0a68e75fe6bde7190f939595287f7495a51c754f8cc50013ef019ac6a996e8a838a5bd318c62bdbec70530e424458
7
+ data.tar.gz: 5dc21e72c816d3437677965059f0ac9ae009e3b41f355aec95927b95b8885ba1a42b086ba685b113279bcf25a4cc75f45e12a7ee39d39e1590b3cf4d494a6e01
data/README.md CHANGED
@@ -401,7 +401,29 @@ end
401
401
 
402
402
  See [Validation](#Validation) for handling validation errors when creating records.
403
403
 
404
- ## Create records through associations (nested resources)
404
+ ### Endpoint paramters and paramter injection during creation
405
+
406
+ LHS injects body parameters to generate target urls, used for creation requests:
407
+
408
+ ```ruby
409
+ class Favorite << LHS::Record
410
+ endpoint '{+datastore}/content-ads/{content_ad_id}/feedbacks'
411
+ end
412
+
413
+ Favorite.create(content_ad_id: 51232, text: 'Great Restaurant!')
414
+ # POST http://datastore/content_ads/51232
415
+ # body: '{ "text" : "Great Restaurant!" }'
416
+ ```
417
+
418
+ Because API's usually reject body paramters for foreign key attributes:
419
+
420
+ ```
421
+ Not allowed to set or change foreign key: content_ad_id!
422
+ ```
423
+
424
+ We remove it from the body, if the information was instead transported through the URL.
425
+
426
+ ### Create records through associations (nested resources)
405
427
 
406
428
  ```ruby
407
429
  class Review < LHS::Record
@@ -413,7 +435,7 @@ See [Validation](#Validation) for handling validation errors when creating recor
413
435
  end
414
436
  ```
415
437
 
416
- ### Item
438
+ #### Item
417
439
  ```ruby
418
440
  review = Review.find(1)
419
441
  # Review#1
@@ -435,7 +457,7 @@ See [Validation](#Validation) for handling validation errors when creating recor
435
457
 
436
458
  If the item already exists `ArgumentError` is raised.
437
459
 
438
- ### Expanded collection
460
+ #### Expanded collection
439
461
  ```ruby
440
462
  review = Review.includes(:comments).find(1)
441
463
  # Review#1
@@ -455,7 +477,7 @@ If the item already exists `ArgumentError` is raised.
455
477
  # :comments => { :href => '{+service}/reviews/1/comments, :items => [{ :href => '{+service}/reviews/1/comments/1', :text => 'Thank you!' }] }
456
478
  ```
457
479
 
458
- ### Not expanded collection
480
+ #### Not expanded collection
459
481
  ```ruby
460
482
  review = Review.find(1)
461
483
  # Review#1
@@ -484,6 +506,10 @@ Build and persist new items from scratch are done either with `new` or it's alia
484
506
  record.save
485
507
  ```
486
508
 
509
+ ### Endpoint parameters and paramter injection for saving records
510
+
511
+ See: [Endpoint paramters and paramter injection during creation](#endpoint-paramters-and-paramter-injection-during-creation)
512
+
487
513
  ## Custom setters and getters
488
514
 
489
515
  Sometimes it is the case that you want to have your custom getters and setters and convert the data to a processable format behind the scenes.
@@ -726,6 +752,32 @@ record = Record.find('1z-5r1fkaj')
726
752
  record.update(recommended: false)
727
753
  ```
728
754
 
755
+ ### Endpoint paramters and paramter injection during updates
756
+
757
+ LHS injects body parameters to generate target urls, used for update requests:
758
+
759
+ ```ruby
760
+ class Customer << LHS::Record
761
+ endpoint '{+customers}/{id}'
762
+ end
763
+
764
+ customer = Customer.find(123)
765
+ # GET http://customers/123
766
+ # { id: '123', name: 'My old company name' }
767
+
768
+ customer.update(name: 'My new company name')
769
+ # POST http://customers/123
770
+ # body: { "name": 'My new company name' }
771
+ ```
772
+
773
+ Because API's usually reject body paramters for primary identifiers:
774
+
775
+ ```
776
+ Not allowed to change primary id!
777
+ ```
778
+
779
+ We remove it from the body, if the information was instead transported through the URL.
780
+
729
781
  ## Partial Update
730
782
 
731
783
  Often you just want to update a single attribute on an existing record. As ActiveRecord's `update_attribute` skips validation, which is unlikely with api services, and `update_attributes` is just an alias for `update`, LHS introduces `partial_update` for that matter.
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
20
20
  s.requirements << 'Ruby >= 2.3.0'
21
21
  s.required_ruby_version = '>= 2.3.0'
22
22
 
23
- s.add_dependency 'lhc', '~> 9.1.1'
23
+ s.add_dependency 'lhc', '~> 9.1.2'
24
24
  s.add_dependency 'activesupport', '> 4.2'
25
25
  s.add_dependency 'activemodel'
26
26
 
@@ -114,6 +114,10 @@ class LHS::Record
114
114
  def class
115
115
  @data.keys.first
116
116
  end
117
+
118
+ def to_a
119
+ [self.class, handler]
120
+ end
117
121
  end
118
122
 
119
123
  # IgnoredError: Ignore certain LHC errors when resolving the chain
@@ -474,9 +474,9 @@ class LHS::Record
474
474
  return_data = nil
475
475
  error_class = LHC::Error.find(response)
476
476
  error = error_class.new(error_class, response)
477
- handlers = handlers.to_a.select { |error_handler| error.is_a? error_handler.class }
477
+ handlers = handlers.map(&:to_a).to_a.select { |handler_error_class, _| error.is_a? handler_error_class }
478
478
  raise(error) unless handlers.any?
479
- handlers.each do |handler|
479
+ handlers.each do |_, handler|
480
480
  handlers_return = handler.call(response)
481
481
  return_data = handlers_return if handlers_return.present?
482
482
  end
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = '15.2.0'
2
+ VERSION = '15.2.1'
3
3
  end
@@ -0,0 +1,30 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHS::Record do
4
+ let(:handler) { spy('handler') }
5
+
6
+ before(:each) do
7
+ class Record < LHS::Record
8
+ endpoint 'http://local.ch/v2/records/{id}'
9
+ end
10
+ class NestedRecord < LHS::Record
11
+ endpoint 'http://local.ch/v2/other_records/{id}'
12
+ end
13
+ stub_request(:get, "http://local.ch/v2/records/1")
14
+ .to_return(body: {
15
+ href: 'http://local.ch/v2/records/1',
16
+ other: {
17
+ href: 'http://local.ch/v2/other_records/2'
18
+ }
19
+ }.to_json)
20
+ stub_request(:get, "http://local.ch/v2/other_records/2")
21
+ .to_return(status: 404)
22
+ end
23
+
24
+ it 'allows to pass error_handling for includes to LHC' do
25
+ handler = ->(_) { return { deleted: true } }
26
+ record = Record.includes(:other).references(other: { error_handler: { LHC::NotFound => handler } }).find(id: 1)
27
+
28
+ expect(record.other.deleted).to be(true)
29
+ end
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 15.2.0
4
+ version: 15.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-21 00:00:00.000000000 Z
11
+ date: 2018-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lhc
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 9.1.1
19
+ version: 9.1.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 9.1.1
26
+ version: 9.1.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -375,6 +375,7 @@ files:
375
375
  - spec/record/find_in_parallel_spec.rb
376
376
  - spec/record/find_spec.rb
377
377
  - spec/record/first_spec.rb
378
+ - spec/record/handle_includes_errors_spec.rb
378
379
  - spec/record/has_many_spec.rb
379
380
  - spec/record/ignore_errors_spec.rb
380
381
  - spec/record/immutable_chains_spec.rb
@@ -436,7 +437,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
436
437
  requirements:
437
438
  - Ruby >= 2.3.0
438
439
  rubyforge_project:
439
- rubygems_version: 2.6.12
440
+ rubygems_version: 2.6.14
440
441
  signing_key:
441
442
  specification_version: 4
442
443
  summary: Rails gem providing an easy, active-record-like interface for http json services
@@ -562,6 +563,7 @@ test_files:
562
563
  - spec/record/find_in_parallel_spec.rb
563
564
  - spec/record/find_spec.rb
564
565
  - spec/record/first_spec.rb
566
+ - spec/record/handle_includes_errors_spec.rb
565
567
  - spec/record/has_many_spec.rb
566
568
  - spec/record/ignore_errors_spec.rb
567
569
  - spec/record/immutable_chains_spec.rb