Class: ThreeScale::Backend::Transactor::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale/backend/transactor/status.rb,
lib/3scale/backend/transactor/usage_report.rb

Defined Under Namespace

Classes: UsageReport

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Status

Returns a new instance of Status.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/3scale/backend/transactor/status.rb', line 17

def initialize(attributes)
  @service_id        = attributes[:service_id]
  @application       = attributes[:application]
  @oauth             = attributes[:oauth]
  @usage             = attributes[:usage]
  @predicted_usage   = attributes[:predicted_usage]
  @values            = filter_values(attributes[:values] || {})
  @timestamp         = attributes[:timestamp] || Time.now.getutc
  @hierarchy_ext     = attributes[:hierarchy]
  @flat_usage_ext    = attributes[:flat_usage]
  @list_app_keys_ext = attributes[:list_app_keys]

  raise 'service_id not specified' if @service_id.nil?
  raise ':application is required' if @application.nil?

  @redirect_uri_field = REDIRECT_URI_FIELD
  @authorized  = true
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



37
38
39
# File 'lib/3scale/backend/transactor/status.rb', line 37

def application
  @application
end

#oauthObject (readonly)

Returns the value of attribute oauth.



38
39
40
# File 'lib/3scale/backend/transactor/status.rb', line 38

def oauth
  @oauth
end

#redirect_uri_fieldObject

Returns the value of attribute redirect_uri_field.



39
40
41
# File 'lib/3scale/backend/transactor/status.rb', line 39

def redirect_uri_field
  @redirect_uri_field
end

#rejection_reason_codeObject (readonly)

Returns the value of attribute rejection_reason_code.



53
54
55
# File 'lib/3scale/backend/transactor/status.rb', line 53

def rejection_reason_code
  @rejection_reason_code
end

#rejection_reason_textObject (readonly)

Returns the value of attribute rejection_reason_text.



54
55
56
# File 'lib/3scale/backend/transactor/status.rb', line 54

def rejection_reason_text
  @rejection_reason_text
end

#service_idObject (readonly)

Returns the value of attribute service_id.



36
37
38
# File 'lib/3scale/backend/transactor/status.rb', line 36

def service_id
  @service_id
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



52
53
54
# File 'lib/3scale/backend/transactor/status.rb', line 52

def timestamp
  @timestamp
end

#valuesObject

Returns the value of attribute values.



40
41
42
# File 'lib/3scale/backend/transactor/status.rb', line 40

def values
  @values
end

Instance Method Details

#actual_or_predicted_usageObject

Returns the actual usage. If there isn’t one, returns the predicted usage. If there isn’t an actual or predicted usage, returns nil.



68
69
70
# File 'lib/3scale/backend/transactor/status.rb', line 68

def actual_or_predicted_usage
  usage || predicted_usage
end

#application_usage_reportsObject



80
81
82
# File 'lib/3scale/backend/transactor/status.rb', line 80

def application_usage_reports
  @usage_report ||= load_usage_reports @application
end

#authorized?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/3scale/backend/transactor/status.rb', line 72

def authorized?
  @authorized
end

#flat_usageObject



42
43
44
# File 'lib/3scale/backend/transactor/status.rb', line 42

def flat_usage
  @flat_usage_ext
end

#hierarchyObject

provides a hierarchy hash with metrics as symbolic names



90
91
92
# File 'lib/3scale/backend/transactor/status.rb', line 90

def hierarchy
  @hierarchy ||= Metric.hierarchy service_id
end

#limit_headers(now = Time.now.utc) ⇒ Object



94
95
96
97
# File 'lib/3scale/backend/transactor/status.rb', line 94

def limit_headers(now = Time.now.utc)
  # maybe filter by exceeded reports if not authorized
  LimitHeaders.get(reports_to_calculate_limit_headers, now)
end

#plan_nameObject



76
77
78
# File 'lib/3scale/backend/transactor/status.rb', line 76

def plan_name
  @application.plan_name unless @application.nil?
end

#predicted_usageObject

Returns the predicted usage of an authorize request.



62
63
64
# File 'lib/3scale/backend/transactor/status.rb', line 62

def predicted_usage
  @predicted_usage ? @usage : nil
end

#reject!(error) ⇒ Object



46
47
48
49
50
# File 'lib/3scale/backend/transactor/status.rb', line 46

def reject!(error)
  @authorized = false
  @rejection_reason_code ||= error.code
  @rejection_reason_text ||= error.message
end

#to_xml(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/3scale/backend/transactor/status.rb', line 99

def to_xml(options = {})
  xml = ''
  xml << '<?xml version="1.0" encoding="UTF-8"?>'.freeze unless options[:skip_instruct]
  xml << '<status>'.freeze

  add_authorize_section(xml)

  if oauth
    add_application_section(xml)
  end

  hierarchy_reports = [] if @hierarchy_ext
  if !@application.nil?
    add_plan_section(xml, 'plan'.freeze, plan_name)
    add_reports_section(xml, application_usage_reports)
    hierarchy_reports.concat application_usage_reports if hierarchy_reports
    add_app_keys_section xml if @list_app_keys_ext
  end

  if hierarchy_reports
    add_hierarchy_section(xml, hierarchy_reports)
  end

  xml << '</status>'.freeze
end

#usageObject

Returns the usage to be reported in an authrep request.



57
58
59
# File 'lib/3scale/backend/transactor/status.rb', line 57

def usage
  @predicted_usage ? nil : @usage
end

#value_for_usage_limit(usage_limit) ⇒ Object



84
85
86
87
# File 'lib/3scale/backend/transactor/status.rb', line 84

def value_for_usage_limit(usage_limit)
  values = @values[usage_limit.period]
  values && values[usage_limit.metric_id] || 0
end