Class: Eve::API::Request
- Inherits:
-
Object
- Object
- Eve::API::Request
- Extended by:
- ActiveSupport::Memoizable
- Defined in:
- lib/eve/api/request.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #cache_key ⇒ Object
- #cache_response(xml) ⇒ Object
- #cached_response ⇒ Object
- #dispatch ⇒ Object
-
#initialize(namespace, service, options = {}) ⇒ Request
constructor
A new instance of Request.
- #response_for(body) ⇒ Object
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, = {}) .reverse_merge! namespace = namespace.to_s if namespace.is_a?(Symbol) service = service.to_s if service.is_a?(Symbol) unless [:xml,:string].include? [:response_type] raise ArgumentError, "Expected :response_type to be :xml or :string" end @options = .dup @service = [:camelize] ? service.camelize : service @namespace = namespace @response_type = [:response_type] @uri = File.join(@options.delete(:base_uri), @namespace, "#{@service}.#{[:extension]}") end |
Instance Attribute Details
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
5 6 7 |
# File 'lib/eve/api/request.rb', line 5 def namespace @namespace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/eve/api/request.rb', line 5 def @options end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
5 6 7 |
# File 'lib/eve/api/request.rb', line 5 def response @response end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
5 6 7 |
# File 'lib/eve/api/request.rb', line 5 def service @service end |
#uri ⇒ Object (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_key ⇒ Object
51 52 53 |
# File 'lib/eve/api/request.rb', line 51 def cache_key ActiveSupport::Cache.(, @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 [:cache] response_for xml end |
#cached_response ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/eve/api/request.rb', line 32 def cached_response if xml = ([: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 |
#dispatch ⇒ Object
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), ).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, ) : body end |