Class: Calculated::Session

Inherits:
Object
  • Object
show all
Includes:
AnswerApiCalls, GenericObjectApiCalls, ObjectTemplateApiCalls, RelatableCategoryApiCalls
Defined in:
lib/calculated/session.rb

Defined Under Namespace

Classes: CalculatedError, Expired, MissingParameter, NotAuthenticated, NotFound, PermissionDenied, ServerNotFound, UnAuthorized, UnknownError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • api_key (String)
  • options (Hash)


42
43
44
45
46
47
48
49
50
51
# File 'lib/calculated/session.rb', line 42

def initialize(api_key, options)
  @api_key = api_key
  if @caching = options[:caching].nil? ? true : options.delete(:caching)
    @expires_in = options.delete(:expires_in) || 60*60*24
    @cache = options.delete(:cache) || Moneta::Memory.new 
  end
  @server = options.delete(:server) || "api-stage.carboncalculated.com"
  @api_version = options.delete(:api_version) || "v1"
  @logging = options[:logging].nil? ? true : options.delete(:caching)
end

Instance Attribute Details

#api_versionObject

accessors



22
23
24
# File 'lib/calculated/session.rb', line 22

def api_version
  @api_version
end

#cacheObject

accessors



22
23
24
# File 'lib/calculated/session.rb', line 22

def cache
  @cache
end

#cachingObject

accessors



22
23
24
# File 'lib/calculated/session.rb', line 22

def caching
  @caching
end

#expires_inObject

accessors



22
23
24
# File 'lib/calculated/session.rb', line 22

def expires_in
  @expires_in
end

#loggingObject

accessors



22
23
24
# File 'lib/calculated/session.rb', line 22

def logging
  @logging
end

#serverObject

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

Parameters:

  • options (Hash) (defaults to: {})

Returns:

Raises:

  • (ArgumentError)


27
28
29
30
31
# File 'lib/calculated/session.rb', line 27

def self.create(options = {})
  api_key = options.delete(:api_key)
  raise ArgumentError.new("You need an API Key to save the planet") unless api_key
  new(api_key, options)
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

Returns:

  • (Boolean)


82
83
84
# File 'lib/calculated/session.rb', line 82

def caching?
  @caching
end

#serviceObject



78
79
80
# File 'lib/calculated/session.rb', line 78

def service 
  @service ||= Calculated::Service.new(@server, @api_version, @api_key)
end