Class: Montage::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, body, resource_name = "resource") ⇒ Response

Returns a new instance of Response.



8
9
10
11
12
13
14
# File 'lib/montage/response.rb', line 8

def initialize(status, body, resource_name = "resource")
  @status = status
  @resource_name = resource_name
  @raw_body = body.clone.freeze
  @body = get_body(body)
  @members = parse_members
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



32
33
34
35
# File 'lib/montage/response.rb', line 32

def method_missing(method_name, *args, &block)
  return super unless resource_name.to_sym == method_name || "#{resource_name}s".to_sym == method_name || method_name == "errors".to_sym
  members
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/montage/response.rb', line 6

def body
  @body
end

#membersObject (readonly)

Returns the value of attribute members.



6
7
8
# File 'lib/montage/response.rb', line 6

def members
  @members
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



6
7
8
# File 'lib/montage/response.rb', line 6

def raw_body
  @raw_body
end

#resource_nameObject (readonly)

Returns the value of attribute resource_name.



6
7
8
# File 'lib/montage/response.rb', line 6

def resource_name
  @resource_name
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/montage/response.rb', line 6

def status
  @status
end

Instance Method Details

#get_body(body) ⇒ Object



16
17
18
# File 'lib/montage/response.rb', line 16

def get_body(body)
  resource_name == "error" ? body["errors"] : body["data"] || body
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/montage/response.rb', line 28

def respond_to?(method_name, include_private = false)
  resource_name.to_sym == method_name || "#{resource_name}s".to_sym == method_name || method_name == "errors".to_sym || super
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  if raw_body["errors"]
    return false
  else
    (200..299).include?(status)
  end
end