Class: LazyResource::Request

Inherits:
Typhoeus::Request
  • Object
show all
Defined in:
lib/lazy_resource/request.rb

Constant Summary collapse

SUCCESS_STATUSES =
200...300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, resource, options = {}) ⇒ Request

Returns a new instance of Request.



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
38
39
40
41
# File 'lib/lazy_resource/request.rb', line 7

def initialize(url, resource, options={})
  options = options.dup
  options[:headers] = (options[:headers] || {}).dup
  options[:headers][:Accept] ||= 'application/json'
  options[:headers].merge!(Thread.current[:default_headers]) unless Thread.current[:default_headers].nil?

  params = (URI.parse(url).query || '')
              .split('&')
              .map { |param| param.split('=') }
              .inject({}) { |memo, (k,v)| memo[URI.unescape(k)] = v.nil? ? v : URI.unescape(v); memo }

  url.gsub!(/\?.*/, '')

  options[:params] ||= {}
  options[:params].merge!(params)
  options[:params].merge!(Thread.current[:default_params]) unless Thread.current[:default_params].nil?

  options[:method] ||= :get

  if [:post, :put].include?(options[:method])
    options[:headers]['Content-Type'] = 'application/json'
  end

  super(url, options)

  @resource = resource

  self.on_complete do
    log_response(response) if LazyResource.debug && LazyResource.logger
    @response = response
    parse
  end

  self
end

Instance Attribute Details

#resourceObject

Returns the value of attribute resource.



5
6
7
# File 'lib/lazy_resource/request.rb', line 5

def resource
  @resource
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/lazy_resource/request.rb', line 5

def response
  @response
end

Instance Method Details

#errorObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lazy_resource/request.rb', line 57

def error
  case @response.code
  when 300...400
    Redirection.new(@response)
  when 400
    BadRequest.new(@response)
  when 401
    UnauthorizedAccess.new(@response)
  when 403
    ForbiddenAccess.new(@response)
  when 404
    ResourceNotFound.new(@response)
  when 405
    MethodNotAllowed.new(@response)
  when 409
    ResourceConflict.new(@response)
  when 410
    ResourceGone.new(@response)
  when 422
    UnprocessableEntity.new(@response)
  when 400...500
    ClientError.new(@response)
  when 500...600
    ServerError.new(@response)
  end
end

#log_response(response) ⇒ Object



43
44
45
# File 'lib/lazy_resource/request.rb', line 43

def log_response(response)
  ActiveSupport::Notifications.instrument('request.lazy_resource', code: response.code, time: response.time, url: url)
end

#parseObject



47
48
49
50
51
52
53
54
55
# File 'lib/lazy_resource/request.rb', line 47

def parse
  unless SUCCESS_STATUSES.include?(@response.code)
    @resource.request_error = error
  end

  unless self.response.body.nil? || self.response.body == ''
    @resource.load(JSON.parse(self.response.body))
  end
end