Class: BaseScraper::Service::LocationService

Inherits:
Object
  • Object
show all
Defined in:
lib/base_scraper_service/location_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ LocationService

Returns a new instance of LocationService.



4
5
6
# File 'lib/base_scraper_service/location_service.rb', line 4

def initialize(address)
  @address = address.to_s.gsub('.', '')
end

Instance Method Details

#extract_countryObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/base_scraper_service/location_service.rb', line 23

def extract_country
  # 1- return country abbreviation if matched with country abbreviations
  # 2- return country abbreviation if matched with country name
  # 3- return USA if address contains a USA state
  # 4- otherwise return empty string

  country = countries_hash.keys.select { |country_abbreviation| @address.include?(country_abbreviation) }.first
  return country if country.present?
  country = countries_hash.select { |_, country_name| @address.downcase.include?(country_name.downcase) }.first&.first
  return country if country.present?

  return 'USA' if @state.present?

  return country.to_s
end

#extract_stateObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/base_scraper_service/location_service.rb', line 8

def extract_state
  # 1- return state abbreviation if matched with state abbreviations
  # 2- return state abbreviation if matched with state name
  # 3- otherwise return empty string

  @state = us_states_hash.keys.select { |state_abbreviation| @address.upcase.include?(state_abbreviation) }.first
  return @state if @state.present? && @address.length != 3
  @state = us_states_hash.select { |_, state| @address.include?(state) }.first&.first

  return @state.to_s

end