Class: Eve::API::Request

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Memoizable
Defined in:
lib/eve/api/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, service, options = {}) ⇒ Request

Returns a new instance of Request.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/eve/api/request.rb', line 7

def initialize(namespace, service, options = {})
  options.reverse_merge! default_options
  namespace = namespace.to_s if namespace.is_a?(Symbol)
  service   = service.to_s   if service.is_a?(Symbol)

  unless [:xml,:string].include? options[:response_type]
    raise ArgumentError, "Expected :response_type to be :xml or :string"
  end

  @options = options.dup
  @service = options[:camelize] ? service.camelize : service
  @namespace = namespace
  @response_type = options[:response_type]

  @uri = File.join(@options.delete(:base_uri), @namespace, "#{@service}.#{options[:extension]}")
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#serviceObject (readonly)

Returns the value of attribute service.



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

def service
  @service
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#cache_keyObject



51
52
53
# File 'lib/eve/api/request.rb', line 51

def cache_key
  ActiveSupport::Cache.expand_cache_key(post_options, @uri)
end

#cache_response(xml) ⇒ Object



46
47
48
49
# File 'lib/eve/api/request.rb', line 46

def cache_response(xml)
  Eve.cache.write(cache_key, xml) if options[:cache]
  response_for xml
end

#cached_responseObject



32
33
34
35
36
37
38
39
40
# File 'lib/eve/api/request.rb', line 32

def cached_response
  if xml = (options[:cache] ? Eve.cache.read(cache_key) : nil)
    potential_response = response_for(xml)
    if !potential_response.respond_to?(:cached_until) || potential_response.cached_until >= Time.now
      return potential_response
    end
  end
  nil
end

#dispatchObject



24
25
26
27
28
29
30
# File 'lib/eve/api/request.rb', line 24

def dispatch
  r = (cached_response || cache_response(Net::HTTP.post_form(URI.parse(uri), post_options).body))
  if r.respond_to?(:error) && r.error
    Eve::Errors.raise(:code => r.error.code, :message => r.error)
  end
  r
end

#response_for(body) ⇒ Object



42
43
44
# File 'lib/eve/api/request.rb', line 42

def response_for(body)
  @response_type == :xml ? Eve::API::Response.new(body, options) : body
end