Class: Eve::API::Request
- Inherits:
-
Object
- Object
- Eve::API::Request
- 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 ⇒ 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.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/eve/api/request.rb', line 6 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.
4 5 6 |
# File 'lib/eve/api/request.rb', line 4 def namespace @namespace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/eve/api/request.rb', line 4 def @options end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
4 5 6 |
# File 'lib/eve/api/request.rb', line 4 def response @response end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
4 5 6 |
# File 'lib/eve/api/request.rb', line 4 def service @service end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
4 5 6 |
# File 'lib/eve/api/request.rb', line 4 def uri @uri end |
Instance Method Details
#cache_key ⇒ Object
59 60 61 |
# File 'lib/eve/api/request.rb', line 59 def cache_key @cache_key ||= ActiveSupport::Cache.(, @uri) end |
#cache_response ⇒ Object
53 54 55 56 57 |
# File 'lib/eve/api/request.rb', line 53 def cache_response xml = yield Eve.cache.write(cache_key, xml) if [:cache] response_for xml end |
#cached_response ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/eve/api/request.rb', line 39 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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/eve/api/request.rb', line 23 def dispatch r = cached_response || cache_response { url = URI.parse uri request = Net::HTTP::Post.new url.path request.set_form_data http = Net::HTTP.new url.host, url.port http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.start { |http| http.request(request) }.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
49 50 51 |
# File 'lib/eve/api/request.rb', line 49 def response_for(body) @response_type == :xml ? Eve::API::Response.new(body, ) : body end |