Class: FastBound::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/fastbound-ruby/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fastbound-ruby/response.rb', line 6

def initialize(response)
  @response = response

  case @response
  when Net::HTTPUnauthorized
    raise FastBound::Error::NotAuthorized.new(@response.body)
  when Net::HTTPNotFound
    raise FastBound::Error::NotFound.new(@response.body)
  when Net::HTTPOK, Net::HTTPSuccess, Net::HTTPNoContent, Net::HTTPCreated
    self.success = true
    _data = (JSON.parse(@response.body) if @response.body.present?)

    @data = case
    when _data.is_a?(Hash)
      _data.deep_symbolize_keys
    when _data.is_a?(Array)
      _data.map(&:deep_symbolize_keys)
    end
  else
    if FastBound.config.full_debug?
      puts "-- DEBUG: #{self}: RequestError: #{@response.inspect}"
    end

    error_message = begin
      JSON.parse(@response.body)['errors'].map { |error| error['message'].chomp('.') }.join('. ')
    rescue
      [@response.message, @response.body].reject(&:blank?).join(" | ")
    end

    raise FastBound::Error::RequestError.new(error_message)
  end
end

Instance Attribute Details

#successObject

Returns the value of attribute success.



4
5
6
# File 'lib/fastbound-ruby/response.rb', line 4

def success
  @success
end

Instance Method Details

#[](key) ⇒ Object



39
40
41
# File 'lib/fastbound-ruby/response.rb', line 39

def [](key)
  @data[key]
end

#bodyObject



43
44
45
# File 'lib/fastbound-ruby/response.rb', line 43

def body
  @data
end

#fetch(key) ⇒ Object



47
48
49
# File 'lib/fastbound-ruby/response.rb', line 47

def fetch(key)
  @data.fetch(key)
end

#success?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/fastbound-ruby/response.rb', line 51

def success?
  !!success
end