Class: Plotlyrb::Response

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



2
3
4
# File 'lib/plotlyrb/response.rb', line 2

def body
  @body
end

#errorsObject

Returns the value of attribute errors

Returns:

  • (Object)

    the current value of errors



2
3
4
# File 'lib/plotlyrb/response.rb', line 2

def errors
  @errors
end

#msgObject

Returns the value of attribute msg

Returns:

  • (Object)

    the current value of msg



2
3
4
# File 'lib/plotlyrb/response.rb', line 2

def msg
  @msg
end

#successObject

Returns the value of attribute success

Returns:

  • (Object)

    the current value of success



2
3
4
# File 'lib/plotlyrb/response.rb', line 2

def success
  @success
end

Class Method Details

.all_have_key?(hs, key) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/plotlyrb/response.rb', line 34

def self.all_have_key?(hs, key)
  hs.all? { |h| h.has_key?(key) }
end

.fail(msg) ⇒ Object



18
19
20
# File 'lib/plotlyrb/response.rb', line 18

def self.fail(msg)
  new(false, '', msg, [msg])
end

.from_http_response(rsp) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/plotlyrb/response.rb', line 3

def self.from_http_response(rsp)
  case rsp.code
  when '200'
    new(true, rsp.body, '200 - OK', [])
  when '201'
    new(true, rsp.body, '201 - CREATED', [])
  when '400'
    new(false, rsp.body, '400 - BAD REQUEST', get_errors(rsp.body))
  when '404'
    new(false, rsp.body, '404 - NOT FOUND', ['Appears we got an endpoint wrong'])
  else
    new(false, rsp.body, "#{rsp.code} - UNHANDLED", ['Unhandled error'])
  end
end

.get_errors(s) ⇒ Object

s is a Net::HTTP::Response body we expect to contain JSON map with a list of errors



23
24
25
26
27
28
29
30
31
32
# File 'lib/plotlyrb/response.rb', line 23

def self.get_errors(s)
  msg_key = 'message'
  h = JSON.parse(s)
  es = h['errors']
  if es.nil? || !es.is_a?(Array) || !all_have_key?(es, msg_key)
    return ['Failed to parse plotly error response - check raw body']
  end

  es.map { |e| e.fetch(msg_key) }
end

Instance Method Details

#to_sObject



38
39
40
# File 'lib/plotlyrb/response.rb', line 38

def to_s
  "#{success ? 'Succeeded' : 'Failed'}: #{msg}"
end