Class: JustCall::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response



5
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
# File 'lib/justcall_ruby/response.rb', line 5

def initialize(response)
  @response = response

  case @response
  when Net::HTTPUnauthorized
    raise JustCall::Error::NotAuthorized, @response.body
  when Net::HTTPNotFound
    raise JustCall::Error::NotFound, @response.body
  when Net::HTTPOK, Net::HTTPSuccess, Net::HTTPNoContent, Net::HTTPCreated
    @success = true
    body = (JSON.parse(@response.body) if @response.body.present?)
    @body =
      if body.is_a?(Hash)
        body.deep_symbolize_keys
      elsif body.is_a?(Array)
        body.map(&:deep_symbolize_keys)
      end
  else
    puts "-- DEBUG: #{self}: RequestError: #{@response.inspect}" if JustCall.config.full_debug?

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

    raise JustCall::Error::RequestError, error_message
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/justcall_ruby/response.rb', line 3

def body
  @body
end

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/justcall_ruby/response.rb', line 3

def response
  @response
end

#successObject (readonly)

Returns the value of attribute success.



3
4
5
# File 'lib/justcall_ruby/response.rb', line 3

def success
  @success
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
# File 'lib/justcall_ruby/response.rb', line 35

def [](key)
  body[key]
end

#fetch(key) ⇒ Object



39
40
41
# File 'lib/justcall_ruby/response.rb', line 39

def fetch(key)
  body.fetch(key)
end

#success?Boolean



43
44
45
# File 'lib/justcall_ruby/response.rb', line 43

def success?
  !!success
end