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
Stefan Lewandowski
committed
Jan 6, 2010
1 parent
0b7a15c
commit 5fc3459
Showing
12 changed files
with
869 additions
and
1 deletion.
There are no files selected for viewing
Submodule geoapi
deleted from
e1d26f
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,94 @@ | ||
|
||
%w{rubygems rest_client httparty json crack/json uuidtools}.each { |x| require x } | ||
|
||
# Patch HTTParty to debug | ||
HTTParty::Request.class_eval do | ||
class << self | ||
attr_accessor :debug | ||
end | ||
self.debug = true | ||
|
||
def perform_with_debug | ||
if self.class.debug | ||
puts "HTTParty making #{http_method::METHOD} request to:" | ||
puts uri | ||
puts "With Body: #{body}" | ||
end | ||
|
||
retryable( :tries => 2 ) do | ||
perform_without_debug | ||
end | ||
end | ||
|
||
alias_method :perform_without_debug, :perform | ||
alias_method :perform, :perform_with_debug | ||
end | ||
|
||
|
||
# monkey patch HTTParty to use a configurable timeout | ||
module HTTParty | ||
class Request | ||
private | ||
def http | ||
http = Net::HTTP.new(uri.host, uri.port, options[:http_proxyaddr], options[:http_proxyport]) | ||
http.use_ssl = (uri.port == 443) | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
http.open_timeout = http.read_timeout = options[:timeout].to_i if options[:timeout].to_i > 0 | ||
puts "HTTParty timeout set to #{http.open_timeout}" | ||
http | ||
end | ||
end | ||
end | ||
|
||
def retryable(options = {}, &block) | ||
opts = { :tries => 1, :on => Exception }.merge(options) | ||
|
||
retry_exception, retries = opts[:on], opts[:tries] | ||
|
||
begin | ||
return yield | ||
rescue retry_exception | ||
retry if (retries -= 1) > 0 | ||
end | ||
|
||
yield | ||
end | ||
|
||
class Array | ||
# Extract options from a set of arguments. Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash. | ||
# | ||
# def options(*args) | ||
# args.extract_options! | ||
# end | ||
# | ||
# options(1, 2) # => {} | ||
# options(1, 2, :a => :b) # => {:a=>:b} | ||
def extract_options! | ||
last.is_a?(::Hash) ? pop : {} | ||
end | ||
end | ||
|
||
module GeoAPI | ||
API_VERSION = "v1" | ||
API_URL = "http://api.geoapi.com/#{API_VERSION}/" | ||
|
||
class ArgumentError < StandardError; end | ||
class BadRequest < StandardError; end | ||
class NotFound < StandardError; end | ||
class NotAcceptable < StandardError; end | ||
end | ||
|
||
|
||
$:.unshift(File.dirname(__FILE__)) unless | ||
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) | ||
|
||
require 'lib/geo_object' | ||
require 'lib/geometry' | ||
require 'lib/version' | ||
require 'lib/entity' | ||
require 'lib/entry' | ||
require 'lib/view' | ||
require 'lib/user_view' | ||
require 'lib/query' | ||
require 'lib/client' | ||
require 'lib/neighborhood' |
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,32 @@ | ||
module GeoAPI | ||
class Client | ||
|
||
# To connect to GeoAPI, either: | ||
# | ||
# Add export GEOAPI_KEY=<your-api-key> in your .bashrc file | ||
# | ||
# @client = GeoAPI::Client.new | ||
# | ||
# OR | ||
# | ||
# @client = GeoAPI::Client.new('<your-api-key>') | ||
# | ||
# OR set GEOAPI_KEY='<your-api-key>' before including GeoAPI | ||
# | ||
# @client = GeoAPI::Client.new | ||
|
||
def initialize(api_key=nil) | ||
@api_key = api_key || ENV['GEOAPI_KEY'] || GeoAPI::GEOAPI_KEY | ||
end | ||
|
||
def api_key | ||
@api_key | ||
end | ||
|
||
def self.id_from_guid(guid,apikey) | ||
id = guid.sub('user-','') | ||
id = id.sub("#{apikey}-",'') | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.