Class: RedditKit::Response::ParseJSON
- Inherits:
-
Faraday::Response::Middleware
- Object
- Faraday::Response::Middleware
- RedditKit::Response::ParseJSON
- Defined in:
- lib/redditkit/response/parse_json.rb
Instance Method Summary collapse
- #bad_status_codes ⇒ Object
- #on_complete(env) ⇒ Object
-
#parse(body) ⇒ Object
Turn the response body into a JSON object.
Instance Method Details
#bad_status_codes ⇒ Object
29 30 31 |
# File 'lib/redditkit/response/parse_json.rb', line 29 def bad_status_codes @status_codes ||= [204, 301, 302, 304] end |
#on_complete(env) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/redditkit/response/parse_json.rb', line 21 def on_complete(env) if respond_to?(:parse) unless bad_status_codes.include? env[:status] env[:body] = parse env[:body] end end end |
#parse(body) ⇒ Object
Note:
Because the reddit API sometimes returns invalid JSON objects with an application/json header, we want to return the body itself if the JSON parsing fails, because the response is still likely useful.
Turn the response body into a JSON object.
15 16 17 18 19 |
# File 'lib/redditkit/response/parse_json.rb', line 15 def parse(body) MultiJson.load(body, :symbolize_keys => true) unless body.nil? rescue MultiJson::LoadError body end |