class Binocular::Bin

Constants

DEFAULT_SETTINGS

Attributes

bin_number[RW]
errors[R]
info[R]
settings[RW]

Public Class Methods

info(bin_number) click to toggle source
# File lib/binocular.rb, line 12
def self.info bin_number
  self.new(bin_number).fetch_info
end
new(bin_number, settings=DEFAULT_SETTINGS) click to toggle source
# File lib/binocular.rb, line 16
def initialize(bin_number, settings=DEFAULT_SETTINGS)
  self.settings = settings
  self.bin_number = bin_number
end

Public Instance Methods

fetch_info() click to toggle source
# File lib/binocular.rb, line 33
def fetch_info
  return @errors unless valid?
  uri = URI.parse("http://www.binlist.net/json/#{self.bin_number}")

  http = Net::HTTP.new(uri.host, uri.port, self.proxy_host, self.proxy_port)
  http.use_ssl      = false
  http.open_timeout = self.timeout
  http.read_timeout = self.timeout

  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
  @info = JSON.parse response.body
rescue JSON::ParserError => e
  @errors = { errors: response.body }
rescue Exception => e
  @errors = { errors: e.to_s }
end
proxy_host() click to toggle source
# File lib/binocular.rb, line 21
def proxy_host
  self.settings[:proxy_host] ? self.settings[:proxy_host] : nil
end
proxy_port() click to toggle source
# File lib/binocular.rb, line 25
def proxy_port
  self.settings[:proxy_port] ? self.settings[:proxy_port] : nil
end
timeout() click to toggle source
# File lib/binocular.rb, line 29
def timeout
  self.settings[:timeout] ? self.settings[:timeout] : 20
end
valid?() click to toggle source
# File lib/binocular.rb, line 51
def valid?
  if (numeric? self.bin_number) && (valid_length? self.bin_number)
    return true
  else
    @errors = { errors: "bin_number should be 6-length numeric" }
    return false
  end
end

Private Instance Methods

numeric?(val) click to toggle source
# File lib/binocular.rb, line 61
def numeric? val
  val.to_i.to_s == val.to_s
rescue
  false
end
valid_length?(val) click to toggle source
# File lib/binocular.rb, line 67
def valid_length? val
  val.size == 6
end