class SncfApi::Request

Constants

IMPLEMENTATION
QUOTA_PERIODS

Attributes

api_token[R]

Public Class Methods

drop_instance(api_token: ENV['SNCF_API_TOKEN']) click to toggle source
# File lib/sncf_api/request.rb, line 17
def drop_instance(api_token: ENV['SNCF_API_TOKEN'])
  @instances.delete(api_token)
  @instances.count
end
instance(api_token: ENV['SNCF_API_TOKEN'], plan: {name: 'Free', limits: { per_day: 3_000, per_month: 90_000 }}) click to toggle source
# File lib/sncf_api/request.rb, line 10
def instance(api_token: ENV['SNCF_API_TOKEN'], plan: {name: 'Free', limits: { per_day: 3_000, per_month: 90_000 }})
  api_token = nil if !api_token.is_a?(String) || api_token.strip == ''
  raise ArgumentError, "You MUST specify either api_token argument nor SNCF_API_TOKEN env variable" unless api_token
  @instances ||= {}
  @instances[api_token] ||= new(plan: plan[:limits], api_token: api_token)
end
new(plan:, api_token:) click to toggle source
# File lib/sncf_api/request.rb, line 34
def initialize(plan:, api_token:)
  @plan = plan
  @api_token = api_token
end

Public Instance Methods

countdown() click to toggle source

My understanding of SNCF quota is that the countdown start at the very first call of a dau and the plan start on that instant

# File lib/sncf_api/request.rb, line 24
def countdown
  @countdown ||= default_countdown  
end
fetch(path) click to toggle source
# File lib/sncf_api/request.rb, line 28
def fetch(path)
  SncfApi::Response.new(path: path, request: self)
end

Private Instance Methods

decrease_quotas() click to toggle source
# File lib/sncf_api/request.rb, line 51
def decrease_quotas
  @countdown ||= default_countdown
  right_now = now
  QUOTA_PERIODS.each do |key, period|
    if @countdown["#{key}_started_at".to_sym] <= (right_now - period)
      @countdown[key] = @plan[key]
      @countdown["#{key}_started_at".to_sym] = right_now
    end
  end
  QUOTA_PERIODS.keys.each { |k| @countdown[k] -= 1 }
  @countdown
end
default_countdown() click to toggle source
# File lib/sncf_api/request.rb, line 39
def default_countdown
  { per_day: @plan[:per_day], per_month: @plan[:per_month], per_month_started_at: now, per_day_started_at: now }
end
now() click to toggle source
# File lib/sncf_api/request.rb, line 43
def now
  Time.now.utc
end