Class: Restfolia::HTTP::Behaviour::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/restfolia/http/behaviour.rb

Overview

Internal: Helpers Available to behaviours blocks

Examples

Restfolia::HTTP.behaviours do
  on(200) do |http_response|
    helpers.parse_json(http_response.body)
  end
end

Instance Method Summary collapse

Instance Method Details

#follow_url(url) ⇒ Object

Public: Request url with GET and forwards to Restfolia::HTTP.

url - String. Ex: service.com/resources

Returns what Restfolia::HTTP.response_by_status_code returns.



43
44
45
46
# File 'lib/restfolia/http/behaviour.rb', line 43

def follow_url(url)
  http_resp = Request.do_request(:get, url)
  Restfolia::HTTP.response_by_status_code(http_resp)
end

#parse_json(http_response) ⇒ Object

Internal: Parse response body, checking for errors.

http_response - HTTP Response with body. Expected to be a JSON.

Returns Hash who represents JSON parsed. Raises Restfolia::ResponseError if body seens invalid somehow.



28
29
30
31
32
33
34
35
36
# File 'lib/restfolia/http/behaviour.rb', line 28

def parse_json(http_response)
  body = http_response.body
  begin
    MultiJson.load(body, :symbolize_keys => true)
  rescue MultiJson::DecodeError => ex
    msg = "Body should be a valid json. #{ex.message}"
    raise Restfolia::ResponseError.new(msg, caller, http_response)
  end
end