Skip to content

Commit

Permalink
Very rough development version
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Bruce committed Nov 12, 2009
0 parents commit 4fb09a7
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
demo.rb
21 changes: 21 additions & 0 deletions LICENSE
@@ -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.
54 changes: 54 additions & 0 deletions README
@@ -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

26 changes: 26 additions & 0 deletions geoapi.gemspec
@@ -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
30 changes: 30 additions & 0 deletions lib/geoapi.rb
@@ -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
40 changes: 40 additions & 0 deletions lib/geoapi/base.rb
@@ -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
36 changes: 36 additions & 0 deletions lib/geoapi/entity.rb
@@ -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
8 changes: 8 additions & 0 deletions lib/geoapi/version.rb
@@ -0,0 +1,8 @@
module GeoAPI
module VERSION #:nodoc:
MAJOR = 0
MINOR = 0
TINY = 1
STRING = [MAJOR, MINOR, TINY].join('.')
end
end
4 changes: 4 additions & 0 deletions lib/geoapi/view.rb
@@ -0,0 +1,4 @@
module GeoAPI
class View < Base
end
end

0 comments on commit 4fb09a7

Please sign in to comment.