Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit a828f083c14e7832e6cae22ba9fffeac31c28d81 @archfear archfear committed May 10, 2010
Showing with 544 additions and 0 deletions.
  1. +5 −0 .document
  2. +21 −0 .gitignore
  3. +20 −0 LICENSE
  4. +17 −0 README.rdoc
  5. +60 −0 Rakefile
  6. +1 −0 VERSION
  7. +21 −0 lib/simple_geo.rb
  8. +167 −0 lib/simple_geo/client.rb
  9. +107 −0 lib/simple_geo/connection.rb
  10. +41 −0 lib/simple_geo/record.rb
  11. +2 −0 lib/simplegeo.rb
  12. +20 −0 test/client_test.rb
  13. +38 −0 test/fixtures/get_record.json
  14. +24 −0 test/test_helper.rb
@@ -0,0 +1,5 @@
+README.rdoc
+lib/**/*.rb
+bin/*
+features/**/*.feature
+LICENSE
@@ -0,0 +1,21 @@
+## MAC OS
+.DS_Store
+
+## TEXTMATE
+*.tmproj
+tmtags
+
+## EMACS
+*~
+\#*
+.\#*
+
+## VIM
+*.swp
+
+## PROJECT::GENERAL
+coverage
+rdoc
+pkg
+
+## PROJECT::SPECIFIC
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Dan Dofter
+
+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.
@@ -0,0 +1,17 @@
+= sg-ruby
+
+Description goes here.
+
+== Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+== Copyright
+
+Copyright (c) 2010 Dan Dofter. See LICENSE for details.
@@ -0,0 +1,60 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "sg-ruby"
+ gem.summary = %Q{A SimpleGeo Ruby Client}
+ # gem.description = %Q{}
+ gem.email = "dan@dofter.com"
+ gem.homepage = "http://github.com/archfear/sg-ruby"
+ gem.authors = ["Dan Dofter"]
+
+ gem.add_dependency("oauth", ">= 0.4.0")
+ gem.add_dependency("json_pure", "~> 1.2.4")
+
+ gem.add_development_dependency("shoulda", "~> 2.10.0")
+ gem.add_development_dependency("jnunemaker-matchy", "~> 0.4.0")
+ gem.add_development_dependency("mocha", "~> 0.9.0")
+ gem.add_development_dependency("fakeweb", "~> 1.2.0")
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/*_test.rb'
+ test.verbose = true
+end
+
+begin
+ require 'rcov/rcovtask'
+ Rcov::RcovTask.new do |test|
+ test.libs << 'test'
+ test.pattern = 'test/**/*_test.rb'
+ test.verbose = true
+ end
+rescue LoadError
+ task :rcov do
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
+ end
+end
+
+task :test => :check_dependencies
+
+task :default => :test
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "sg-ruby #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+0.1.0
@@ -0,0 +1,21 @@
+require 'uri'
+require 'json'
+require 'oauth'
+
+require 'simple_geo/connection'
+require 'simple_geo/client'
+require 'simple_geo/record'
+
+module SimpleGeo
+ API_VERSION = '0.1'.freeze
+ REALM = "http://api.simplegeo.com"
+ VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION'))
+
+ class SimpleGeoError < StandardError; end
+ class Unauthorized < SimpleGeoError; end
+ class NotFound < SimpleGeoError; end
+ class ServerError < SimpleGeoError; end
+ class Unavailable < SimpleGeoError; end
+ class DecodeError < SimpleGeoError; end
+ class NoConnectionEstablished < SimpleGeoError; end
+end
@@ -0,0 +1,167 @@
+module SimpleGeo
+
+ class Endpoint
+
+ class << self
+ def record(layer, id)
+ endpoint_url "records/#{layer}/#{id}.json"
+ end
+
+ def records(layer, ids)
+ ids = ids.join(',') if ids.is_a? Array
+ endpoint_url "records/#{layer}/#{ids}.json"
+ end
+
+ def add_records(layer)
+ endpoint_url "records/#{layer}.json"
+ end
+
+ def history(layer, id)
+ endpoint_url "records/#{layer}/#{id}/history.json"
+ end
+
+ def nearby(layer, geohash)
+ endpoint_url "records/#{layer}/nearby/#{geohash}.json"
+ end
+
+ def nearby_address(lat, lon)
+ endpoint_url "nearby/address/#{lat},#{lon}.json"
+ end
+
+ def density(lat, lon, day, hour=nil)
+ if hour.nil?
+ path = "density/#{day}/#{lat},#{lon}.json"
+ else
+ path = "density/#{day}/#{hour}/#{lat},#{lon}.json"
+ end
+ endpoint_url path
+ end
+
+ def layer(layer)
+ endpoint_url "layer/#{layer}.json"
+ end
+
+ def contains(lat, lon)
+ endpoint_url "contains/#{lat},#{lon}.json"
+ end
+
+ def overlaps(south, west, north, east)
+ endpoint_url "overlaps/#{south},#{west},#{north},#{east}.json"
+ end
+
+ def boundary(id)
+ endpoint_url "boundary/#{id}.json"
+ end
+
+ def endpoint_url(path)
+ [REALM, API_VERSION, path].join('/')
+ end
+ end
+
+ end
+
+ class Client
+
+ @@connection = nil
+ @@debug = false
+
+ class << self
+
+ def set_credentials(token, secret)
+ @@connection = Connection.new(token, secret)
+ @@connection.debug = @@debug
+ end
+
+ def debug=(debug_flag)
+ @@debug = debug_flag
+ @@connection.debug = @@debug if @@connection
+ end
+
+ def debug
+ @@debug
+ end
+
+ def add_record(record)
+ raise SimpleGeoError, "Record has no layer" if record.layer.nil?
+ put Endpoint.record(record.layer, record.id), record
+ end
+
+ def delete_record(layer, id)
+ delete Endpoint.record(layer, id)
+ end
+
+ def get_record(layer, id)
+ get Endpoint.record(layer, id)
+ end
+
+ def add_records(layer, records)
+ features = {
+ :type => 'FeatureCollection',
+ :features => records.collect { |record| record.to_hash }
+ }
+ post Endpoint.add_records(layer), "POST", features
+ end
+
+ def get_records(layer, ids)
+ features = get Endpoint.records(layer, ids)
+ features['features'] || []
+ end
+
+ def get_history(layer, id, options={})
+ get Endpoint.history(layer, id), options
+ end
+
+ #TODO: get nearby by lat, lon
+ # api.simplegeo.com/{version}/records/{layer}/nearby/{lat},{lon}.json
+ def get_nearby(layer, geohash, options={})
+ get Endpoint.nearby(layer, geohash), options
+ end
+
+ def get_nearby_address(lat, lon)
+ get Endpoint.nearby_address(lat, lon)
+ end
+
+ def get_layer_information(layer)
+ get Endpoint.layer(layer)
+ end
+
+ def get_density(lat, lon, day, hour=nil)
+ get Endpoint.density(lat, lon, day, hour)
+ end
+
+ def get_overlaps(south, west, north, east, options)
+ get Endpoint.overlaps(south, west, north, east), options
+ end
+
+ def get_boundary(id)
+ get Endpoint.boundary(id)
+ end
+
+ def get_contains(lat, lon)
+ get Endpoint.contains(lat, lon)
+ end
+
+ def get(endpoint, data=nil)
+ raise NoConnectionEstablished if @@connection.nil?
+ @@connection.get endpoint, data
+ end
+
+ def delete(endpoint, data=nil)
+ raise NoConnectionEstablished if @@connection.nil?
+ @@connection.delete endpoint, data
+ end
+
+ def post(endpoint, data=nil)
+ raise NoConnectionEstablished if @@connection.nil?
+ @@connection.post endpoint, data
+ end
+
+ def put(endpoint, data=nil)
+ raise NoConnectionEstablished if @@connection.nil?
+ @@connection.put endpoint, data
+ end
+ end
+
+ end
+
+end
Oops, something went wrong.

0 comments on commit a828f08

Please sign in to comment.