lurch 0.1.0 → 0.2.0

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: 41ee4ddf20b095a7e02065c4e5a7f6d4fc1d1dbc
4
- data.tar.gz: 915497d9093ad580cc406a0c236fcd4b27029378
3
+ metadata.gz: c8fb6274173704bb0f17e22796c50ecbb4fe75b0
4
+ data.tar.gz: 06ee7c2aefa1d85d083520d5b008ed30b689e4b4
5
5
  SHA512:
6
- metadata.gz: 42109bc7dda4b0b912b50176a90c8c2519b1e0978a5e7588144a261cff7bd42f078c2ce368d05246d7a77d266cced02f1db37399a867fa9ca0e694200b3b466c
7
- data.tar.gz: ee3fb80f1f7b6ca88b62ae469348356db96ebd33a3558a1b1badf038624648950ee7e27e089128f9507bdb2cb83c0ae3e0b97beefc0943e9f1f0def3c8e91b88
6
+ metadata.gz: 5586c71bb63f79f4887ce86e18ce1884f55a680ec18e26f9a10be9a18405fc6f5b72c6f63200b554a5cb168d71fce8a4b63a6fddafb55f73a4fe5a8ffc056d2c
7
+ data.tar.gz: fa90e2c7b33df39e6d0c4e87b63471c770d109b56faca9d3bfa64ebf764077b5e472d393fb728c98f26fd75cca870d3904c60ee808954bba2add4ad837e998ab
data/CHANGELOG.md CHANGED
@@ -6,7 +6,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6
6
 
7
7
  ## [Unreleased][]
8
8
 
9
+ ## [0.2.0][] - 2017-11-04
10
+ ### Fixed
11
+ - Better error handling; server response status code is now always included in the exception message.
12
+ - Calling `fetch` on an already loaded collection will just return itself now instead of raising a `NoMethodError`
13
+
9
14
  ## 0.1.0 - 2017-10-30
10
15
  ### Initial public release
11
16
 
12
- [Unreleased]: https://github.com/peek-travel/cocktail/compare/0.1.0...HEAD
17
+ [Unreleased]: https://github.com/peek-travel/cocktail/compare/0.2.0...HEAD
18
+ [0.2.0]: https://github.com/peek-travel/cocktail/compare/0.1.0...0.2.0
data/README.md CHANGED
@@ -10,7 +10,7 @@ A simple Ruby [JSON API](http://jsonapi.org/) client.
10
10
  Add this line to your application's Gemfile:
11
11
 
12
12
  ```
13
- gem 'lurch'
13
+ gem "lurch", "~> 0.2"
14
14
  ```
15
15
 
16
16
  And then execute:
data/TODO.md CHANGED
@@ -18,5 +18,5 @@
18
18
  * [ ] Yardoc documentation
19
19
  * [x] Add license
20
20
  * [x] Add changelog
21
- * [ ] Release 0.1.0 open source
21
+ * [x] Release 0.1.0 open source
22
22
  * [x] Switch to codecov.io
data/lib/lurch/client.rb CHANGED
@@ -8,8 +8,7 @@ module Lurch
8
8
  403 => Errors::Forbidden,
9
9
  404 => Errors::NotFound,
10
10
  409 => Errors::Conflict,
11
- 422 => Errors::UnprocessableEntity,
12
- 500 => Errors::ServerError
11
+ 422 => Errors::UnprocessableEntity
13
12
  }.freeze
14
13
 
15
14
  def initialize(url, config)
@@ -68,12 +67,18 @@ module Lurch
68
67
  end
69
68
 
70
69
  def catch_errors(response)
71
- raise STATUS_EXCEPTIONS[response.status], response.body if STATUS_EXCEPTIONS[response.status]
72
- raise Errors::ServerError, response.body unless (200..299).cover?(response.status)
70
+ raise_error(STATUS_EXCEPTIONS[response.status], response) if STATUS_EXCEPTIONS[response.status]
71
+ raise_error(Errors::ClientError, response) if (400..499).cover?(response.status)
72
+ raise_error(Errors::ServerError, response) unless (200..299).cover?(response.status)
73
+ # TODO: handle 3xx
73
74
 
74
75
  response
75
76
  end
76
77
 
78
+ def raise_error(klass, response)
79
+ raise klass.new(response.body, response.status)
80
+ end
81
+
77
82
  def client
78
83
  @client ||= Faraday.new(url: url) do |conn|
79
84
  conn.headers[AUTHORIZATION] = authorization unless authorization.nil?
@@ -0,0 +1,6 @@
1
+ module Lurch
2
+ module Errors
3
+ class ClientError < JSONApiError
4
+ end
5
+ end
6
+ end
@@ -1,28 +1,33 @@
1
1
  module Lurch
2
2
  module Errors
3
3
  class JSONApiError < StandardError
4
- def initialize(document)
4
+ attr_reader :status
5
+
6
+ def initialize(document, status)
5
7
  @document = document
8
+ @status = status
6
9
  end
7
10
 
8
11
  def message
