Class: SupplejackApi::HarvesterConstraint

Inherits:
Object
  • Object
show all
Defined in:
lib/supplejack_api/harvester_constraint.rb

Instance Method Summary collapse

Constructor Details

#initializeHarvesterConstraint

Returns a new instance of HarvesterConstraint.



11
12
13
14
15
16
# File 'lib/supplejack_api/harvester_constraint.rb', line 11

def initialize
  @ips = ENV['HARVESTER_IPS'].gsub(/\s+/, '').split(',')
rescue
  # Allow supplejack:install generator to work without application.yml file
  @ips = ['127.0.0.1']
end

Instance Method Details

#forwarded_ips(request) ⇒ Object



23
24
25
26
# File 'lib/supplejack_api/harvester_constraint.rb', line 23

def forwarded_ips(request)
  ip_addresses = request.env['HTTP_X_FORWARDED_FOR']
  ip_addresses ? ip_addresses.strip.split(/[,\s]+/) : []
end

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/supplejack_api/harvester_constraint.rb', line 18

def matches?(request)
  forwarded_ips(request).each { |ip| return false unless @ips.include?(ip) }
  @ips.include?(request.remote_ip)
end