Class: Morpheus::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Request

Returns a new instance of Request.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/morpheus/request.rb', line 5

def initialize(path, options = {})
  if options[:method] == :put
   options[:method] = :post
   options[:params].merge!(:_method => :put)
  end

  if options[:params]
    options[:params] = fix_array_param_keys(options[:params])
  end


  options[:username] = Configuration.username if Configuration.username
  options[:password] = Configuration.password if Configuration.password

  super(Configuration.host + path, options)
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



3
4
5
# File 'lib/morpheus/request.rb', line 3

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/morpheus/request.rb', line 3

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/morpheus/request.rb', line 3

def path
  @path
end

Class Method Details

.enqueue(method, path, params) ⇒ Object



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

def self.enqueue(method, path, params)
  options = { :method => method }
  options.merge!(:params => params) unless params.blank?
  new(path, options).tap do |request|
   RequestQueue.enqueue(request)
  end
end

Instance Method Details

#cache_keyObject



34
35
36
# File 'lib/morpheus/request.rb', line 34

def cache_key
  Digest::SHA1.hexdigest([method, url, params].inspect)
end

#fix_array_param_keys(params) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/morpheus/request.rb', line 22

def fix_array_param_keys(params)
  fixed_params = {}
  params.each do |key, value|
    if Array === value
      fixed_params.store("#{key}[]", value)
    else
      fixed_params.store(key, value)
    end
  end
  fixed_params
end

#responseObject



44
45
46
47
# File 'lib/morpheus/request.rb', line 44

def response
  RequestQueue.run! if RequestQueue.has_request?(self)
  RequestCache.cache[cache_key] || super
end

#response=(response) ⇒ Object



38
39
40
41
42
# File 'lib/morpheus/request.rb', line 38

def response=(response)
  RequestCache.cache[cache_key] = response
  response.tag_for_caching!
  super
end