9
- return @document unless errors_document?
10
- @document["errors"].map { |error| message_from_document_error(error) }.join(", ")
12
+ return "#{@status}: #{@document}" unless errors_document?
13
+
14
+ "#{@status}: #{errors_string}"
11
15
  end
12
16
 
13
17
  def errors
14
18
  return [] unless errors_document?
19
+
15
20
  @document["errors"].map { |error| Lurch::Error.new(error) }
16
21
  end
17
22
 
18
23
  private
19
24
 
20
25
  def errors_document?
21
- @document.is_a?(Hash) && @document["errors"]
26
+ @document.is_a?(Hash) && @document["errors"].is_a?(Array)
22
27
  end
23
28
 
24
- def message_from_document_error(error)
25
- [*error["status"], *error["detail"]].join(": ")
29
+ def errors_string
30
+ @document["errors"].map { |error| error["detail"] }.join(", ")
26
31
  end
27
32
  end
28
33
  end
data/lib/lurch/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lurch
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
data/lib/lurch.rb CHANGED
@@ -20,6 +20,7 @@ require "lurch/errors/forbidden"
20
20
  require "lurch/errors/not_found"
21
21
  require "lurch/errors/conflict"
22
22
  require "lurch/errors/unprocessable_entity"
23
+ require "lurch/errors/client_error"
23
24
  require "lurch/errors/server_error"
24
25
  require "lurch/errors/not_loaded"
25
26
  require "lurch/errors/relationship_not_loaded"
@@ -20,6 +20,10 @@ class ResponseFactory
20
20
  header(:not_found) + errors_document([404, "Not Found"])
21
21
  end
22
22
 
23
+ def client_error_response
24
+ header(:teapot) + "I'm a teapot!"
25
+ end
26
+
23
27
  def unprocessable_entity_response(message)
24
28
  header(:unprocessable_entity) + errors_document([422, message])
25
29
  end
@@ -28,6 +32,10 @@ class ResponseFactory
28
32
  header(:server_error) + errors_document([500, "Internal Server Error"])
29
33
  end
30
34
 
35
+ def bad_server_error_response
36
+ header(:server_error) + "Oops! Something went wrong!"
37
+ end
38
+
31
39
  def person_response(id, name, phone_numbers_args = nil, code: :ok, include_phone_numbers: true)
32
40
  included = phone_numbers_args && include_phone_numbers ? phone_numbers_args.map { |args| phone_number_data(*args) } : []
33
41
  phone_number_ids = phone_numbers_args ? phone_numbers_args.map(&:first) : nil
@@ -171,6 +179,7 @@ private
171
179
  unauthorized: "401 Unauthorized".freeze,
172
180
  forbidden: "403 Forbidden".freeze,
173
181
  not_found: "404 Not Found".freeze,
182
+ teapot: "418 I'm a teapot".freeze,
174
183
  unprocessable_entity: "422 Unprocessable Entity".freeze,
175
184
  server_error: "500 Internal Server Error".freeze
176
185
  }.freeze
@@ -21,9 +21,21 @@ class TestErrors < Minitest::Test
21
21
  assert_equal "404: Not Found", err.message
22
22
  end
23
23
 
24
+ def test_418
25
+ stub_get("#{person_type}/1", @response_factory.client_error_response)
26
+ err = assert_raises(Lurch::Errors::ClientError) { @store.from(:people).find("1") }
27
+ assert_equal "418: I'm a teapot!", err.message
28
+ end
29
+
24
30
  def test_500
25
31
  stub_get("#{person_type}/1", @response_factory.server_error_response)
26
32
  err = assert_raises(Lurch::Errors::ServerError) { @store.from(:people).find("1") }
27
33
  assert_equal "500: Internal Server Error", err.message
28
34
  end
35
+
36
+ def test_unformatted_500
37
+ stub_get("#{person_type}/1", @response_factory.bad_server_error_response)
38
+ err = assert_raises(Lurch::Errors::ServerError) { @store.from(:people).find("1") }
39
+ assert_equal "500: Oops! Something went wrong!", err.message
40
+ end
29
41
  end
@@ -41,6 +41,9 @@ class TestFetchRelationships < Minitest::Test
41
41
  person.phone_numbers.fetch
42
42
  phone_numbers = person.phone_numbers
43
43
 
44
+ # calling fetch again should not fail
45
+ person.phone_numbers.fetch
46
+
44
47
  assert_kind_of Lurch::Collection, phone_numbers
45
48
  assert_equal true, person.phone_numbers.loaded?
46
49
  assert_kind_of Lurch::Relationship::Linked, person.relationships[:phone_numbers]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lurch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Dosé
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-31 00:00:00.000000000 Z
11
+ date: 2017-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -161,6 +161,7 @@ files:
161
161
  - lib/lurch/configuration.rb
162
162
  - lib/lurch/error.rb
163
163
  - lib/lurch/errors/bad_request.rb
164
+ - lib/lurch/errors/client_error.rb
164
165
  - lib/lurch/errors/conflict.rb
165
166
  - lib/lurch/errors/forbidden.rb
166
167
  - lib/lurch/errors/json_api_error.rb