Class: Codelocks::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(faraday_response) ⇒ Response

Initialize the response object


11
12
13
# File 'lib/codelocks/response.rb', line 11

def initialize(faraday_response)
  @response = faraday_response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *opts, &block) ⇒ String

Simple method missing accessor for reading returned attributes


47
48
49
# File 'lib/codelocks/response.rb', line 47

def method_missing(method_name, *opts, &block)
  body[method_name.to_s] if body.is_a?(Hash)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response


5
6
7
# File 'lib/codelocks/response.rb', line 5

def response
  @response
end

Instance Method Details

#bodyHash, Nil

Parse the response from the server


27
28
29
# File 'lib/codelocks/response.rb', line 27

def body
  @body ||= JSON.parse(response.body)
end

#error_messageString

Convenience method for fetching the error message


35
36
37
38
39
40
41
# File 'lib/codelocks/response.rb', line 35

def error_message
  if !success?
    body["Message"]
  end
rescue JSON::ParserError => ex
  ex.message
end

#success?Truthy

Was the request successful?


19
20
21
# File 'lib/codelocks/response.rb', line 19

def success?
  response.success?
end