faraday_middleware-reddit 0.4.0 → 0.4.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: 6c8176b8963036b5df1a473466816f24c7097f01
4
- data.tar.gz: 4aec072756fcbccb834d32ed8dd3aeb1c66f8858
3
+ metadata.gz: 3a8bf096bc82afbfb647b67b46d95a2197a760ae
4
+ data.tar.gz: f1cea4e6ffe5be56610905ecf3471e7279383c61
5
5
  SHA512:
6
- metadata.gz: ae93fb75328474ccf279951bb7582de3273794550708428e180c91d2e34030c18ae5ba956fc9c8b24f7262817cc9ace6b7da8f0e50659b17965f872708068185
7
- data.tar.gz: eb6393b04f2a06a7abd3c4019a7cd3959685d8983db670140779d5bbc87393892bdd372555e44da6adc3880214b9f181fadd0d28dafe6016fdbdbc6f0abe651e
6
+ metadata.gz: 3b5462123374a82de0a701da5188855bd1b2766478c027b8070d0c56fe460effd53013af394341bce6cb9d6bad5ecef64c220f5a6b22fac281a5be9e5ae7db2e
7
+ data.tar.gz: ab18755c20a5c60d31b9b600d2dd26aacafc8e7f4b90d314767fd22fe76fc5a381531a609a1dbec30785d5300f6e6d56fcd93a29115c8b4b1acc58d43d4172d5
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A collection of Faraday middleware for use with the Reddit API.
4
4
 
