geoapi 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/LICENSE +21 -0
- data/README +57 -0
- data/geoapi.gemspec +27 -0
- data/lib/geoapi.rb +30 -0
- data/lib/geoapi/base.rb +40 -0
- data/lib/geoapi/entity.rb +49 -0
- data/lib/geoapi/version.rb +8 -0
- data/lib/geoapi/view.rb +4 -0
- metadata +82 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2009 Chris Bruce
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
= NOTE
|
2
|
+
|
3
|
+
This is currently in Alpha Version and isn't fully functional. Currently, you can only conduct an Entity search and get the raw_json back.
|
4
|
+
|
5
|
+
I am quickly developing this into a fully featured gem that supports the full MQL queries offered by GeoAPI.com, so expect frequent updates until a major point release is reached.
|
6
|
+
|
7
|
+
= GeoAPI
|
8
|
+
|
9
|
+
A Ruby wrapper for the GeoAPI.com APIs. This gem was almost entirely inspired by the various geoplanet gems.
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
|
13
|
+
=== Reverse Geocoding:
|
14
|
+
|
15
|
+
require 'geoapi'
|
16
|
+
GeoAPI.apikey = [Your App ID Here]
|
17
|
+
|
18
|
+
# Location
|
19
|
+
latitude = -27.000
|
20
|
+
longitude = -131.000
|
21
|
+
|
22
|
+
# Non Required Options
|
23
|
+
optional_parameters = {:radius => '500m', :type => 'POI', :include_parents => true, :limit => 5, :pretty => true}
|
24
|
+
|
25
|
+
# Lookup
|
26
|
+
entities = GeoAPI::Entity.search(latitude, longitude, optional_parameters)
|
27
|
+
|
28
|
+
entities.each do |i|
|
29
|
+
puts "guid: #{i.guid}\nname: #{i.name}\ntype: #{i.type}\nviews: #{i.views}\nuserviews: #{i.userviews}"
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
== REQUIREMENTS:
|
35
|
+
|
36
|
+
To use this library, you must have a valid GeoAPI.com API Key.
|
37
|
+
You can get one at http://api.geoapi.com
|
38
|
+
|
39
|
+
Additionally, geoapi has the following gem dependencies:
|
40
|
+
|
41
|
+
* rest-client >= 0.9
|
42
|
+
* json >= 1.1.3
|
43
|
+
|
44
|
+
Please note that if you have ActiveSupport::JSON defined (either by
|
45
|
+
manually having loaded it or when you use geoapi within a Rails
|
46
|
+
application) the json dependency will be ignored and geoapi uses
|
47
|
+
ActiveSupport::JSON instead.
|
48
|
+
|
49
|
+
== INSTALL:
|
50
|
+
|
51
|
+
This gem is hosted on Gemcutter. To install gemcutter:
|
52
|
+
gem install gemcutter
|
53
|
+
gem tumble
|
54
|
+
|
55
|
+
To install geoapi after gemcutter:
|
56
|
+
gem install geoapi
|
57
|
+
|
data/geoapi.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "geoapi"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
s.date = "2009-11-10"
|
5
|
+
s.summary = "A Ruby wrapper for the GeoAPI.com API."
|
6
|
+
s.email = "chrisabruce@gmail.com"
|
7
|
+
s.homepage = "http://github.com/chrisabruce/GeoAPI/"
|
8
|
+
s.description = "A Ruby wrapper for the GeoAPI.com API. See http://api.geoapi.com for more information about the API."
|
9
|
+
s.authors = ["Chris Bruce"]
|
10
|
+
|
11
|
+
s.files = [
|
12
|
+
"README",
|
13
|
+
"LICENSE",
|
14
|
+
"geoapi.gemspec",
|
15
|
+
"lib/geoapi.rb",
|
16
|
+
"lib/geoapi/base.rb",
|
17
|
+
"lib/geoapi/entity.rb",
|
18
|
+
"lib/geoapi/view.rb",
|
19
|
+
"lib/geoapi/version.rb"
|
20
|
+
]
|
21
|
+
|
22
|
+
s.add_dependency("rest-client", [">= 0.9"])
|
23
|
+
s.add_dependency("json", [">= 1.1.3"])
|
24
|
+
|
25
|
+
s.has_rdoc = false
|
26
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
27
|
+
end
|
data/lib/geoapi.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
%w{rubygems rest_client}.each { |x| require x }
|
2
|
+
|
3
|
+
if defined?(ActiveSupport::JSON)
|
4
|
+
JSON = ActiveSupport::JSON
|
5
|
+
module JSON
|
6
|
+
def self.parse(json)
|
7
|
+
decode(json)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
else
|
11
|
+
require 'json'
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'geoapi/version'
|
15
|
+
require 'geoapi/base'
|
16
|
+
require 'geoapi/entity'
|
17
|
+
require 'geoapi/view'
|
18
|
+
|
19
|
+
module GeoAPI
|
20
|
+
API_VERSION = "v1"
|
21
|
+
API_URL = "http://api.geoapi.com/#{API_VERSION}/"
|
22
|
+
|
23
|
+
class << self
|
24
|
+
attr_accessor :apikey
|
25
|
+
end
|
26
|
+
|
27
|
+
class BadRequest < StandardError; end
|
28
|
+
class NotFound < StandardError; end
|
29
|
+
class NotAcceptable < StandardError; end
|
30
|
+
end
|
data/lib/geoapi/base.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module GeoAPI
|
2
|
+
class Base
|
3
|
+
class << self
|
4
|
+
def build_url(resource_path, options = {})
|
5
|
+
|
6
|
+
options[:apikey] ||= GeoAPI.apikey
|
7
|
+
query_string = build_query_params(options)
|
8
|
+
|
9
|
+
"#{GeoAPI::API_URL}#{resource_path}#{query_string}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def get(url)
|
13
|
+
RestClient.get(url)
|
14
|
+
rescue RestClient::RequestFailed
|
15
|
+
raise BadRequest, "Parameter invalid"
|
16
|
+
rescue RestClient::ResourceNotFound
|
17
|
+
raise NotFound, "GUID invalid"
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
# Take options and build query string
|
23
|
+
def build_query_params(options)
|
24
|
+
query = {}
|
25
|
+
|
26
|
+
# Filter '_' and convert True/False
|
27
|
+
options.each_pair do |key, value|
|
28
|
+
new_key = key.to_s.gsub(/_/, '-')
|
29
|
+
new_val = case value
|
30
|
+
when TrueClass then 1
|
31
|
+
when FalseClass then 0
|
32
|
+
else value
|
33
|
+
end
|
34
|
+
query[new_key] = new_val
|
35
|
+
end
|
36
|
+
"?" + query.map{|k,v| "#{k}=#{v}"}.join('&')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module GeoAPI
|
2
|
+
class Entity < Base
|
3
|
+
attr_reader :guid, :name, :type, :geom, :url, :latitude, :longitude, :views, :userviews, :raw_json
|
4
|
+
|
5
|
+
alias_method :lat, :latitude
|
6
|
+
alias_method :lon, :longitude
|
7
|
+
|
8
|
+
# Class methods
|
9
|
+
def self.search(lat, lon, options = {})
|
10
|
+
options[:lat] = lat
|
11
|
+
options[:lon] = lon
|
12
|
+
url = build_url('search', options)
|
13
|
+
get_then_parse(url)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.get_then_parse(url)
|
17
|
+
response = JSON.parse(get(url))
|
18
|
+
results = []
|
19
|
+
if response && response['result']
|
20
|
+
response['result'].each do |entity|
|
21
|
+
results << Entity.new(entity)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
return results
|
25
|
+
end
|
26
|
+
|
27
|
+
# Instance methods
|
28
|
+
def initialize(attrs)
|
29
|
+
@raw_json = JSON.generate(attrs)
|
30
|
+
@guid = attrs['guid']
|
31
|
+
if attrs['meta']
|
32
|
+
@name = attrs['meta']['name']
|
33
|
+
@views = attrs['meta']['views'] || []
|
34
|
+
@userviews = attrs['meta']['userviews'] || []
|
35
|
+
@type = attrs['meta']['type'].to_sym
|
36
|
+
end
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_s
|
41
|
+
self.name
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_json
|
45
|
+
self.raw_json
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
data/lib/geoapi/view.rb
ADDED
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geoapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Bruce
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-10 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rest-client
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0.9"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.3
|
34
|
+
version:
|
35
|
+
description: A Ruby wrapper for the GeoAPI.com API. See http://api.geoapi.com for more information about the API.
|
36
|
+
email: chrisabruce@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- README
|
45
|
+
- LICENSE
|
46
|
+
- geoapi.gemspec
|
47
|
+
- lib/geoapi.rb
|
48
|
+
- lib/geoapi/base.rb
|
49
|
+
- lib/geoapi/entity.rb
|
50
|
+
- lib/geoapi/view.rb
|
51
|
+
- lib/geoapi/version.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/chrisabruce/GeoAPI/
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options:
|
58
|
+
- --main
|
59
|
+
- README.rdoc
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.5
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: A Ruby wrapper for the GeoAPI.com API.
|
81
|
+
test_files: []
|
82
|
+
|