Exception: WOTC::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/wotc/error.rb

Overview

Custom error class for rescuing from all wotc.com errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Error

Returns a new instance of Error.



6
7
8
9
10
11
12
13
14
# File 'lib/wotc/error.rb', line 6

def initialize(response)
  super
  @response = response.dup
  @http_method = response.method.to_s
  @url = response.url
  if response.body.is_a?(Hash) && !response.body.empty? && !response.body.fetch("errors", nil).nil?
    @raw_errors = response.body.fetch("errors")
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/wotc/error.rb', line 4

def errors
  @errors
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



4
5
6
# File 'lib/wotc/error.rb', line 4

def http_method
  @http_method
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/wotc/error.rb', line 4

def url
  @url
end

Instance Method Details

#error_sentenceObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/wotc/error.rb', line 29

def error_sentence
  return if @raw_errors.nil?

  array = []
  @raw_errors.each do |_, v| 
    array += v
  end

  array.join(' ')
end

#messageObject



16
17
18
19
20
21
22
23
# File 'lib/wotc/error.rb', line 16

def message
  <<-HEREDOC
  URL: #{@response.url}
  method: #{@response.method}
  response status: #{@response.status}
  response body: #{@response.response.body}
  HEREDOC
end

#raw_errorsObject



25
26
27
# File 'lib/wotc/error.rb', line 25

def raw_errors
  @raw_errors
end