Class: MangoPay::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/mangopay/environment.rb

Overview

Holds environment-specific configuration and data. Allows for multiple client handling and configuration from within the same application.

Constant Summary collapse

LOG =
LogProvider.provide(self)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Environment

Returns a new instance of Environment.



27
28
29
30
31
32
# File 'lib/mangopay/environment.rb', line 27

def initialize(id)
  @id = id
  @rate_limit_count = {}
  @rate_limit_remaining = {}
  @rate_limit_reset = {}
end

Instance Attribute Details

#configurationObject

Configuration

Its MangoPay configuration details



16
17
18
# File 'lib/mangopay/environment.rb', line 16

def configuration
  @configuration
end

#idObject (readonly)

Symbol

Its identification symbol



13
14
15
# File 'lib/mangopay/environment.rb', line 13

def id
  @id
end

#rate_limit_countObject

Hash

Counts of the requests sent to the API per time interval



19
20
21
# File 'lib/mangopay/environment.rb', line 19

def rate_limit_count
  @rate_limit_count
end

#rate_limit_remainingObject

Hash

Number of remaining possible calls to be made per time interval



22
23
24
# File 'lib/mangopay/environment.rb', line 22

def rate_limit_remaining
  @rate_limit_remaining
end

#rate_limit_resetObject

Hash

UNIX times at which counts will be reset per time interval



25
26
27
# File 'lib/mangopay/environment.rb', line 25

def rate_limit_reset
  @rate_limit_reset
end

Instance Method Details

#time_interval(index) ⇒ Object

Asserts the time interval corresponding to each index of the values returned in API headers.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mangopay/environment.rb', line 51

def time_interval(index)
  case index
  when 0
    RateLimitInterval::FIFTEEN_MIN
  when 1
    RateLimitInterval::THIRTY_MIN
  when 2
    RateLimitInterval::HOUR
  when 3
    RateLimitInterval::DAY
  else
    LOG.warn 'Unexpected rate limit time interval count'
  end
end

#update_rate_limits(rate_limits) ⇒ Object

Updates the rate limit data based on headers from API.

noinspection RubyResolve



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mangopay/environment.rb', line 37

def update_rate_limits(rate_limits)
  rate_limits['x-ratelimit'].each.with_index do |count, index|
    @rate_limit_count[time_interval(index)] = count
  end
  rate_limits['x-ratelimit-remaining'].each.with_index do |remain, index|
    @rate_limit_remaining[time_interval(index)] = remain
  end
  rate_limits['x-ratelimit-reset'].each.with_index do |reset, index|
    @rate_limit_reset[time_interval(index)] = reset
  end
end