Class: Mei::Geonames

Inherits:
Object
  • Object
show all
Includes:
WebServiceBase
Defined in:
lib/mei/geonames.rb

Instance Attribute Summary

Attributes included from WebServiceBase

#raw_response

Instance Method Summary collapse

Methods included from WebServiceBase

#get_json, #get_xml

Constructor Details

#initialize(e) ⇒ Geonames

Returns a new instance of Geonames.



12
13
14
# File 'lib/mei/geonames.rb', line 12

def initialize(e)
  @type = e
end

Instance Method Details

#broader(row) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/mei/geonames.rb', line 61

def broader(row)
  broader_list = []
  if row["fcl"] == "P" || row["fcl"] == "S" || row["fcl"] == "A"
    broader_list << {:uri_link=>"http://www.geonames.org/#{row["adminId1"]}", :label=>row["adminName1"]} if row["adminId1"].present? && row["adminName1"].present?
    broader_list << {:uri_link=>"http://www.geonames.org/#{row["adminId2"]}", :label=>row["adminName2"]} if row["adminId2"].present? && row["adminName2"].present?
    broader_list << {:uri_link=>"http://www.geonames.org/#{row["countryId"]}", :label=>row["countryName"]} if row["countryId"].present? && row["countryName"].present?
  end

  return broader_list
end

#build_query_url(q) ⇒ Object



22
23
24
25
26
# File 'lib/mei/geonames.rb', line 22

def build_query_url q
  @escaped_query = URI.escape(q)
  @escaped_query = q
  return "http://ws.geonames.org/searchJSON"
end

#narrower(row) ⇒ Object



72
73
74
# File 'lib/mei/geonames.rb', line 72

def narrower(row)
  return []
end

#parse_authority_responseObject

result.first result.first bbox adminId1 adminId2



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mei/geonames.rb', line 33

def parse_authority_response
  end_response = []

  @raw_response["geonames"].each do |geoname|

    count = ActiveFedora::Base.find_with_conditions("based_near_ssim:#{solr_clean("http://www.geonames.org/#{geoname["geonameId"]}")}", rows: '100', fl: 'id' ).length

    if count >= 99
      count = "99+"
    else
      count = count.to_s
    end

    end_response << {
        "uri_link" => "http://www.geonames.org/#{geoname["geonameId"]}" || "missing!",
        "label" => geoname["name"],
        "broader" => broader(geoname),
        "narrower" => narrower(geoname),
        "variants" => variants(geoname),
        "count" => count
    }
  end

  end_response
end

#request_optionsObject



8
9
10
# File 'lib/mei/geonames.rb', line 8

def request_options
  {:params => {:featureClass => "#{@type}", :style => 'full', :maxRows => 20, :name_startsWith => "#{@escaped_query}", :username=>"boston_library"}, accept: :json}
end

#search(q) ⇒ Object



16
17
18
19
# File 'lib/mei/geonames.rb', line 16

def search q
  @raw_response = get_json(build_query_url(q))
  parse_authority_response
end

#solr_clean(term) ⇒ Object



90
91
92
# File 'lib/mei/geonames.rb', line 90

def solr_clean(term)
  return term.gsub('\\', '\\\\').gsub(':', '\\:').gsub(' ', '\ ')
end

#variants(row) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mei/geonames.rb', line 76

def variants(row)
  varient_list = []
  if row["alternateNames"].present?
    row["alternateNames"].each do |variant|
      #if ['eng'].include?( variant["lang"])
      varient_list <<  variant["name"]
      #end
    end
  end

 return varient_list
end