class Restfolia::HTTP::Behaviour::Helpers

Internal: Helpers Available to behaviours blocks

Examples

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

Public Instance Methods

follow_url(url) click to toggle source

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.

# 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) click to toggle source

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.

# File lib/restfolia/http/behaviour.rb, line 28
def parse_json(http_response)
  body = http_response.body
  begin
    MultiJson.decode(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