forked from chrisabruce/GeoAPI
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Bruce
committed
Nov 12, 2009
0 parents
commit 4fb09a7
Showing
9 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
demo.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License | ||
|
||
Copyright (c) 2009 Chris Bruce | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
= NOTE | ||
|
||
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. | ||
|
||
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. | ||
|
||
= GeoAPI | ||
|
||
A Ruby wrapper for the GeoAPI.com APIs. This gem was almost entirely inspired by the various geoplanet gems. | ||
|
||
== Usage | ||
|
||
=== Reverse Geocoding: | ||
|
||
require 'geoapi' | ||
GeoAPI.apikey = [Your App ID Here] | ||
|
||
# Location | ||
latitude = -27.000 | ||
longitude = -131.000 | ||
|
||
# Non Required Options | ||
optional_parameters = {:radius => '500m', :type => 'POI', :include_parents => true, :limit => 5, :pretty => true} | ||
|
||
# Lookup | ||
entities = GeoAPI::Entity.search(latitude, longitude, optional_parameters) | ||
|
||
|
||
|
||
=== Views | ||
|
||
require 'geoapi' | ||
GeoAPI.apikey = [Your App ID Here] | ||
|
||
|
||
== REQUIREMENTS: | ||
|
||
To use this library, you must have a valid GeoAPI.com API Key. | ||
You can get one at http://api.geoapi.com | ||
|
||
Additionally, geoapi has the following gem dependencies: | ||
|
||
* rest-client >= 0.9 | ||
* json >= 1.1.3 | ||
|
||
Please note that if you have ActiveSupport::JSON defined (either by | ||
manually having loaded it or when you use geoapi within a Rails | ||
application) the json dependency will be ignored and geoapi uses | ||
ActiveSupport::JSON instead. | ||
|
||
== INSTALL: | ||
|
||
gem install geoapi --source http://gems.github.com | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Gem::Specification.new do |s| | ||
s.name = "geoapi" | ||
s.version = "0.0.1" | ||
s.date = "2009-11-10" | ||
s.summary = "A Ruby wrapper for the GeoAPI.com API." | ||
s.email = "chrisabruce@gmail.com" | ||
s.homepage = "http://github.com/chrisabruce/GeoAPI/" | ||
s.description = "A Ruby wrapper for the GeoAPI.com API. See http://api.geoapi.com for more information about the API." | ||
s.authors = ["Chris Bruce"] | ||
|
||
s.files = [ | ||
"README.rdoc", | ||
"townme.gemspec", | ||
"lib/geoapi.rb", | ||
"lib/geoapi/base.rb", | ||
"lib/geoapi/entity.rb", | ||
"lib/geoapi/view.rb", | ||
"lib/geoapi/version.rb" | ||
] | ||
|
||
s.add_dependency("rest-client", [">= 0.9"]) | ||
s.add_dependency("json", [">= 1.1.3"]) | ||
|
||
s.has_rdoc = false | ||
s.rdoc_options = ["--main", "README.rdoc"] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
%w{rubygems rest_client}.each { |x| require x } | ||
|
||
if defined?(ActiveSupport::JSON) | ||
JSON = ActiveSupport::JSON | ||
module JSON | ||
def self.parse(json) | ||
decode(json) | ||
end | ||
end | ||
else | ||
require 'json' | ||
end | ||
|
||
require 'geoapi/version' | ||
require 'geoapi/base' | ||
require 'geoapi/entity' | ||
require 'geoapi/view' | ||
|
||
module GeoAPI | ||
API_VERSION = "v1" | ||
API_URL = "http://api.geoapi.com/#{API_VERSION}/" | ||
|
||
class << self | ||
attr_accessor :apikey | ||
end | ||
|
||
class BadRequest < StandardError; end | ||
class NotFound < StandardError; end | ||
class NotAcceptable < StandardError; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module GeoAPI | ||
class Base | ||
class << self | ||
def build_url(resource_path, options = {}) | ||
|
||
options[:apikey] ||= TownMe.apikey | ||
query_string = build_query_params(options) | ||
|
||
"#{GeoAPI::API_URL}#{resource_path}#{query_string}" | ||
end | ||
|
||
def get(url) | ||
RestClient.get(url) | ||
rescue RestClient::RequestFailed | ||
raise BadRequest, "Parameter invalid" | ||
rescue RestClient::ResourceNotFound | ||
raise NotFound, "GUID invalid" | ||
end | ||
|
||
protected | ||
|
||
# Take options and build query string | ||
def build_query_params(options) | ||
query = {} | ||
|
||
# Filter '_' and convert True/False | ||
options.each_pair do |key, value| | ||
new_key = key.to_s.gsub(/_/, '-') | ||
new_val = case value | ||
when TrueClass then 1 | ||
when FalseClass then 0 | ||
else value | ||
end | ||
query[new_key] = new_val | ||
end | ||
"?" + query.map{|k,v| "#{k}=#{v}"}.join('&') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module TownMe | ||
class Entity < Base | ||
attr_reader :guid, :name, :type, :geom, :url, :latitude, :longitude, :raw_json | ||
|
||
alias_method :lat, :latitude | ||
alias_method :lon, :longitude | ||
|
||
# Class methods | ||
def self.search(lat, lon, options = {}) | ||
options[:lat] = lat | ||
options[:lon] = lon | ||
url = build_url('search', options) | ||
get_then_parse(url) | ||
end | ||
|
||
def self.get_then_parse(url) | ||
puts url | ||
return Entity.new(get(url)) | ||
end | ||
|
||
# Instance methods | ||
def initialize(json) | ||
@raw_json = json | ||
self | ||
end | ||
|
||
def to_s | ||
self.name | ||
end | ||
|
||
def to_json | ||
self.raw_json | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module GeoAPI | ||
module VERSION #:nodoc: | ||
MAJOR = 0 | ||
MINOR = 0 | ||
TINY = 1 | ||
STRING = [MAJOR, MINOR, TINY].join('.') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module GeoAPI | ||
class View < Base | ||
end | ||
end |