Module: JDX::Geocoder

Defined in:
lib/jdx/geocoder.rb,
lib/jdx/geocoder/base.rb,
lib/jdx/geocoder/google.rb,
lib/jdx/geocoder/ip_api.rb,
lib/jdx/geocoder/request.rb,
lib/jdx/geocoder/version.rb

Defined Under Namespace

Modules: Base, Request Classes: Configuration, Google, IpApi

Constant Summary collapse

STATE_ABB =
{
  'al' => 'alabama',
  'ak' => 'alaska',
  'as' => 'america samoa',
  'az' => 'arizona',
  'ar' => 'arkansas',
  'ca' => 'california',
  'co' => 'colorado',
  'ct' => 'connecticut',
  'de' => 'delaware',
  'dc' => 'district of columbia',
  'fm' => 'federated states of micronesia',
  'fl' => 'florida',
  'ga' => 'georgia',
  'gu' => 'guam',
  'hi' => 'hawaii',
  'id' => 'idaho',
  'il' => 'illinois',
  'in' => 'indiana',
  'ia' => 'iowa',
  'ks' => 'kansas',
  'ky' => 'kentucky',
  'la' => 'louisiana',
  'me' => 'maine',
  'mh' => 'marshall islands',
  'md' => 'maryland',
  'ma' => 'massachusetts',
  'mi' => 'michigan',
  'mn' => 'minnesota',
  'ms' => 'mississippi',
  'mo' => 'missouri',
  'mt' => 'montana',
  'ne' => 'nebraska',
  'nv' => 'nevada',
  'nh' => 'new hampshire',
  'nj' => 'new jersey',
  'nm' => 'new mexico',
  'ny' => 'new york',
  'nc' => 'north carolina',
  'nd' => 'north dakota',
  'oh' => 'ohio',
  'ok' => 'oklahoma',
  'or' => 'oregon',
  'pw' => 'palau',
  'pa' => 'pennsylvania',
  'pr' => 'puerto rico',
  'ri' => 'rhode island',
  'sc' => 'south carolina',
  'sd' => 'south dakota',
  'tn' => 'tennessee',
  'tx' => 'texas',
  'ut' => 'utah',
  'vt' => 'vermont',
  'vi' => 'virgin island',
  'va' => 'virginia',
  'wa' => 'washington',
  'wv' => 'west virginia',
  'wi' => 'wisconsin',
  'wy' => 'wyoming'
}.freeze
STATE_ABB_REGEXP =
Regexp.new(STATE_ABB.keys.map { |x| "\\b#{x}\\b" }.join('|')).freeze
VERSION =
"0.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



74
75
76
# File 'lib/jdx/geocoder.rb', line 74

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



76
77
78
79
# File 'lib/jdx/geocoder.rb', line 76

def configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.countrywide?(down) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/jdx/geocoder.rb', line 107

def countrywide?(down)
  down.match?(/\b(nation?.|country?.)wide\b/)
end

.extract_zipcode(down) ⇒ Object



111
112
113
# File 'lib/jdx/geocoder.rb', line 111

def extract_zipcode(down)
  down.match(/\b[0-9]{5}(?:-[0-9]{4})?\b/)&.[](0)
end

.ip_search(ip, validate: false) ⇒ Object



90
91
92
93
94
# File 'lib/jdx/geocoder.rb', line 90

def ip_search(ip, validate: false)
  return nil if ip.nil? || ip.empty? || validate && !valid_ip?(ip)

  ::JDX::Geocoder::IpApi.new(ip).search
end

.location_search(term, sanitize: true) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/jdx/geocoder.rb', line 81

def location_search(term, sanitize: true)
  return if term.nil?

  term = sanitize(term) if sanitize
  return if term.empty?

  ::JDX::Geocoder::Google.new(term).search
end

.remove_duplicates(down) ⇒ Object



120
121
122
# File 'lib/jdx/geocoder.rb', line 120

def remove_duplicates(down)
  down.split(/\W+/).tap(&:uniq!).join(' ').tap(&:strip!)
end

.remove_stuff(down) ⇒ Object



115
116
117
118
# File 'lib/jdx/geocoder.rb', line 115

def remove_stuff(down)
  down.gsub!(%r{\b(?:united\W*st?ates(?:\W*of\W*america)?|u\W*s\W*a?|n/a)\b}, '')
  down.gsub!(STATE_ABB_REGEXP, STATE_ABB)
end

.sanitize(term) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/jdx/geocoder.rb', line 96

def sanitize(term)
  down = term.downcase
  return 'us' if countrywide?(down)

  zip = extract_zipcode(down)
  return zip unless zip.nil?

  remove_stuff(down)
  remove_duplicates(down)
end

.valid_ip?(ip) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
# File 'lib/jdx/geocoder.rb', line 124

def valid_ip?(ip)
  case ip
  when Resolv::IPv4::Regex, Resolv::IPv6::Regex
    true
  else
    false
  end
end