Class: DubbletrackRemote::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/dubbletrack_remote/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Error

Returns a new instance of Error.



10
11
12
13
14
# File 'lib/dubbletrack_remote/errors.rb', line 10

def initialize(response)
  @time = Time.now.iso8601
  @code = response.status
  @text = error_text(response)
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



8
9
10
# File 'lib/dubbletrack_remote/errors.rb', line 8

def code
  @code
end

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/dubbletrack_remote/errors.rb', line 8

def text
  @text
end

#timeObject

Returns the value of attribute time.



8
9
10
# File 'lib/dubbletrack_remote/errors.rb', line 8

def time
  @time
end

Instance Method Details

#error_text(response) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dubbletrack_remote/errors.rb', line 16

def error_text(response)
  error_text = response.body
  begin
    json = JSON.parse(response.body)
    errors = json["errors"] || []
    error_text = errors.flatten.collect { |error| error["detail"] }.join(",")
  rescue JSON::ParserError
    # keep the error text as response.body
  end
  error_text
end

#to_jsonObject



28
29
30
31
32
33
34
# File 'lib/dubbletrack_remote/errors.rb', line 28

def to_json
  {
    text: @text,
    code: @code,
    time: @time
  }
end