geonames_api 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -2
- data/lib/geonames_api.rb +5 -15
- data/lib/geonames_api/country.rb +6 -37
- data/lib/geonames_api/hash.rb +5 -0
- data/lib/geonames_api/object.rb +44 -0
- data/lib/geonames_api/version.rb +1 -1
- data/lib/geonames_api/weather.rb +10 -25
- metadata +4 -2
data/README.md
CHANGED
@@ -45,7 +45,7 @@ For a complete list of the config options, take a look at the `geonames_api.rb`
|
|
45
45
|
|
46
46
|
The geonames api uses the ISO code of the country as its natural key.
|
47
47
|
|
48
|
-
> GeoNamesAPI::Country.
|
48
|
+
> GeoNamesAPI::Country.find("US")
|
49
49
|
|
50
50
|
=> #<GeoNamesAPI::Country:0x007fd43503dfc0 @country_code="US", @country_name="United States", @currency_code="USD", @fips_code="US", @iso_numeric=840, @north=49.388611, @capital="Washington", @continent_name="North America", @area_in_sq_km=9629091.0, @languages="en-US,es-US,haw,fr", @iso_alpha3="USA", @continent="NA", @south=24.544245, @east=-66.954811, @geoname_id=6252001, @west=-124.733253, @population=310232863>
|
51
51
|
|
@@ -57,7 +57,7 @@ The geonames api uses the ISO code of the country as its natural key.
|
|
57
57
|
|
58
58
|
The geonames api uses the latitude and longitude of the place as the parameters for its weather service.
|
59
59
|
|
60
|
-
> GeoNamesAPI::Weather.
|
60
|
+
> GeoNamesAPI::Weather.find(41.88,-87.68)
|
61
61
|
|
62
62
|
=> #<GeoNamesAPI::Weather:0x007fab1c80dc10 @latitude=41.88, @longitude=-87.68, @weather_condition="n/a", @clouds="few clouds", @observation="KMDW 012051Z 28009KT 10SM FEW049 BKN070 11/M02 A2983 RMK AO2 SLP105 T01111022 55010", @wind_direction=280, @icao="KMDW", @sea_level_pressure=1010.5, @elevation=188, @country_code="US", @clouds_code="FEW", @lng=-87.75, @temperature=11.1, @dew_point="-2.2", @wind_speed=9, @humidity=39, @station_name="Chicago, Chicago Midway Airport", @datetime="2012-11-01 20:51:00", @lat=41.78333333333333>
|
63
63
|
|
data/lib/geonames_api.rb
CHANGED
@@ -2,16 +2,16 @@ require 'open-uri'
|
|
2
2
|
require 'json'
|
3
3
|
require 'active_support/all'
|
4
4
|
require "geonames_api/version"
|
5
|
+
require "geonames_api/hash"
|
6
|
+
require "geonames_api/object"
|
5
7
|
require "geonames_api/country"
|
6
8
|
require "geonames_api/weather"
|
7
9
|
|
8
10
|
module GeoNamesAPI
|
9
|
-
BASE_URL = "http://api.geonames.org/"
|
10
|
-
|
11
|
-
def self.to_url_params(params)
|
12
|
-
"?" + params.collect { |key, value| "#{key}=#{value}" }.join("&")
|
13
|
-
end
|
14
11
|
|
12
|
+
mattr_accessor :url
|
13
|
+
@@url = "http://api.geonames.org/"
|
14
|
+
|
15
15
|
mattr_accessor :formatted
|
16
16
|
@@formatted = true
|
17
17
|
|
@@ -28,15 +28,5 @@ module GeoNamesAPI
|
|
28
28
|
{ formatted: formatted, lang: lang, username: username, style: style }
|
29
29
|
end
|
30
30
|
|
31
|
-
def self.set_default_type(value)
|
32
|
-
case value
|
33
|
-
when /\A\d+\Z/
|
34
|
-
value.to_i
|
35
|
-
when /\A\d*\.\d*\Z/
|
36
|
-
value.to_f
|
37
|
-
else
|
38
|
-
value
|
39
|
-
end
|
40
|
-
end
|
41
31
|
|
42
32
|
end
|
data/lib/geonames_api/country.rb
CHANGED
@@ -1,47 +1,16 @@
|
|
1
1
|
module GeoNamesAPI
|
2
|
-
class Country
|
2
|
+
class Country < GeoNamesAPI::Object
|
3
3
|
|
4
4
|
METHOD = "countryInfoJSON"
|
5
|
+
ID = ["country"]
|
5
6
|
|
6
7
|
def self.all
|
7
|
-
|
8
|
-
new.parse(response)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
attr_accessor :country_code
|
13
|
-
|
14
|
-
def initialize(country_code=nil)
|
15
|
-
self.country_code = country_code
|
16
|
-
end
|
17
|
-
|
18
|
-
def find
|
19
|
-
parse(JSON.load(open(url).read)["geonames"].first)
|
20
|
-
self
|
21
|
-
end
|
22
|
-
|
23
|
-
def parse(response)
|
24
|
-
response.each do |key, value|
|
25
|
-
attr_name = key.underscore.to_sym
|
26
|
-
self.class.send(:attr_accessor, attr_name) unless respond_to? attr_name
|
27
|
-
value = GeoNamesAPI.set_default_type(value)
|
28
|
-
send("#{attr_name}=", value)
|
29
|
-
end
|
30
|
-
self
|
31
|
-
end
|
32
|
-
|
33
|
-
def params
|
34
|
-
GeoNamesAPI.params.merge({ country: country_code })
|
35
|
-
end
|
36
|
-
|
37
|
-
def url
|
38
|
-
self.class.url(params)
|
8
|
+
where.collect { |response| new(response) }
|
39
9
|
end
|
40
10
|
|
41
|
-
def self.
|
42
|
-
|
43
|
-
GeoNamesAPI::BASE_URL + METHOD + GeoNamesAPI.to_url_params(params)
|
11
|
+
def self.where(params={})
|
12
|
+
JSON.load(open(url(params)).read)["geonames"]
|
44
13
|
end
|
45
|
-
|
14
|
+
|
46
15
|
end
|
47
16
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module GeoNamesAPI
|
2
|
+
class Object
|
3
|
+
|
4
|
+
def self.find(*names)
|
5
|
+
params, n = {}, 0
|
6
|
+
self::ID.each { |i| params[i] = names[n]; n+= 1 }
|
7
|
+
new(where(params).first)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.url(params={})
|
11
|
+
GeoNamesAPI.url + self::METHOD + GeoNamesAPI.params.merge(params).to_url
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(response)
|
15
|
+
parse(response)
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_attribute(attribute)
|
19
|
+
attr_name = attribute.underscore.to_sym
|
20
|
+
self.class.send(:attr_accessor, attr_name) unless respond_to?(attr_name)
|
21
|
+
attr_name
|
22
|
+
end
|
23
|
+
|
24
|
+
def set_default_type(value)
|
25
|
+
case value
|
26
|
+
when /\A\d+\Z/
|
27
|
+
value.to_i
|
28
|
+
when /\A\d*\.\d*\Z/
|
29
|
+
value.to_f
|
30
|
+
else
|
31
|
+
value
|
32
|
+
end
|
33
|
+
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
|
+
|
43
|
+
end
|
44
|
+
end
|
data/lib/geonames_api/version.rb
CHANGED
data/lib/geonames_api/weather.rb
CHANGED
@@ -1,35 +1,20 @@
|
|
1
1
|
module GeoNamesAPI
|
2
|
-
class Weather
|
3
|
-
METHOD = "findNearByWeatherJSON"
|
2
|
+
class Weather < GeoNamesAPI::Object
|
4
3
|
|
5
|
-
|
4
|
+
METHOD = "findNearByWeatherJSON"
|
5
|
+
ID = ["lat", "lng"]
|
6
6
|
|
7
|
-
def
|
8
|
-
|
7
|
+
def self.where(params={})
|
8
|
+
JSON.load(open(url(params)).read)["weatherObservation"]
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
|
13
|
-
self
|
11
|
+
def self.url(params)
|
12
|
+
GeoNamesAPI.url + METHOD + GeoNamesAPI.params.merge(params).to_url
|
14
13
|
end
|
15
|
-
|
16
|
-
def
|
17
|
-
response
|
18
|
-
attr_name = key.underscore.to_sym
|
19
|
-
self.class.send(:attr_accessor, attr_name) unless respond_to? attr_name
|
20
|
-
value = GeoNamesAPI.set_default_type(value)
|
21
|
-
send("#{attr_name}=", value)
|
22
|
-
end
|
23
|
-
self
|
14
|
+
|
15
|
+
def initialize(response)
|
16
|
+
parse(response)
|
24
17
|
end
|
25
18
|
|
26
|
-
def params
|
27
|
-
GeoNamesAPI.params.merge({ lat: latitude, lng: longitude })
|
28
|
-
end
|
29
|
-
|
30
|
-
def url
|
31
|
-
GeoNamesAPI::BASE_URL + METHOD + GeoNamesAPI.to_url_params(params)
|
32
|
-
end
|
33
|
-
|
34
19
|
end
|
35
20
|
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.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -43,6 +43,8 @@ files:
|
|
43
43
|
- geonames_api.gemspec
|
44
44
|
- lib/geonames_api.rb
|
45
45
|
- lib/geonames_api/country.rb
|
46
|
+
- lib/geonames_api/hash.rb
|
47
|
+
- lib/geonames_api/object.rb
|
46
48
|
- lib/geonames_api/version.rb
|
47
49
|
- lib/geonames_api/weather.rb
|
48
50
|
homepage: https://github.com/buytruckload/geonames_api
|