Class: Response::StubHub

Inherits:
Middleware
  • Object
show all
Defined in:
lib/stub_hub_api/response.rb

Instance Method Summary collapse

Instance Method Details

#call(environment) ⇒ Object



37
38
39
40
41
42
# File 'lib/stub_hub_api/response.rb', line 37

def call(environment)
  @app.call(environment).on_complete do |env|
    check_status(env)
    env[:body] = parse_body(env[:body])
  end
end

#check_status(env) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/stub_hub_api/response.rb', line 27

def check_status(env)
  status = env[:status].to_s
  body   = env[:body]
  if status =~ /^5/
    raise ApiException.new({status: status, body: body})
  elsif status =~ /^4/
    raise ApiException.new({status: status, body: body})
  end
end

#parse_body(body) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/stub_hub_api/response.rb', line 19

def parse_body(body)
  if JSON.is_json?(body)
    JSON.parse(body)
  else
    Nokogiri::XML(body)
  end
end