open_places 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ec524fc7eed7c33638c7c6021d2f02a04e6288d
4
- data.tar.gz: 397e1c0c9fd649e3ae5ee1f2438afcb2adfc0f5c
3
+ metadata.gz: 7d93a62f2c504af60fb46b0765deae1a8502a128
4
+ data.tar.gz: 45d9b533fcd6d3f0ddc45dec78abf033a3233b36
5
5
  SHA512:
6
- metadata.gz: eda323608724ab9c9b470acffaf3c99e2699de8c8429a6d2e432ccea05044312cd8c2579a545faceff52027337d910e3a0916c4cea638579f4053448b15da509
7
- data.tar.gz: 66bdee81e7c2e4d3bbe481c36b5a5afb83bdfdafd8c9d31efd88d6fa23cf719f7fc2ab6180d4733ff8fd4f1983099381672a24208c1d5a6232d16919d74134f9
6
+ metadata.gz: dac0aaf4a8913923dd5593f59d2d5d11d9780e205e5a72c970989b4c43c3277420f3ee6e8e560458d398d0cb6b5dd0bb5f762734a92a5292e8845f75d0e82e58
7
+ data.tar.gz: 3b2ae299d52d015ca46c51eae5b3541eab48e857add24aec1783f33038f463c050ab494d238af4b4fd31f96e8881df1e752609d0a60cee7640675b2d5146ce2c
@@ -13,7 +13,18 @@ module OpenPlaces
13
13
  @results = @results.near(params[:near]) if params[:near].present?
14
14
  @results = @results.order('scalerank ASC')
15
15
  @results = @results.limit(params[:limit])
16
-
16
+
17
+ if @results.empty?
18
+ OpenPlaces::Geo.transaction do
19
+ @result = OpenPlaces::Geo.create(long_name: query_string)
20
+ if @result.id.present?
21
+ @results = OpenPlaces::Geo.where(id: @result.id)
22
+ else
23
+ @results = OpenPlaces::Geo.where(slug: @result.slug)
24
+ end
25
+ end
26
+ end
27
+
17
28
  respond_to do |format|
18
29
  format.json do
19
30
  render json: @results.to_json(except: OpenPlaces::Geo.GEO_FIELDS), root: false, status: 200, :callback => params['callback']
@@ -5,7 +5,34 @@ module OpenPlaces
5
5
  self.primary_key = :id
6
6
  self.inheritance_column = 'geotype'
7
7
 
8
- include Concerns::ReadOnlyModel
8
+ validates :name, presence: { scope: [:latitude, :longitude]}
9
+ validates :long_name, :slug, :path, presence: true, uniqueness: true
10
+ validates :name, presence: true, uniqueness: { scope: [:province_name, :country_name ]}
11
+
12
+ before_validation :geocode_record
13
+ before_validation :validate_country_name
14
+ before_validation :validate_province_name
15
+ before_validation :set_postgres_columns
16
+
17
+ geocoded_by :long_name do |obj,results|
18
+ if geo = results.first
19
+ parse_geocode_results(obj, geo)
20
+ end
21
+ end
22
+
23
+ reverse_geocoded_by :latitude, :longitude do |obj,results|
24
+ if geo = results.first
25
+ parse_geocode_results(obj, geo, false)
26
+ end
27
+ end
28
+
29
+ def geocode_record
30
+ if latitude_changed? && longitude_changed?
31
+ reverse_geocode
32
+ elsif long_name_changed?
33
+ geocode
34
+ end
35
+ end
9
36
 
10
37
  def self.FIELDS
11
38
  @fields ||= ["id","geotype", "subtype", "scalerank", "code", "name", "short_name", "long_name",
@@ -60,5 +87,76 @@ module OpenPlaces
60
87
  ) As fc;)
61
88
  )
62
89
  }
