Class: Tumblr::Parsing

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/tumblr/middleware/parsing.rb

Instance Method Summary collapse

Instance Method Details

#handle_formatting_error(env) ⇒ Object



29
30
31
32
# File 'lib/tumblr/middleware/parsing.rb', line 29

def handle_formatting_error(env)
  env[:status] = 500
  env[:message] = "Poorly formatted response"
end

#on_complete(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tumblr/middleware/parsing.rb', line 16

def on_complete(env)
  parsed = parse_body(env[:body])
  
  if parsed && parsed.kind_of?(Hash) && parsed["meta"]
    env[:status] = parsed["meta"]["status"]
    env[:message] = parsed["meta"]["msg"]
    env[:body] = Tumblr::Object.objectify(parsed["response"])
  else
    handle_formatting_error(env)
  end
  
end

#parse_body(body) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/tumblr/middleware/parsing.rb', line 3

def parse_body(body)
  case body
  when ''
    nil
  when 'true'
    true
  when 'false'
    false
  else
    JSON.parse(body) rescue nil
  end
end