geonames_api 0.0.1
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +1 -0
- data/geonames_api.gemspec +19 -0
- data/lib/geonames_api.rb +42 -0
- data/lib/geonames_api/country.rb +47 -0
- data/lib/geonames_api/version.rb +3 -0
- data/lib/geonames_api/weather.rb +35 -0
- metadata +76 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Sean Devine
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# GeoNames API
|
2
|
+
|
3
|
+
This is a lightweight client for the [GeoNames](http://www.geonames.org) API. Huge thanks to them for such a great service!
|
4
|
+
|
5
|
+
There are many GeoNames API clients. BUT, most are rewritten versions of a Java API whose interface is a little funny =|
|
6
|
+
|
7
|
+
This is a simplified ruby implementation that does not implement the entire API. But, its lightweight and has a nice interface and will be easy to extend :)
|
8
|
+
|
9
|
+
The gem was written by [@barelyknown](http://twitter.com/barelyknown).
|
10
|
+
|
11
|
+
## Getting Started
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'geonames_api'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install geonames_api
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### FYI
|
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.
|
30
|
+
|
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
|
+
|
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
|
+
|
35
|
+
### Configuration
|
36
|
+
|
37
|
+
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.
|
38
|
+
|
39
|
+
> GeoNamesAPI.username = "yourusername@yourdomain.com"
|
40
|
+
> GeoNamesAPI.lang = :en
|
41
|
+
|
42
|
+
For a complete list of the config options, take a look at the `geonames_api.rb` file.
|
43
|
+
|
44
|
+
### Country
|
45
|
+
|
46
|
+
The geonames api uses the ISO code of the country as its natural key.
|
47
|
+
|
48
|
+
> GeoNamesAPI::Country.new("US").find
|
49
|
+
|
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
|
+
|
52
|
+
> GeoNamesAPI::Country.all
|
53
|
+
|
54
|
+
=> ... array of GeoNamesAPI::Country instances
|
55
|
+
|
56
|
+
### Weather
|
57
|
+
|
58
|
+
The geonames api uses the latitude and longitude of the place as the parameters for its weather service.
|
59
|
+
|
60
|
+
> GeoNamesAPI::Weather.new(41.88,-87.68).find
|
61
|
+
|
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
|
+
|
64
|
+
### Others
|
65
|
+
|
66
|
+
The other services will be implemented as needed. In the mean time, feel free to contribute! Let's keep the style consistent.
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
1. Fork it
|
71
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
72
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
73
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
74
|
+
5. Create new Pull Request
|
75
|
+
6. Thanks <3
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'geonames_api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "geonames_api"
|
8
|
+
gem.version = GeoNamesAPI::VERSION
|
9
|
+
gem.authors = ["Sean Devine"]
|
10
|
+
gem.email = ["barelyknown@icloud.com"]
|
11
|
+
gem.description = %q{Simple ruby client for the GeoNames API to get free and easy geographic info.}
|
12
|
+
gem.summary = %q{This is a lightweight client for the GeoNames API. Huge thanks to them for such a great service! There are many GeoNames API clients. BUT, most are rewritten versions of a Java API whose interface is a little funny =| This is a simplified ruby implementation that does not implement the entire API. But, its lightweight and has a nice interface and will be easy to extend :)}
|
13
|
+
gem.homepage = "https://github.com/buytruckload/geonames_api"
|
14
|
+
gem.add_runtime_dependency "activesupport"
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/geonames_api.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
require 'active_support/all'
|
4
|
+
require "geonames_api/version"
|
5
|
+
require "geonames_api/country"
|
6
|
+
require "geonames_api/weather"
|
7
|
+
|
8
|
+
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
|
+
|
15
|
+
mattr_accessor :formatted
|
16
|
+
@@formatted = true
|
17
|
+
|
18
|
+
mattr_accessor :lang
|
19
|
+
@@lang = :en
|
20
|
+
|
21
|
+
mattr_accessor :username
|
22
|
+
@@username = "demo"
|
23
|
+
|
24
|
+
mattr_accessor :style
|
25
|
+
@@style = :full
|
26
|
+
|
27
|
+
def self.params
|
28
|
+
{ formatted: formatted, lang: lang, username: username, style: style }
|
29
|
+
end
|
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
|
+
|
42
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module GeoNamesAPI
|
2
|
+
class Country
|
3
|
+
|
4
|
+
METHOD = "countryInfoJSON"
|
5
|
+
|
6
|
+
def self.all
|
7
|
+
JSON.load(open(url).read)["geonames"].collect do |response|
|
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)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.url(params={})
|
42
|
+
params = GeoNamesAPI.params.merge(params)
|
43
|
+
GeoNamesAPI::BASE_URL + METHOD + GeoNamesAPI.to_url_params(params)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module GeoNamesAPI
|
2
|
+
class Weather
|
3
|
+
METHOD = "findNearByWeatherJSON"
|
4
|
+
|
5
|
+
attr_accessor :latitude, :longitude
|
6
|
+
|
7
|
+
def initialize(latitude, longitude)
|
8
|
+
self.latitude, self.longitude = latitude, longitude
|
9
|
+
end
|
10
|
+
|
11
|
+
def find
|
12
|
+
parse(JSON.load(open(url).read)["weatherObservation"])
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse(response)
|
17
|
+
response.each do |key, value|
|
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
|
24
|
+
end
|
25
|
+
|
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
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geonames_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sean Devine
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Simple ruby client for the GeoNames API to get free and easy geographic
|
31
|
+
info.
|
32
|
+
email:
|
33
|
+
- barelyknown@icloud.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- geonames_api.gemspec
|
44
|
+
- lib/geonames_api.rb
|
45
|
+
- lib/geonames_api/country.rb
|
46
|
+
- lib/geonames_api/version.rb
|
47
|
+
- lib/geonames_api/weather.rb
|
48
|
+
homepage: https://github.com/buytruckload/geonames_api
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.24
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: This is a lightweight client for the GeoNames API. Huge thanks to them for
|
72
|
+
such a great service! There are many GeoNames API clients. BUT, most are rewritten
|
73
|
+
versions of a Java API whose interface is a little funny =| This is a simplified
|
74
|
+
ruby implementation that does not implement the entire API. But, its lightweight
|
75
|
+
and has a nice interface and will be easy to extend :)
|
76
|
+
test_files: []
|