Class: SmoothOperator::RemoteCall::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/smooth_operator/remote_call.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, object_class = nil) ⇒ Base

Returns a new instance of Base.



19
20
21
# File 'lib/smooth_operator/remote_call.rb', line 19

def initialize(response, object_class = nil)
  @response, @object_class = response, object_class
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



11
12
13
# File 'lib/smooth_operator/remote_call.rb', line 11

def object
  @object
end

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/smooth_operator/remote_call.rb', line 9

def response
  @response
end

Instance Method Details

#dataObject



50
51
52
# File 'lib/smooth_operator/remote_call.rb', line 50

def data
  object.nil? ? parsed_response : object
end

#error?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/smooth_operator/remote_call.rb', line 28

def error?
  http_status.between?(500, 599)
end

#failure?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/smooth_operator/remote_call.rb', line 24

def failure?
  http_status.between?(400, 499)
end

#objectsObject



46
47
48
# File 'lib/smooth_operator/remote_call.rb', line 46

def objects
  object.respond_to?(:length) ? object : []
end

#parsed_responseObject



32
33
34
35
36
37
38
39
40
# File 'lib/smooth_operator/remote_call.rb', line 32

def parsed_response
  require 'json' unless defined? JSON
  
  begin
    JSON.parse(body)
  rescue JSON::ParserError
    nil
  end
end

#statusObject



42
43
44
# File 'lib/smooth_operator/remote_call.rb', line 42

def status
  error? ? nil : success?
end