simplegeo 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simplegeo.rb CHANGED
@@ -1,177 +1,2 @@
1
- require 'rubygems'
2
-
3
- gem 'crack', '~> 0.1.7'
4
- require 'crack'
5
-
6
- gem 'oauth', '>= 0.3.6'
7
- require 'oauth'
8
-
9
- gem 'json', '>= 1.2.2'
10
- require 'json'
11
-
12
- require 'forwardable'
13
- require 'uri'
14
-
15
- # This is a client for accessing Simplegeo's REST APIs
16
- # for full documentation see:
17
- # http://help.simplegeo.com/faqs/api-documentation/endpoints
18
-
19
- class Simplegeo
20
- BASE_URI = 'http://api.simplegeo.com'
21
- VER = '0.1'
22
- attr_accessor :layer
23
- attr_reader :access_token
24
-
25
- extend Forwardable
26
- def_delegators :access_token, :get, :post, :put, :delete
27
-
28
- class NotAuthorized < StandardError; end
29
- class NotFound < StandardError; end
30
- class InternalError < StandardError; end
31
-
32
- # initialize a new client with a acess key, secret key, and a default layer
33
- def initialize(key, secret, layer = nil)
34
- @access_token = self.class.get_access_token(key, secret)
35
- @layer = layer
36
- end
37
-
38
- # find nearby objects given a lat & lng
39
- # by default, only searches the the current layer
40
- def nearby(lat, lng, options = {})
41
- options[:layers] ||= [self.layer]
42
- perform_get("/nearby/#{lat},#{lng}.json", :query => options)
43
- end
44
-
45
- # reverse geocodes a lat & lng
46
- def nearby_address(lat, lng, options = {})
47
- perform_get("/nearby/address/#{lat},#{lng}.json", :query => options)
48
- end
49
-
50
- def user_stats
51
- perform_get("/stats.json")
52
- end
53
-
54
- def layer_stats
55
- perform_get("/stats/#{layer}.json")
56
- end
57
-
58
- class Records
59
- def initialize(simplegeo) #:nodoc:
60
- @simplegeo = simplegeo
61
- end
62
-
63
- # get a record by id
64
- def get(id)
65
- @simplegeo.records_dispatch(:get, id)
66
- end
67
-
68
- # put a record by id
69
- # data should be a hash with at least lat & lon keys, as well as extra data
70
- def put(id, data)
71
- @simplegeo.records_dispatch(:put, id, data)
72
- end
73
-
74
- # delete a record by id
75
- def delete(id)
76
- @simplegeo.records_dispatch(:delete, id)
77
- end
78
-
79
- # get the history of a record by id
80
- def history(id)
81
- @simplegeo.records_dispatch(:get_history, id)
82
- end
83
- end
84
-
85
- # used to access record APIs
86
- # i.e. client.records.get(1)
87
- def records
88
- Records.new(self)
89
- end
90
-
91
- def records_dispatch(operation, id, body = nil) #:nodoc:
92
- case operation
93
- when :get_history
94
- perform(:get, "/records/#{layer}/#{id}/history.json")
95
- when :put
96
- perform(:put, "/records/#{layer}/#{id}.json", :body => build_feature(id, body))
97
- else
98
- perform(operation, "/records/#{layer}/#{id}.json")
99
- end
100
- end
101
-
102
- protected
103
-
104
- def self.get_access_token(key, secret)
105
- consumer = OAuth::Consumer.new(key,secret, :site => BASE_URI)
106
- OAuth::AccessToken.new(consumer)
107
- end
108
-
109
- def perform(operation, path, options = {})
110
- case operation
111
- when :put, :post
112
- handle_response(self.send(operation, build_uri(path, options), options[:body].to_json))
113
- else
114
- handle_response(self.send(operation, build_uri(path, options)))
115
- end
116
- end
117
-
118
- def perform_get(path, options = {})
119
- perform(:get, path, options)
120
- end
121
-
122
- def perform_delete(path, options = {})
123
- perform(:delete, path, options)
124
- end
125
-
126
- def perform_put(path, options = {})
127
- perform(:put, path, options)
128
- end
129
-
130
- def perform_post(path, options = {})
131
- perform(:post, path, options)
132
- end
133
-
134
- def build_uri(path, options = {})
135
- uri = URI.parse("/#{VER}#{path}")
136
- uri.query = build_query(options[:query])
137
- uri.to_s
138
- end
139
-
140
- def build_query(query)
141
- if query && query != {}
142
- query.map do |k,v|
143
- [k.to_s,v.to_s].join('=')
144
- end.join('&')
145
- end
146
- end
147
-
148
- def build_feature(id, body)
149
- {:type => 'Feature',
150
- :id => id,
151
- :created => body[:created],
152
- :geometry => { :type => 'Point',
153
- :coordinates => [body[:lon], body[:lat]]},
154
- :properties => body.reject{|k,v| [:created, :lat, :lon].include?(k) }}
155
- end
156
-
157
- def handle_response(response)
158
- raise_errors(response)
159
- parse(response)
160
- end
161
-
162
- def raise_errors(response)
163
- case response.code.to_i
164
- when 401
165
- raise NotAuthorized.new "(#{response.code}): #{response.message}"
166
- when 404
167
- raise NotFound.new "(#{response.code}): #{response.message}"
168
- when 500
169
- raise InternalError.new "(#{response.code}): #{response.message}"
170
- end
171
- end
172
-
173
- def parse(response)
174
- Crack::JSON.parse(response.body)
175
- end
176
-
177
- end
1
+ # So you can require "simplegeo" instead of "simple_geo"
2
+ require "simple_geo"
data/simplegeo.gemspec CHANGED
@@ -5,37 +5,54 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simplegeo}
8
- s.version = "0.0.3"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Scott Windsor"]
12
- s.date = %q{2010-06-16}
13
- s.description = %q{a simplegeo client written in ruby. see http://help.simplegeo.com/faqs/api-documentation/endpoints for more info.}
14
- s.email = %q{swindsor@gmail.com}
11
+ s.authors = ["Dan Dofter"]
12
+ s.date = %q{2010-05-12}
13
+ s.email = %q{dan@dofter.com}
15
14
  s.extra_rdoc_files = [
16
15
  "LICENSE",
17
16
  "README.rdoc"
18
17
  ]
19
18
  s.files = [
20
- ".gitignore",
19
+ ".document",
20
+ ".gitignore",
21
21
  "LICENSE",
22
22
  "README.rdoc",
23
23
  "Rakefile",
24
24
  "VERSION",
25
+ "lib/simple_geo.rb",
26
+ "lib/simple_geo/client.rb",
27
+ "lib/simple_geo/connection.rb",
28
+ "lib/simple_geo/endpoint.rb",
29
+ "lib/simple_geo/hash_utils.rb",
30
+ "lib/simple_geo/record.rb",
25
31
  "lib/simplegeo.rb",
26
32
  "simplegeo.gemspec",
27
- "spec/simplegeo_spec.rb",
33
+ "spec/client_spec.rb",
34
+ "spec/fixtures/contains.json",
35
+ "spec/fixtures/get_density_by_day.json",
36
+ "spec/fixtures/get_density_by_hour.json",
37
+ "spec/fixtures/get_history.json",
38
+ "spec/fixtures/get_nearby.json",
39
+ "spec/fixtures/get_record.json",
40
+ "spec/fixtures/get_records.json",
41
+ "spec/fixtures/layer_info.json",
42
+ "spec/fixtures/nearby_address.json",
43
+ "spec/fixtures/no_such_record.json",
44
+ "spec/fixtures/nonetype_not_iterable.json",
45
+ "spec/fixtures/overlaps.json",
28
46
  "spec/spec.opts",
29
- "spec/spec_helper.rb",
30
- "spec/test_keys.yml"
47
+ "spec/spec_helper.rb"
31
48
  ]
32
- s.homepage = %q{http://github.com/sentientmonkey/simplegeo-ruby}
49
+ s.homepage = %q{http://github.com/simplegeo/simplegeo-ruby}
33
50
  s.rdoc_options = ["--charset=UTF-8"]
34
51
  s.require_paths = ["lib"]
35
52
  s.rubygems_version = %q{1.3.6}
36
- s.summary = %q{a simplegeo client written in ruby}
53
+ s.summary = %q{A SimpleGeo Ruby Client}
37
54
  s.test_files = [
38
- "spec/simplegeo_spec.rb",
55
+ "spec/client_spec.rb",
39
56
  "spec/spec_helper.rb"
40
57
  ]
41
58
 
@@ -44,24 +61,21 @@ Gem::Specification.new do |s|
44
61
  s.specification_version = 3
45
62
 
46
63
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
- s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
48
- s.add_development_dependency(%q<hanna>, [">= 0.1.12"])
49
- s.add_runtime_dependency(%q<oauth>, [">= 0.3.6"])
50
- s.add_runtime_dependency(%q<crack>, [">= 0.1.7"])
51
- s.add_runtime_dependency(%q<json>, [">= 1.2.2"])
64
+ s.add_runtime_dependency(%q<oauth>, [">= 0.4.0"])
65
+ s.add_runtime_dependency(%q<json_pure>, [">= 0"])
66
+ s.add_development_dependency(%q<rspec>, [">= 1.2.0"])
67
+ s.add_development_dependency(%q<fakeweb>, [">= 1.2.0"])
52
68
  else
53
- s.add_dependency(%q<rspec>, [">= 1.3.0"])
54
- s.add_dependency(%q<hanna>, [">= 0.1.12"])
55
- s.add_dependency(%q<oauth>, [">= 0.3.6"])
56
- s.add_dependency(%q<crack>, [">= 0.1.7"])
57
- s.add_dependency(%q<json>, [">= 1.2.2"])
69
+ s.add_dependency(%q<oauth>, [">= 0.4.0"])
70
+ s.add_dependency(%q<json_pure>, [">= 0"])
71
+ s.add_dependency(%q<rspec>, [">= 1.2.0"])
72
+ s.add_dependency(%q<fakeweb>, [">= 1.2.0"])
58
73
  end
59
74
  else
60
- s.add_dependency(%q<rspec>, [">= 1.3.0"])
61
- s.add_dependency(%q<hanna>, [">= 0.1.12"])
62
- s.add_dependency(%q<oauth>, [">= 0.3.6"])
63
- s.add_dependency(%q<crack>, [">= 0.1.7"])
64
- s.add_dependency(%q<json>, [">= 1.2.2"])
75
+ s.add_dependency(%q<oauth>, [">= 0.4.0"])
76
+ s.add_dependency(%q<json_pure>, [">= 0"])
77
+ s.add_dependency(%q<rspec>, [">= 1.2.0"])
78
+ s.add_dependency(%q<fakeweb>, [">= 1.2.0"])
65
79
  end
66
80
  end
67
81