Skip to content

Commit

Permalink
Fix tgn and countries issues (#25)
Browse files Browse the repository at this point in the history
* Refactor TGN-related methods for changes to Getty APIs
  (https://groups.google.com/g/gettyvocablod/c/GCXMd5pubY4/m/wrtavcuWAgAJ)

* Fixed #24

* Update countries gem (fixes #23)

* Rubocop cleanup, bin/console entry, #frozen string literals

* Added a comments for tests that could not be fixed
  • Loading branch information
bbarberBPL authored Aug 22, 2022
1 parent e86f2f0 commit 460162d
Show file tree
Hide file tree
Showing 18 changed files with 495 additions and 459 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Icon?
ehthumbs.db
.idea # Rubymine config files
/.idea/*
.ruby-version
Gemfile.lock

# Directories to ignore #
Expand Down
8 changes: 7 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
source "https://rubygems.org"
# frozen_string_literal: true

source 'https://rubygems.org'

# Declare your gem's dependencies in bplgeo.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
Expand All @@ -13,3 +15,7 @@ gemspec

# To use debugger
# gem 'debugger'

group :development, :test do
gem 'pry'
end
3 changes: 0 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end




Bundler::GemHelper.install_tasks

require 'rake/testtask'
Expand Down
9 changes: 9 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby

# frozen_string_literal: true

require 'bundler/setup'
require 'pry'
require 'geomash'

Pry.start
35 changes: 20 additions & 15 deletions geomash.gemspec
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
# frozen_string_literal: true

$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "geomash/version"
require 'geomash/version'

# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "geomash"
s.name = 'geomash'
s.version = Geomash::VERSION
s.authors = ["Boston Public Library"]
s.email = ["[email protected]"]
s.homepage = "http://www.bpl.org"
s.summary = "Parse string for potential geographic matches and return that data along with the TGN ID and Geonames ID."
s.description = "Parse string for potential geographic matches and return that data along with the TGN ID and Geonames ID."
s.authors = ['Boston Public Library']
s.email = ['[email protected]', '[email protected]', '[email protected]']
s.homepage = 'http://www.bpl.org'
s.summary = 'Parse string for potential geographic matches and return that data along with the TGN ID and Geonames ID.'
s.description = 'Parse string for potential geographic matches and return that data along with the TGN ID and Geonames ID.'

s.files = Dir["{app,config,db,lib}/**/*", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]
s.files = Dir['{app,config,db,lib}/**/*', 'Rakefile', 'README.rdoc']
s.test_files = Dir['test/**/*']
s.required_ruby_version = '>= 2.5'

s.add_dependency "activesupport"
s.add_dependency "countries", '>=2.0.0'
s.add_dependency "geocoder"
s.add_dependency 'activesupport', '>= 5.0'
s.add_dependency 'countries', '>= 5.0.0'
s.add_dependency 'geocoder'
s.add_dependency 'stringex'
s.add_dependency 'typhoeus'
s.add_dependency 'typhoeus', '>= 1.4'
s.add_dependency 'nokogiri'
s.add_dependency 'htmlentities'
s.add_dependency 'sparql'
s.add_development_dependency "sqlite3"
s.add_development_dependency "rails"
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rails', '>= 5.0', '< 7.0'
s.add_development_dependency 'bundler'
s.add_development_dependency 'rake'
end
56 changes: 27 additions & 29 deletions lib/geomash.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
# frozen_string_literal: true

module Geomash
require "geomash/constants"
require "geomash/parser"
require "geomash/standardizer"
require "geomash/tgn"
require "geomash/geonames"
require "geomash/town_lookup"
require "geomash/autoexpire_cache_dalli"
require "geomash/autoexpire_cache_redis"
require "geocoder"
require "countries"
require "stringex"
require "typhoeus"
require "nokogiri"
require "htmlentities"
require "active_support/core_ext/string/filters"
require "active_support/hash_with_indifferent_access"
require 'geocoder'
require 'countries'
require 'stringex'
require 'typhoeus'
require 'nokogiri'
require 'htmlentities'
require 'active_support'
require 'active_support/core_ext/string/filters'
require 'active_support/core_ext/enumerable'
require 'active_support/core_ext/hash'
require 'geomash/constants'
require 'geomash/parser'
require 'geomash/standardizer'
require 'geomash/tgn'
require 'geomash/geonames'
require 'geomash/town_lookup'
require 'geomash/autoexpire_cache_dalli'
require 'geomash/autoexpire_cache_redis'

def self.config
@config ||= YAML::load(File.open(config_path))[env]
.with_indifferent_access
end

def self.app_root
return @app_root if @app_root
return @app_root if defined?(@app_root)

@app_root = Rails.root if defined?(Rails) and defined?(Rails.root)
@app_root ||= APP_ROOT if defined?(APP_ROOT)
@app_root ||= '.'
end

def self.env
return @env if @env
return @env if defined?(@env)
#The following commented line always returns "test" in a rails c production console. Unsure of how to fix this yet...
#@env = ENV["RAILS_ENV"] = "test" if ENV
@env ||= Rails.env if defined?(Rails) and defined?(Rails.root)
Expand All @@ -40,18 +45,14 @@ def self.config_path
File.join(app_root, 'config', 'geomash.yml')
end

def self.parse(term,parse_term=false)
def self.parse(term, parse_term = false)
return {} if term.blank?

return_hash = Geomash::Parser.parse_mapquest_api(term, parse_term)

if return_hash.blank?
return_hash = Geomash::Parser.parse_bing_api(term, parse_term)
end
return_hash = Geomash::Parser.parse_bing_api(term, parse_term) if return_hash.blank?

if return_hash.blank?
return_hash = Geomash::Parser.parse_google_api(term, parse_term)
end
return_hash = Geomash::Parser.parse_google_api(term, parse_term) if return_hash.blank?

if return_hash[:country_part].present?
#FIXME
Expand All @@ -65,13 +66,10 @@ def self.parse(term,parse_term=false)
elsif geo_hash_temp.present? && geo_hash_temp[:tgn][:parse_depth] > return_hash[:tgn][:parse_depth]
return_hash[:tgn] = geo_hash_temp[:tgn]
end

end

return_hash[:geonames] = Geomash::Geonames.geonames_id_from_geo_hash(return_hash)
end

return return_hash

return_hash
end
end
6 changes: 4 additions & 2 deletions lib/geomash/autoexpire_cache_dalli.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#Taken from: https://github.com/alexreisner/geocoder/blob/master/examples/autoexpire_cache_dalli.rb
# frozen_string_literal: true

# Taken from: https://github.com/alexreisner/geocoder/blob/master/examples/autoexpire_cache_dalli.rb
module Geomash
class AutoexpireCacheDalli
def initialize(store, ttl = 86400)
Expand Down Expand Up @@ -52,4 +54,4 @@ def key_cache_delete(key)
@store.replace(@keys, YAML::dump(tmp))
end
end
end
end
41 changes: 22 additions & 19 deletions lib/geomash/autoexpire_cache_redis.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
#Taken from: https://github.com/alexreisner/geocoder/blob/master/examples/autoexpire_cache_redis.rb
# frozen_string_literal: true

# Taken from: https://github.com/alexreisner/geocoder/blob/master/examples/autoexpire_cache_redis.rb

module Geomash
class AutoexpireCacheRedis
def initialize(store, ttl = 86400)
@store = store
@ttl = ttl
end
class AutoexpireCacheRedis
def initialize(store, ttl = 86400)
@store = store
@ttl = ttl
end

def [](url)
@store.[](url)
end
def [](url)
@store.[](url)
end

def []=(url, value)
@store.[]=(url, value)
@store.expire(url, @ttl)
end
def []=(url, value)
@store.[]=(url, value)
@store.expire(url, @ttl)
end

def keys
@store.keys
end
def keys
@store.keys
end

def del(url)
@store.del(url)
def del(url)
@store.del(url)
end
end
end
end
8 changes: 5 additions & 3 deletions lib/geomash/constants.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Geomash
class Constants
COUNTRY_TGN_LOOKUP = {}
Expand Down Expand Up @@ -69,7 +71,7 @@ class Constants
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming'
}
}.freeze

#Terms that drive geographic parsers mad...
#Possibly check using QA against LCSH Subject topics that aren't geographical...
Expand All @@ -87,7 +89,7 @@ class Constants
'county',
#Some date removal
/[\d]+th [cC]entury,/
]
].freeze


STATE_TOWN_TGN_IDS =
Expand Down Expand Up @@ -479,6 +481,6 @@ class Constants
{:location_name => "Worthington", :tgn_id => "2051048"},
{:location_name => "Wrentham", :tgn_id => "2051050"},
{:location_name => "Yarmouth", :tgn_id => "2051052"}
]}
]}.freeze
end
end
3 changes: 1 addition & 2 deletions lib/geomash/geonames.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Geomash
class Geonames

def self.geonames_username
Geomash.config[:geonames_username] || '<username>'
end
Expand Down Expand Up @@ -110,7 +109,7 @@ def self.geonames_id_from_geo_hash(geo_hash)
elsif geo_hash[:country_part] == 'The Bahamas'
country_code = 'BS'
else
country_lookup = ISO3166::Country.find_country_by_name(geo_hash[:country_part])
country_lookup = ISO3166::Country.find_country_by_any_name(geo_hash[:country_part])
if country_lookup.blank?
Rails.logger.warn("WARNING [Geomash]: Could not find Country in geonames for: #{geo_hash[:country_part]}")
country_code = ''
Expand Down
Loading

0 comments on commit 460162d

Please sign in to comment.