reamaze_api 0.3.0 → 0.4.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: 8d2efff7cf528f03bb9740c5760e8b550fba79b6
4
- data.tar.gz: 49e9d0787266a1f1cb8c4cba2461cae3b87ea87e
3
+ metadata.gz: e555a6235c5bb2d36d98fb4b6ab19fe9947d0575
4
+ data.tar.gz: 4e067a44af81e4f4ff901c3c802c66506404cb15
5
5
  SHA512:
6
- metadata.gz: c0b921866ccc2c8410b24ac760d2631794628d77a25e450cbbb206bedf1e8d82050dbe8cdd36669e6452868cd6e0da5af35c6f4e7ed3c11ff95d0b6e312e43f3
7
- data.tar.gz: 9cc19b0ab9219fd2b2ff3aa41a6657bcb9b88b12bba63432d457d3370ecd5e76cd1c70ebbf24d185efee71671316ac388cbba98de09bde30350d1956f391973d
6
+ metadata.gz: ee7e1e594e3e532087ba366f69edaf8a057deef34b5ac8a3caadd6be4c6d8c2dd18584b28d5462ef4424d6b8eaaa7f102ee168e0ad139aeafb7aec77e19a3385
7
+ data.tar.gz: 67174ce5cad75fa3cb818eda68dcc66d9e3bab7997b40690f1476dad9d6bad86ca9f0f775071bf10146d89ea45aae63cb5e9802953f425c5f9c5915e9bd51671
data/README.md CHANGED
@@ -93,6 +93,35 @@ end
93
93
 
94
94
  [Faraday]: https://github.com/lostisland/faraday
95
95
 
96
+ ### API Errors
97
+
98
+ By default, ReamazeAPI returns an error Hash for any API response with status
99
+ 400-599. For example, the API returns a 404 if you provide an invalid brand
100
+ name:
101
+
102
+ ```ruby
103
+ client = ReamazeAPI.new brand: "invalid"
104
+ client.articles.all
105
+ # {
106
+ # :success => false,
107
+ # :error => "ReamazeAPI::NotFound",
108
+ # :message => "GET https://invalid.reamaze.com/api/v1/articles: 404"
109
+ # }
110
+ ```
111
+
112
+ If you would rather raise exceptions:
113
+
114
+ ```ruby
115
+ ReamazeAPI.config do |c|
116
+ c.exceptions = true
117
+ end
118
+
119
+ client = ReamazeAPI.new brand: "invalid"
120
+ client.articles.all # raises
121
+ # ReamazeAPI::NotFound: GET https://invalid.reamaze.com/api/v1/articles: 404
122
+ # ...backtrace
123
+ ```
124
+
96
125
  ## Development
97
126
 
98
127
  After checking out the repo, run `bin/setup` to install dependencies. Then,
@@ -3,20 +3,23 @@ require "uri"
3
3
 
4
4
  module ReamazeAPI
5
5
  class Client
6
- # Reamaze's API doesn't always return JSON. If there's an error parsing
7
- # the response as JSON, just return the raw body.
8
- class Middleware < FaradayMiddleware::ParseJson
9
- define_parser do |body|
10
- begin
11
- JSON.parse(body) unless body.strip.empty?
12
- rescue JSON::ParserError
13
- body
6
+ # Raises a ReamazeAPI::Error for any HTTP response code 400-599.
7
+ class RaiseErrorMiddleware < Faraday::Response::Middleware
8
+ private
9
+
10
+ # Private: Raises a ReamazeAPI::Error for any HTTP response code
11
+ # 400-599.
12
+ #
13
+ # response - HTTP response (Faraday::Env)
14
+ #
15
+ # Returns nothing.
16
+ def on_complete(response)
17
+ if error = ReamazeAPI::Error.from_response(response)
18
+ raise error
14
19
  end
15
20
  end
16
21
  end
17
22
 
18
- Faraday::Response.register_middleware reamaze_api: lambda { Middleware }
19
-
20
23
  # Public: HTTP methods used by the API
21
24
  #
22
25
  # Returns an Array.
@@ -47,6 +50,7 @@ module ReamazeAPI
47
50
  @http = Faraday.new(url: @url, ssl: { verify: true }) do |builder|
48
51
  builder.request :json
49
52
  builder.response :json
53
+ builder.use RaiseErrorMiddleware
50
54
  builder.adapter Faraday.default_adapter
51
55
  builder.basic_auth login, token
52
56
 
@@ -98,6 +102,9 @@ module ReamazeAPI
98
102
  # path: API path (without `/api/v1` prefix, eg: "/messages")
99
103
  # params: Hash of parameters to send with the request (default: {})
100
104
  #
105
+ # Raises a ReamazeAPI::Error for any HTTP response code 400-599 unless
106
+ # `ReamazeAPI.config.exceptions` is false.
107
+ #
101
108
  # Returns a Hash.
102
109
  def commit(method:, path:, params: {})
103
110
  path = "#{@url.path}#{path}"
@@ -105,7 +112,9 @@ module ReamazeAPI
105
112
  response = @http.run_request(method, path, params, {})
106
113
 
107
114
  Utils.symbolize_hash(success: response.success?, payload: response.body)
108
- rescue => e
115
+ rescue ReamazeAPI::Error => e
116
+ raise if ReamazeAPI.config.exceptions
117
+
109
118
  Utils.error_hash(e)
110
119
  end
111
120
 
@@ -0,0 +1,73 @@
1
+ module ReamazeAPI
2
+ # Encapsulates HTTP errors that may be returned by the Reamaze API. All API
3
+ # errors inherit from this class.
4
+ class Error < StandardError
5
+ # Public: Create an exception from the given response.
6
+ #
7
+ # response - HTTP response (Faraday::Env)
8
+ #
9
+ # Returns a ReamazeAPI::Error or nil.
10
+ def self.from_response(response)
11
+ if klass = case response[:status].to_i
12
+ when 403
13
+ ReamazeAPI::Forbidden
14
+ when 404
15
+ ReamazeAPI::NotFound
16
+ when 422
17
+ ReamazeAPI::UnprocessableEntity
18
+ when 429
19
+ ReamazeAPI::TooManyRequests
20
+ when 400..499
21
+ ReamazeAPI::ClientError
22
+ when 500..599
23
+ ReamazeAPI::ServerError
24
+ end
25
+
26
+ klass.new(response)
27
+ end
28
+ end
29
+
30
+ # Public: Initialize a new ReamazeAPI::Error instance.
31
+ #
32
+ # response - HTTP response (Faraday::Env)
33
+ #
34
+ # Returns nothing.
35
+ def initialize(response = nil)
36
+ @response = response
37
+ super(build_message)
38
+ end
39
+
40
+ private
41
+
42
+ # Private: Error message to be displayed.
43
+ #
44
+ # Returns a String or nil.
45
+ def build_message
46
+ return if @response.nil?
47
+
48
+ message = "#{@response[:method].to_s.upcase} "
49
+ message << "#{@response[:url]}: "
50
+ message << "#{@response[:status]}"
51
+ message << "\n\nBODY: #{@response[:body].inspect}" if @response[:body]
52
+ message
53
+ end
54
+ end
55
+
56
+ # Raised on HTTP 400-499
57
+ class ClientError < Error; end
58
+
59
+ # Raised on HTTP 403 (bad username/password)
60
+ class Forbidden < ClientError; end
61
+
62
+ # Raised on HTTP 404 (eg: bad brand or API URL)
63
+ class NotFound < ClientError; end
64
+
65
+ # Raised on HTTP 422 (eg: missing params in a POST)
66
+ class UnprocessableEntity < ClientError; end
67
+
68
+ # Raised on HTTP 429 (API rate limit exceeded)
69
+ class TooManyRequests < ClientError; end
70
+
71
+ # Raised on HTTP 500-599 (error on the Reamaze API server itself)
72
+ class ServerError < Error; end
73
+ end
@@ -1,3 +1,3 @@
1
1
  module ReamazeAPI
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/reamaze_api.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "reamaze_api/version"
2
2
  require "reamaze_api/utils"
3
+ require "reamaze_api/error"
3
4
  require "reamaze_api/client"
4
5
  require "reamaze_api/resource"
5
6
  require "reamaze_api/article"
@@ -10,7 +11,7 @@ require "reamaze_api/message"
10
11
 
11
12
  module ReamazeAPI
12
13
  # Public: Configuration class
13
- Config = Struct.new(:brand, :login, :token)
14
+ Config = Struct.new(:brand, :login, :token, :exceptions)
14
15
 
15
16
  # Public: Optional default configuration used to authenticate with the
16
17
  # Reamaze API.
@@ -41,7 +42,11 @@ module ReamazeAPI
41
42
  #
42
43
  # Returns a ReamazeAPI::Client instance.
43
44
  def self.new(**credentials, &block)
44
- params = config.to_h.select { |_, value| value }.merge(credentials)
45
+ params = {
46
+ brand: credentials.fetch(:brand) { config.brand },
47
+ login: credentials.fetch(:login) { config.login },
48
+ token: credentials.fetch(:token) { config.token },
49
+ }
45
50
 
46
51
  Client.new(**params, &block)
47
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reamaze_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Priddle
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-22 00:00:00.000000000 Z
11
+ date: 2016-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -114,6 +114,7 @@ files:
114
114
  - lib/reamaze_api/client.rb
115
115
  - lib/reamaze_api/contact.rb
116
116
  - lib/reamaze_api/conversation.rb
117
+ - lib/reamaze_api/error.rb
117
118
  - lib/reamaze_api/message.rb
118
119
  - lib/reamaze_api/resource.rb
119
120
  - lib/reamaze_api/utils.rb