Class: ThreeScale::Backend::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale/backend/transaction.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Transaction

Returns a new instance of Transaction.



22
23
24
# File 'lib/3scale/backend/transaction.rb', line 22

def initialize(params = {})
  ATTRIBUTES.each { |attr| send("#{attr}=", (params[attr] || params[attr.to_s])) }
end

Instance Method Details

#ensure_on_time!unspecified

Validates if transaction timestamp is within accepted range

Returns:

  • (unspecified)

    if the timestamp is within the valid range.

Raises:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/3scale/backend/transaction.rb', line 49

def ensure_on_time!
  time_diff_sec = timestamp.to_i - Time.now.to_i

  unless DEADLINE_RANGE.cover?(time_diff_sec)
    if time_diff_sec < 0
      fail(TransactionTimestampTooOld, REPORT_DEADLINE_PAST)
    else
      fail(TransactionTimestampTooNew, REPORT_DEADLINE_FUTURE)
    end
  end
end

#extract_response_codeObject



35
36
37
38
39
40
41
42
# File 'lib/3scale/backend/transaction.rb', line 35

def extract_response_code
  if (response_code.is_a?(String) && response_code =~ /\A\d{3}\z/) ||
     (response_code.is_a?(Integer) && (100 ..999).cover?(response_code) )
    response_code.to_i
  else
    false
  end
end

#timestamp=(value = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/3scale/backend/transaction.rb', line 26

def timestamp=(value = nil)
  if value.is_a?(Time)
    @timestamp = value
  else
    @timestamp = Time.parse_to_utc(value) || Time.now.getutc
  end
end