5
+ This project is the backbone for [reddit-base](https://github.com/dobs/reddit-base). If you're building a client from scratch or just want to get up and running quickly you might want to check it out instead.
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
@@ -29,29 +31,34 @@ Like `faraday_middleware`, `faraday_middleware-reddit` requires a `json` library
29
31
  | Middleware | Type | Description |
30
32
  | --- | --- | --- |
31
33
  | `:reddit_authentication` | request | Authentication based on a username, password or pre-generated cookie. |
32
- | `:reddit_force_json` | request | Coerces reddit into returnign JSON for GET and POST requests. |
34
+ | `:reddit_force_json` | use | Coerces reddit into returnign JSON for GET and POST requests. |
33
35
  | `:reddit_modhash` | use | Automatic modhash handling. |
36
+ | `:reddit_raise_error` | response | Raises errors for common reddit error cases. |
34
37
  | `:reddit_rate_limit` | use | Rate limiting based on reddit's `x-ratelimit` headers. Accepts a `strategy` proc to override default linear strategy. |
35
38
 
36
39
  ## Examples
37
40
 
38
41
  An example Farday client might look like:
39
42
 
40
- require 'faraday_middleware/reddit'
43
+ ```ruby
44
+ require 'faraday_middleware/reddit'
41
45
 
42
- conn = Faraday.new(url: 'http://www.reddit.com', headers: {'User-Agent' => 'faraday_middleware-reddit example (v 0.0.1)'}) do |faraday|
43
- faraday.request :url_encoded
44
- faraday.request :reddit_authentication, user: 'yourusername', password: 'yourpassword'
46
+ conn = Faraday.new(url: 'http://www.reddit.com', headers: {'User-Agent' => 'faraday_middleware-reddit example (v 0.0.1)'}) do |faraday|
47
+ faraday.request :url_encoded
48
+ faraday.request :reddit_authentication, user: 'yourusername', password: 'yourpassword'
49
+ faraday.request :retry, max: 2, interval: 2, exceptions: FaradayMiddleware::Reddit::RETRIABLE_ERRORS
45
50
 
46
- faraday.response :logger
47
- faraday.response :follow_redirects
51
+ faraday.response :logger
52
+ faraday.response :follow_redirects
53
+ faraday.response :reddit_raise_error
48
54
 
49
- faraday.use :reddit_force_json
50
- faraday.use :reddit_rate_limit
51
- faraday.use :reddit_modhash
55
+ faraday.use :reddit_force_json
56
+ faraday.use :reddit_rate_limit
57
+ faraday.use :reddit_modhash
52
58
 
53
- faraday.adapter Faraday.default_adapter
54
- end
59
+ faraday.adapter Faraday.default_adapter
60
+ end
61
+ ```
55
62
 
56
63
  ## Contributing
57
64
 
@@ -2,11 +2,48 @@ require 'faraday/error'
2
2
 
3
3
  module FaradayMiddleware
4
4
  module Reddit
5
- class TooManyRequestsError < Faraday::ClientError; end # HTTP Status 429
6
- class BadGatewayError < Faraday::ClientError; end # HTTP Status 502
7
- class ServiceUnavailableError < Faraday::ClientError; end # HTTP Status 503
8
- class GatewayTimeoutError < Faraday::ClientError; end # HTTP Status 504
5
+ class ClientError < Faraday::ClientError; end
6
+ class ServerError < Faraday::ClientError; end
9
7
 
10
- RETRIABLE_ERRORS = [ServiceUnavailableError, GatewayTimeoutError]
8
+ # Many of these errors are hypothetical, but better safe than sorry.
9
+ class BadRequestError < ClientError; end
10
+ class UnauthorizedError < ClientError; end
11
+ class ForbiddenError < ClientError; end
12
+ class NotFoundError < ClientError; end
13
+ class MethodNotAllowedError < ClientError; end
14
+ class NotAcceptableError < ClientError; end
15
+ class RequestTimeoutError < ClientError; end
16
+ class RequestEntityTooLargeError < ClientError; end
17
+ class RequestURITooLongError < ClientError; end
18
+ class UnsupportedMediaTypeError < ClientError; end
19
+ class TooManyRequestsError < ClientError; end
20
+ class InternalServerError < ServerError; end
21
+ class NotImplementedError < ServerError; end
22
+ class BadGatewayError < ServerError; end
23
+ class ServiceUnavailableError < ServerError; end
24
+ class GatewayTimeoutError < ServerError; end
25
+
26
+ ERROR_CODES = {
27
+ 400 => BadRequestError,
28
+ 401 => UnauthorizedError,
29
+ 403 => ForbiddenError,
30
+ 404 => NotFoundError,
31
+ 405 => MethodNotAllowedError,
32
+ 406 => NotAcceptableError,
33
+ 408 => RequestTimeoutError,
34
+ 413 => RequestEntityTooLargeError,
35
+ 414 => RequestURITooLongError,
36
+ 429 => TooManyRequestsError,
37
+ 500 => InternalServerError,
38
+ 501 => NotImplementedError,
39
+ 502 => BadGatewayError,
40
+ 503 => ServiceUnavailableError,
41
+ 504 => GatewayTimeoutError
42
+ }.freeze
43
+
44
+ RETRIABLE_ERRORS = [
45
+ ServiceUnavailableError,
46
+ GatewayTimeoutError
47
+ ].freeze
11
48
  end
12
49
  end
@@ -14,8 +14,8 @@ module FaradayMiddleware
14
14
 
15
15
  body = env[:body]
16
16
  body = JSON.parse(body) if body.is_a?(String) && !body.strip.empty?
17
- body = body['json'] if body['json']
18
- body['data']['modhash'] if body['data']
17
+ body = body['json'] if body.is_a?(Hash) && body['json']
18
+ body['data']['modhash'] if body.is_a?(Hash) && body['data']
19
19
  end
20
20
  end
21
21
  end
@@ -8,18 +8,11 @@ module FaradayMiddleware
8
8
  # 429 for hitting the API rate limit or 504 for gateway timeouts.
9
9
  class RaiseError < Faraday::Response::RaiseError
10
10
  def on_complete(env)
11
- case env[:status]
12
- when 429
13
- raise FaradayMiddleware::Reddit::TooManyRequestsError, response_values(env)
14
- when 502
15
- raise FaradayMiddleware::Reddit::BadGatewayError, response_values(env)
16
- when 503
17
- raise FaradayMiddleware::Reddit::ServiceUnavailableError, response_values(env)
18
- when 504
19
- raise FaradayMiddleware::Reddit::GatewayTimeoutError, response_values(env)
11
+ if FaradayMiddleware::Reddit::ERROR_CODES.include? env[:status]
12
+ raise FaradayMiddleware::Reddit::ERROR_CODES[env[:status]], response_values(env)
13
+ else
14
+ super
20
15
  end
21
-
22
- super
23
16
  end
24
17
  end
25
18
  end
@@ -30,7 +30,8 @@ module FaradayMiddleware
30
30
  end
31
31
 
32
32
  def update_modhash(env)
33
- @modhash = extract_modhash(env)
33
+ modhash = extract_modhash(env)
34
+ @modhash = modhash if modhash
34
35
  rescue JSON::JSONError
35
36
  # Ignore -- modhash can be acquired lazily.
36
37
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module FaradayMiddleware
4
4
  module Reddit
5
- VERSION = '0.4.0'
5
+ VERSION = '0.4.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_middleware-reddit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel O'Brien
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-20 00:00:00.000000000 Z
11
+ date: 2014-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler