geonames_api 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +112 -5
- data/lib/geonames_api.rb +4 -2
- data/lib/geonames_api/country.rb +1 -9
- data/lib/geonames_api/error.rb +5 -0
- data/lib/geonames_api/object.rb +42 -14
- data/lib/geonames_api/time_zone.rb +31 -0
- data/lib/geonames_api/version.rb +2 -2
- data/lib/geonames_api/weather.rb +43 -6
- data/lib/geonames_api/wikipedia.rb +8 -0
- metadata +4 -1
data/README.md
CHANGED
@@ -26,12 +26,14 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
### FYI
|
28
28
|
|
29
|
-
GeoNamesAPI uses conventions similar to Rails' ActiveRecord. If you add any features, please stick to `find` and `all` as the core query methods.
|
29
|
+
GeoNamesAPI uses conventions similar (but not exactly the same) to Rails' ActiveRecord. If you add any features, please stick to `find` and `all` as the core query methods.
|
30
30
|
|
31
31
|
The data is converted into the type implied by the content by default. If that isn't sufficient, let's create something more explicit for the exceptions.
|
32
32
|
|
33
33
|
The GeoNamesAPI classes include getters and setters that are the GeoNames API field names converted dynamically to more rubyish names using Rails' ActiveSupport `underscore` method. If the API ever changes, the attributes of the classes will change.
|
34
34
|
|
35
|
+
There are some extra methods in most of the key classes. Documentation is included below.
|
36
|
+
|
35
37
|
### Configuration
|
36
38
|
|
37
39
|
The gem is setup to use the `demo` user account by default. You'll need to provide your own credentials for anything but a simple set of test searches.
|
@@ -47,19 +49,124 @@ The geonames api uses the ISO code of the country as its natural key.
|
|
47
49
|
|
48
50
|
> GeoNamesAPI::Country.find("US")
|
49
51
|
|
50
|
-
=> #<GeoNamesAPI::Country:0x007fd43503dfc0
|
52
|
+
=> #<GeoNamesAPI::Country:0x007fd43503dfc0
|
53
|
+
@country_code="US",
|
54
|
+
@country_name="United States",
|
55
|
+
@currency_code="USD",
|
56
|
+
@fips_code="US",
|
57
|
+
@iso_numeric=840,
|
58
|
+
@north=49.388611,
|
59
|
+
@capital="Washington",
|
60
|
+
@continent_name="North America",
|
61
|
+
@area_in_sq_km=9629091.0,
|
62
|
+
@languages="en-US,es-US,haw,fr",
|
63
|
+
@iso_alpha3="USA",
|
64
|
+
@continent="NA",
|
65
|
+
@south=24.544245,
|
66
|
+
@east=-66.954811,
|
67
|
+
@geoname_id=6252001,
|
68
|
+
@west=-124.733253,
|
69
|
+
@population=310232863
|
70
|
+
>
|
51
71
|
|
52
72
|
> GeoNamesAPI::Country.all
|
53
73
|
|
54
74
|
=> ... array of GeoNamesAPI::Country instances
|
75
|
+
|
76
|
+
There are no additional methods offered for countries.
|
55
77
|
|
56
78
|
### Weather
|
57
79
|
|
58
|
-
The
|
80
|
+
The GeoNamesAPI uses the latitude and longitude of the place as the parameters for its weather service.
|
59
81
|
|
60
|
-
> GeoNamesAPI::Weather.find(41.88,-87.68)
|
82
|
+
> weather = GeoNamesAPI::Weather.find(41.88,-87.68)
|
83
|
+
|
84
|
+
=> #<GeoNamesAPI::Weather:0x007fab1c80dc10
|
85
|
+
@latitude=41.88,
|
86
|
+
@longitude=-87.68,
|
87
|
+
@weather_condition="n/a",
|
88
|
+
@clouds="few clouds",
|
89
|
+
@observation="KMDW 012051Z 28009KT 10SM FEW049 BKN070 11/M02 A2983 RMK AO2 SLP105 T01111022 55010",
|
90
|
+
@wind_direction=280,
|
91
|
+
@icao="KMDW",
|
92
|
+
@sea_level_pressure=1010.5,
|
93
|
+
@elevation=188,
|
94
|
+
@country_code="US",
|
95
|
+
@clouds_code="FEW",
|
96
|
+
@lng=-87.75,
|
97
|
+
@temperature=11.1,
|
98
|
+
@dew_point=-2.2,
|
99
|
+
@wind_speed=9,
|
100
|
+
@humidity=39,
|
101
|
+
@station_name="Chicago, Chicago Midway Airport",
|
102
|
+
@datetime="2012-11-01 20:51:00",
|
103
|
+
@lat=41.78333333333333
|
104
|
+
>
|
105
|
+
|
106
|
+
The following instance methods are also available:
|
107
|
+
|
108
|
+
> weather.geo_names_api_country
|
109
|
+
> weather.geo_names_api_time_zone
|
110
|
+
> weather.time_zone
|
111
|
+
> weather.at_local
|
112
|
+
> weather.at_utc
|
113
|
+
> weather.elevation_feet
|
114
|
+
> weather.elevation_meters
|
115
|
+
# fahrenheit and celcius versions of temps
|
116
|
+
> weather.temperature_f
|
117
|
+
> weather.temperature_c
|
118
|
+
> weather.dew_point_f
|
119
|
+
> weather.dew_point_c
|
120
|
+
|
121
|
+
### TimeZone
|
122
|
+
|
123
|
+
The GeoNamesAPI uses the latitude and longitude of the place as the parameters for its time zone service.
|
124
|
+
|
125
|
+
> time_zone = GeoNamesAPI::TimeZone.find(41.88,-87.68)
|
61
126
|
|
62
|
-
=> #<GeoNamesAPI::
|
127
|
+
=> #<GeoNamesAPI::TimeZone:0x007fd4b24d7e00
|
128
|
+
@time="2012-11-02 12:04",
|
129
|
+
@country_name="United States",
|
130
|
+
@sunset="2012-11-02 17:42",
|
131
|
+
@raw_offset=-6,
|
132
|
+
@dst_offset=-5,
|
133
|
+
@country_code="US",
|
134
|
+
@gmt_offset=-6,
|
135
|
+
@lng=-87.68,
|
136
|
+
@sunrise="2012-11-02 07:25",
|
137
|
+
@timezone_id="America/Chicago",
|
138
|
+
@lat=41.88
|
139
|
+
>
|
140
|
+
|
141
|
+
# ActiveSupport::TimeZone instance
|
142
|
+
> time_zone.time_zone
|
143
|
+
# Local and UTC version of each time
|
144
|
+
> time_zone.sunset_local
|
145
|
+
> time_zone.sunset_utc
|
146
|
+
> time_zone.sunrise_local
|
147
|
+
> time_zone.sunrise_utc
|
148
|
+
> time_zone.time_local
|
149
|
+
> time_zone.time_utc
|
150
|
+
|
151
|
+
### Wikipedia
|
152
|
+
|
153
|
+
The GeoNamesAPI uses the latitude and longitude of the place as the parameters for its Wikipedia artical service.
|
154
|
+
|
155
|
+
Please note the use of the `all` method with `GeoNamesAPI::Wikipedia`. It returns all of the articles that match. The `find` method will return the first matching article.
|
156
|
+
|
157
|
+
> articles = GeoNamesAPI::Wikipedia.all(41.88,-87.68)
|
158
|
+
=> [#<GeoNamesAPI::Wikipedia:0x007fd4b24e6040
|
159
|
+
@summary="Richard T. Crane Technical Preparatory High School, commonly known as Crane Tech Prep or Crane Tech High School, is a public high school in Chicago, Illinois. It is located at 2245 West Jackson Boulevard in Chicago's Near West Side community area. (...)",
|
160
|
+
@distance=0.4108,
|
161
|
+
@rank=82,
|
162
|
+
@title="Crane High School (Chicago, Illinois)",
|
163
|
+
@wikipedia_url="en.wikipedia.org/wiki/Crane_High_School_%28Chicago%2C_Illinois%29",
|
164
|
+
@elevation=183,
|
165
|
+
@country_code="US",
|
166
|
+
@lng=-87.6827,
|
167
|
+
@lang="en",
|
168
|
+
@lat=41.8769>,
|
169
|
+
...]
|
63
170
|
|
64
171
|
### Others
|
65
172
|
|
data/lib/geonames_api.rb
CHANGED
@@ -3,9 +3,12 @@ require 'json'
|
|
3
3
|
require 'active_support/all'
|
4
4
|
require "geonames_api/version"
|
5
5
|
require "geonames_api/hash"
|
6
|
+
require "geonames_api/error"
|
6
7
|
require "geonames_api/object"
|
7
8
|
require "geonames_api/country"
|
8
9
|
require "geonames_api/weather"
|
10
|
+
require "geonames_api/time_zone"
|
11
|
+
require "geonames_api/wikipedia"
|
9
12
|
|
10
13
|
module GeoNamesAPI
|
11
14
|
|
@@ -27,6 +30,5 @@ module GeoNamesAPI
|
|
27
30
|
def self.params
|
28
31
|
{ formatted: formatted, lang: lang, username: username, style: style }
|
29
32
|
end
|
30
|
-
|
31
|
-
|
33
|
+
|
32
34
|
end
|
data/lib/geonames_api/country.rb
CHANGED
@@ -2,15 +2,7 @@ module GeoNamesAPI
|
|
2
2
|
class Country < GeoNamesAPI::Object
|
3
3
|
|
4
4
|
METHOD = "countryInfoJSON"
|
5
|
-
ID =
|
6
|
-
|
7
|
-
def self.all
|
8
|
-
where.collect { |response| new(response) }
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.where(params={})
|
12
|
-
JSON.load(open(url(params)).read)["geonames"]
|
13
|
-
end
|
5
|
+
ID = "country"
|
14
6
|
|
15
7
|
end
|
16
8
|
end
|
data/lib/geonames_api/object.rb
CHANGED
@@ -1,20 +1,56 @@
|
|
1
1
|
module GeoNamesAPI
|
2
2
|
class Object
|
3
|
+
|
4
|
+
NESTED = true
|
3
5
|
|
4
6
|
def self.find(*names)
|
5
|
-
|
6
|
-
|
7
|
-
|
7
|
+
if response = where(name_params(names)).first
|
8
|
+
new(response)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.all(*names)
|
13
|
+
where(name_params(names)).collect { |response| new(response) if response }.compact
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.where(params={})
|
17
|
+
response = JSON.load(open(url(params)).read)
|
18
|
+
case response.keys.first
|
19
|
+
when /\Astatus\Z/
|
20
|
+
message = response["status"]["message"]
|
21
|
+
raise GeoNamesAPI::Error, message[0,1].capitalize + message[1,message.length - 1]
|
22
|
+
end
|
23
|
+
if self::NESTED
|
24
|
+
[response.values].flatten
|
25
|
+
else
|
26
|
+
[response]
|
27
|
+
end
|
8
28
|
end
|
9
29
|
|
10
30
|
def self.url(params={})
|
11
31
|
GeoNamesAPI.url + self::METHOD + GeoNamesAPI.params.merge(params).to_url
|
12
32
|
end
|
33
|
+
|
34
|
+
def self.name_params(names)
|
35
|
+
params, n = {}, 0
|
36
|
+
if names.any?
|
37
|
+
[self::ID].flatten.each { |i| params[i] = names[n]; n+= 1 }
|
38
|
+
end
|
39
|
+
params
|
40
|
+
end
|
13
41
|
|
14
42
|
def initialize(response)
|
15
43
|
parse(response)
|
16
44
|
end
|
17
45
|
|
46
|
+
def parse(response)
|
47
|
+
response.each do |key, value|
|
48
|
+
attr_name = create_attribute(key)
|
49
|
+
value = set_default_type(value)
|
50
|
+
send("#{attr_name}=", value)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
18
54
|
def create_attribute(attribute)
|
19
55
|
attr_name = attribute.underscore.to_sym
|
20
56
|
self.class.send(:attr_accessor, attr_name) unless respond_to?(attr_name)
|
@@ -23,22 +59,14 @@ module GeoNamesAPI
|
|
23
59
|
|
24
60
|
def set_default_type(value)
|
25
61
|
case value
|
26
|
-
when /\A
|
62
|
+
when /\A-?\d+\Z/
|
27
63
|
value.to_i
|
28
|
-
when /\A
|
64
|
+
when /\A-?\d*\.\d*\Z/
|
29
65
|
value.to_f
|
30
66
|
else
|
31
67
|
value
|
32
68
|
end
|
33
69
|
end
|
34
|
-
|
35
|
-
def parse(response)
|
36
|
-
response.each do |key, value|
|
37
|
-
attr_name = create_attribute(key)
|
38
|
-
value = set_default_type(value)
|
39
|
-
send("#{attr_name}=", value)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
70
|
+
|
43
71
|
end
|
44
72
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module GeoNamesAPI
|
2
|
+
class TimeZone < GeoNamesAPI::Object
|
3
|
+
|
4
|
+
METHOD = "timezoneJSON"
|
5
|
+
ID = ["lat", "lng"]
|
6
|
+
NESTED = false
|
7
|
+
|
8
|
+
def time_zone
|
9
|
+
ActiveSupport::TimeZone.new(timezone_id)
|
10
|
+
end
|
11
|
+
|
12
|
+
def local(time_type)
|
13
|
+
t = DateTime.parse(send(time_type))
|
14
|
+
time_zone.local(t.year, t.month, t.day, t.hour, t.minute)
|
15
|
+
end
|
16
|
+
|
17
|
+
def utc(time_type)
|
18
|
+
local(time_type).utc
|
19
|
+
end
|
20
|
+
|
21
|
+
def method_missing(method, *args)
|
22
|
+
case method
|
23
|
+
# Provides the local and utc time variant of each time
|
24
|
+
# Examples: time_zone.sunrise_local, time_zone.time_utc
|
25
|
+
when /\A(.*)_(local|utc)\Z/
|
26
|
+
send($2, $1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/lib/geonames_api/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module GeoNamesAPI
|
2
|
-
VERSION = "0.0.
|
3
|
-
end
|
2
|
+
VERSION = "0.0.3"
|
3
|
+
end
|
data/lib/geonames_api/weather.rb
CHANGED
@@ -4,16 +4,53 @@ module GeoNamesAPI
|
|
4
4
|
METHOD = "findNearByWeatherJSON"
|
5
5
|
ID = ["lat", "lng"]
|
6
6
|
|
7
|
-
def
|
8
|
-
|
7
|
+
def geo_names_api_country
|
8
|
+
@geo_names_api_country ||= GeoNamesAPI::Country.find(country_code)
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
|
11
|
+
def geo_names_api_time_zone
|
12
|
+
@geo_names_api_time_zone ||= GeoNamesAPI::TimeZone.find(lat, lng)
|
13
|
+
end
|
14
|
+
|
15
|
+
def time_zone
|
16
|
+
geo_names_api_time_zone.time_zone
|
17
|
+
end
|
18
|
+
|
19
|
+
def at_local
|
20
|
+
t = DateTime.parse(datetime)
|
21
|
+
time_zone.local(t.year, t.month, t.day, t.hour, t.minute)
|
22
|
+
end
|
23
|
+
|
24
|
+
def at_utc
|
25
|
+
at_local.utc
|
26
|
+
end
|
27
|
+
|
28
|
+
def elevation_feet
|
29
|
+
elevation * 3.28084
|
30
|
+
end
|
31
|
+
|
32
|
+
def elevation_meters
|
33
|
+
elevation
|
34
|
+
end
|
35
|
+
|
36
|
+
def convert_c_to_f(temp)
|
37
|
+
temp * 9.to_f / 5.to_f + 32
|
13
38
|
end
|
14
39
|
|
15
|
-
def
|
16
|
-
|
40
|
+
def temperature_f
|
41
|
+
convert_c_to_f(temperature)
|
42
|
+
end
|
43
|
+
|
44
|
+
def temperature_c
|
45
|
+
temperature
|
46
|
+
end
|
47
|
+
|
48
|
+
def dew_point_f
|
49
|
+
convert_c_to_f(dew_point)
|
50
|
+
end
|
51
|
+
|
52
|
+
def dew_point_c
|
53
|
+
dew_point
|
17
54
|
end
|
18
55
|
|
19
56
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geonames_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -43,10 +43,13 @@ files:
|
|
43
43
|
- geonames_api.gemspec
|
44
44
|
- lib/geonames_api.rb
|
45
45
|
- lib/geonames_api/country.rb
|
46
|
+
- lib/geonames_api/error.rb
|
46
47
|
- lib/geonames_api/hash.rb
|
47
48
|
- lib/geonames_api/object.rb
|
49
|
+
- lib/geonames_api/time_zone.rb
|
48
50
|
- lib/geonames_api/version.rb
|
49
51
|
- lib/geonames_api/weather.rb
|
52
|
+
- lib/geonames_api/wikipedia.rb
|
50
53
|
homepage: https://github.com/buytruckload/geonames_api
|
51
54
|
licenses: []
|
52
55
|
post_install_message:
|