Class: Calculated::Session
- Inherits:
-
Object
- Object
- Calculated::Session
- Defined in:
- lib/calculated/session.rb
Defined Under Namespace
Classes: CalculatedError, Expired, MissingParameter, NotAuthenticated, NotFound, PermissionDenied, ServerNotFound, UnAuthorized, UnknownError
Instance Attribute Summary collapse
-
#api_version ⇒ Object
accessors.
-
#cache ⇒ Object
accessors.
-
#caching ⇒ Object
accessors.
-
#expires_in ⇒ Object
accessors.
-
#logging ⇒ Object
accessors.
-
#server ⇒ Object
accessors.
Class Method Summary collapse
-
.create(options = {}) ⇒ Calculated::Session
creating a session one must supply the api_key as this is always required basically.
Instance Method Summary collapse
-
#api_call(method, path, params = {}, &proc) ⇒ Object
if we caching and we have the same cache lets try and get the cache; otherwise we will make the request logging if need be.
-
#api_call_without_logging(method, path, params = {}, &proc) ⇒ Object
api calls.
- #caching? ⇒ Boolean
-
#initialize(api_key, options) ⇒ Session
constructor
default options caching => true expires_in => 60*60*24 cache => Moneta::Memory.new server => “api.carboncalculated.com” api_version => “v1” logging => true.
- #service ⇒ Object
Methods included from AnswerApiCalls
#answer_for_calculator, #answer_for_computation
Methods included from RelatableCategoryApiCalls
#relatable_categories, #relatable_category, #related_categories_from_relatable_category, #related_objects_from_relatable_categories
Methods included from ObjectTemplateApiCalls
#generic_objects_for_object_template, #generic_objects_for_object_template_with_filter, #object_template, #object_templates, #relatable_categories_for_object_template
Methods included from GenericObjectApiCalls
#formula_inputs_for_generic_object, #generic_object, #generic_objects
Constructor Details
#initialize(api_key, options) ⇒ Session
default options
caching => true
expires_in => 60*60*24
cache => Moneta::Memory.new
server => "api.carboncalculated.com"
api_version => "v1"
logging => true
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/calculated/session.rb', line 42 def initialize(api_key, ) @api_key = api_key if @caching = [:caching].nil? ? true : .delete(:caching) @expires_in = .delete(:expires_in) || 60*60*24 @cache = .delete(:cache) || Moneta::Memory.new end @server = .delete(:server) || "api-stage.carboncalculated.com" @api_version = .delete(:api_version) || "v1" @logging = [:logging].nil? ? true : .delete(:caching) end |
Instance Attribute Details
#api_version ⇒ Object
accessors
22 23 24 |
# File 'lib/calculated/session.rb', line 22 def api_version @api_version end |
#cache ⇒ Object
accessors
22 23 24 |
# File 'lib/calculated/session.rb', line 22 def cache @cache end |
#caching ⇒ Object
accessors
22 23 24 |
# File 'lib/calculated/session.rb', line 22 def caching @caching end |
#expires_in ⇒ Object
accessors
22 23 24 |
# File 'lib/calculated/session.rb', line 22 def expires_in @expires_in end |
#logging ⇒ Object
accessors
22 23 24 |
# File 'lib/calculated/session.rb', line 22 def logging @logging end |
#server ⇒ Object
accessors
22 23 24 |
# File 'lib/calculated/session.rb', line 22 def server @server end |
Class Method Details
.create(options = {}) ⇒ Calculated::Session
creating a session one must supply the api_key as this is always required basically
27 28 29 30 31 |
# File 'lib/calculated/session.rb', line 27 def self.create( = {}) api_key = .delete(:api_key) raise ArgumentError.new("You need an API Key to save the planet") unless api_key new(api_key, ) end |
Instance Method Details
#api_call(method, path, params = {}, &proc) ⇒ Object
if we caching and we have the same cache lets try and get the cache; otherwise we will make the request logging if need be
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/calculated/session.rb', line 64 def api_call(method, path, params ={}, &proc) if cache = caching? && (@cache[cache_key(path, params)]) return cache else if @logging Calculated::Logging.log_calculated_api(method, path, params) do api_call_without_logging(method, path, params, &proc) end else api_call_without_logging(method, path, params, &proc) end end end |
#api_call_without_logging(method, path, params = {}, &proc) ⇒ Object
api calls
55 56 57 58 59 60 |
# File 'lib/calculated/session.rb', line 55 def api_call_without_logging(method, path, params = {}, &proc) result = service.send(method, path, params) result = yield result if block_given? store_call(result, path, params || {}) if caching? result end |
#caching? ⇒ Boolean
82 83 84 |
# File 'lib/calculated/session.rb', line 82 def caching? @caching end |
#service ⇒ Object
78 79 80 |
# File 'lib/calculated/session.rb', line 78 def service @service ||= Calculated::Service.new(@server, @api_version, @api_key) end |