90
+
91
+ protected
92
+
93
+ def self.parse_geocode_results(obj, geo, latlng=true)
94
+ ['latitude', 'longitude'].each do |k|
95
+ obj[k] = geo.send(k) if geo.respond_to?(k)
96
+ end if !!(latlng)
97
+ obj['country_name'] = geo.send('country')
98
+ obj['country_code'] = geo.send('country_code')
99
+ obj['province_name'] = geo.send('state')
100
+ obj['province_code'] = geo.send('state_code')
101
+ city_components = [
102
+ geo.address_components_of_type('point_of_interest'),
103
+ geo.address_components_of_type('establishment'),
104
+ geo.address_components_of_type('locality'),
105
+ geo.address_components_of_type('political')
106
+ ]
107
+ city = city_components.try(:flatten).try(:first)["long_name"] rescue nil
108
+ obj['name'] = city || geo.send('city') || obj['province_name'] || obj['country_name']
109
+ obj['short_name'] = obj['country_code'] == 'US' ? [obj['name'],obj['province_name']].join(", ") : [obj['name'],obj['country_name']].join(", ")
110
+ obj['scalerank'] ||= 12
111
+ obj['geotype'] ||= 'OpenPlaces::Place'
112
+ obj['subtype'] ||= 'Town'
113
+ return obj
114
+ end
115
+
116
+ def validate_country_name
117
+ country = Country.autocomplete(country_name)
118
+ if country.empty?
119
+ raise "Country #{country_name} not found"
120
+ else
121
+ country = country.first
122
+ self.country_name = country.name
123
+ self.country_code = country.code
124
+ self.continent = country.continent
125
+ self.region_un = country.region_un
126
+ self.subregion = country.subregion
127
+ self.region_wb = country.region_wb
128
+ end
129
+ end
130
+
131
+ def validate_province_name
132
+ province = Province.autocomplete(province_name)
133
+ if province.empty?
134
+ raise "Province #{province_name} not found"
135
+ else
136
+ province = province.first
137
+ self.province_name = province.name
138
+ self.province_code = province.code
139
+ end
140
+ end
141
+
142
+ def set_postgres_columns
143
+ results = ActiveRecord::Base.connection.execute(%(
144
+ SELECT ST_SetSRID(ST_Point(#{latitude}, #{longitude}),4326) AS latlng,
145
+ ST_Expand(ST_SetSRID(ST_Point(#{longitude}, #{latitude}),4326), .1) AS geom,
146
+ ST_Envelope(ST_Expand(ST_SetSRID(ST_Point(#{longitude}, #{latitude}),4326), .1)) AS bbox,
147
+ ST_XMin(ST_Expand(ST_SetSRID(ST_Point(#{longitude}, #{latitude}),4326), .1)) AS x_min,
148
+ ST_YMin(ST_Expand(ST_SetSRID(ST_Point(#{longitude}, #{latitude}),4326), .1)) AS y_min,
149
+ ST_XMax(ST_Expand(ST_SetSRID(ST_Point(#{longitude}, #{latitude}),4326), .1)) AS x_max,
150
+ ST_YMax(ST_Expand(ST_SetSRID(ST_Point(#{longitude}, #{latitude}),4326), .1)) AS y_max,
151
+ ST_Area(ST_Expand(ST_SetSRID(ST_Point(#{longitude}, #{latitude}),4326), .1)) AS area,
152
+ concat_ws(' ', concat_ws(', ', '#{name}', '#{province_name}'), '#{country_name}') AS long_name,
153
+ setweight(to_tsvector('#{name}'), 'A') || setweight(to_tsvector('#{province_name}'), 'B') || setweight(to_tsvector('#{country_name}'), 'C') AS tsvector,
154
+ regexp_replace(trim(regexp_replace(lower(concat_ws('-', '#{name}', '#{province_name}', '#{country_name}')), '[^0-9a-z/]+', ' ', 'g')), '[ ]+', '-', 'g') AS slug,
155
+ regexp_replace(trim(regexp_replace(lower(concat_ws('/', '#{country_name}', '#{province_name}', '#{name}')), '[^0-9a-z/]+', ' ', 'g')), '[ ]+', '-', 'g') AS path
156
+ ))
157
+ results.first.each do |k,v|
158
+ self.send("#{k}=", v)
159
+ end if results.try(:first)
160
+ end
63
161
  end
64
162
  end
@@ -29,10 +29,11 @@
29
29
 
30
30
  <% content_for(:javascript) do %>
31
31
  $(document).ready(function(){
32
-
33
- L.mapbox.accessToken = '<%= ENV['MAPBOX_ID']%>';
34
- window["map"] = L.mapbox.map('map', 'mapbox.streets')
32
+ <% if ENV['MAPBOX_ID'].present? %>
33
+ L.mapbox.accessToken = '<%= ENV['MAPBOX_ID']%>';
34
+ window["map"] = L.mapbox.map('map', 'mapbox.streets')
35
35
  .setView([0,0], 2);
36
+ <% end %>
36
37
 
37
38
  $( "#location" ).autocomplete({
38
39
  source: function( request, response ) {
@@ -1,3 +1,3 @@
1
1
  module OpenPlaces
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_places
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ty Rauber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-17 00:00:00.000000000 Z
11
+ date: 2016-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: geocoder
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: Imports Natural Earth geospatial data and provides a mountable JSON/GeoJSON
70
84
  API endpoint.
71
85
  email: