thinknear 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.2-p0@thinknear-ruby
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'oauth'
4
+
5
+ group :development do
6
+ gem 'rspec', '~> 2.3.0'
7
+ gem 'bundler', '~> 1.0.0'
8
+ gem 'jeweler', '~> 1.5.2'
9
+ gem 'rcov', '>= 0'
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ oauth (0.4.4)
11
+ rake (0.8.7)
12
+ rcov (0.9.9)
13
+ rspec (2.3.0)
14
+ rspec-core (~> 2.3.0)
15
+ rspec-expectations (~> 2.3.0)
16
+ rspec-mocks (~> 2.3.0)
17
+ rspec-core (2.3.1)
18
+ rspec-expectations (2.3.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.3.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ jeweler (~> 1.5.2)
28
+ oauth
29
+ rcov
30
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Denis Barushev
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = thinknear
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to thinknear
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Denis Barushev. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "thinknear"
16
+ gem.homepage = "http://github.com/denis/thinknear"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{A ThinkNear Ruby Client}
19
+ gem.email = "barushev@gmail.com"
20
+ gem.authors = ["Denis Barushev"]
21
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
22
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
23
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
24
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "thinknear #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
Binary file
Binary file
@@ -0,0 +1,19 @@
1
+ module ThinkNear
2
+ class Category < OpenStruct
3
+ def self.all
4
+ Client.get_categories.map do |category_name|
5
+ new :name => category_name
6
+ end
7
+ end
8
+
9
+ def draws
10
+ Client.get_draws(name).map do |item|
11
+ Draw.new item['draw']
12
+ end
13
+ end
14
+
15
+ def attributes
16
+ @table
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,64 @@
1
+ module ThinkNear
2
+ class Client
3
+ @@connection = nil
4
+ @@debug = false
5
+
6
+ class << self
7
+ def set_credentials(token, secret)
8
+ @@connection = Connection.new(token, secret)
9
+ @@connection.debug = @@debug
10
+ end
11
+
12
+ def debug=(debug_flag)
13
+ @@debug = debug_flag
14
+ @@connection.debug = @@debug if @@connection
15
+ end
16
+
17
+ def debug
18
+ @@debug
19
+ end
20
+
21
+ def get_ping
22
+ get Endpoint.ping
23
+ end
24
+
25
+ def get_health
26
+ get Endpoint.health
27
+ end
28
+
29
+ def get_authenticated
30
+ get Endpoint.authenticated
31
+ end
32
+
33
+ def get_categories
34
+ get Endpoint.categories
35
+ end
36
+
37
+ def get_draws(category)
38
+ get Endpoint.draws(category)
39
+ end
40
+
41
+ def get_offers(params)
42
+ get Endpoint.offers(params)
43
+ end
44
+
45
+ def get_offer(id)
46
+ get Endpoint.offer(id)
47
+ end
48
+
49
+ def claim_offer(offer_id, latitude, longitude)
50
+ post Endpoint.claim_offer(offer_id, latitude, longitude)
51
+ end
52
+
53
+ def get(endpoint, data=nil)
54
+ raise NoConnectionEstablished if @@connection.nil?
55
+ @@connection.get endpoint, data
56
+ end
57
+
58
+ def post(endpoint, data=nil)
59
+ raise NoConnectionEstablished if @@connection.nil?
60
+ @@connection.post endpoint, data
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,102 @@
1
+ require 'oauth'
2
+
3
+ module ThinkNear
4
+ class Connection
5
+
6
+ attr_accessor :debug
7
+
8
+ def initialize(token, secret)
9
+ consumer = OAuth::Consumer.new(token, secret, :site => REALM)
10
+ @access_token = OAuth::AccessToken.new(consumer)
11
+ debug = false
12
+ end
13
+
14
+ def get(endpoint, data=nil)
15
+ request :get, endpoint, data
16
+ end
17
+
18
+ def post(endpoint, data=nil)
19
+ request :post, endpoint, data
20
+ end
21
+
22
+ private
23
+
24
+ def request(method, endpoint, data)
25
+ headers = { 'User-Agent' => 'ThinkNear Ruby Client' }
26
+
27
+ if [:get].include?(method) && !data.nil?
28
+ endpoint = endpoint + '?' + build_query(data)
29
+ end
30
+
31
+ if debug
32
+ puts "request: #{method.to_s.upcase} #{endpoint}"
33
+ puts "headers:"
34
+ headers.each do |key, value|
35
+ puts "#{key}=#{value}"
36
+ end
37
+ if [:post, :put].include?(method) && !data.nil?
38
+ puts "data:"
39
+ puts data.to_json
40
+ end
41
+ end
42
+
43
+ case method
44
+ when :get
45
+ response = @access_token.request(method, endpoint, headers)
46
+ when :post
47
+ data = data.to_json unless data.nil?
48
+ response = @access_token.request(method, endpoint, data, headers)
49
+ end
50
+
51
+ if debug
52
+ puts "\nresponse: #{response.code}"
53
+ puts "headers:"
54
+ response.header.each do |key, value|
55
+ puts "#{key}=#{value}"
56
+ end
57
+ puts "body:"
58
+ puts response.body
59
+ end
60
+
61
+ raise_errors(response)
62
+
63
+ if response.body.empty?
64
+ content = nil
65
+ else
66
+ begin
67
+ content = JSON.parse(response.body)
68
+ rescue JSON::ParserError
69
+ raise DecodeError, "content: <#{response.body}>"
70
+ end
71
+ end
72
+
73
+ content
74
+ end
75
+
76
+ def build_query(data)
77
+ data.map do |key, value|
78
+ [key.to_s, URI.escape(value.to_s)].join('=')
79
+ end.join('&')
80
+ end
81
+
82
+ def raise_errors(response)
83
+ response_description = "(#{response.code}): #{response.message}"
84
+ response_description += " - #{response.body}" unless response.body.empty?
85
+
86
+ case response.code.to_i
87
+ when 401
88
+ raise Unauthorized
89
+ when 404
90
+ raise NotFound
91
+ when 500
92
+ raise ServerError, "ThinkNear had an internal error. Please let them know. #{response_description}"
93
+ when 502..503
94
+ raise Unavailable, response_description
95
+ else
96
+ unless response.is_a? Net::HTTPSuccess
97
+ raise ThinkNearError, response_description
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,7 @@
1
+ module ThinkNear
2
+ class Draw < OpenStruct
3
+ def attributes
4
+ @table
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,46 @@
1
+ module ThinkNear
2
+ class Endpoint
3
+ class << self
4
+ def ping
5
+ endpoint_url 'ping'
6
+ end
7
+
8
+ def health
9
+ endpoint_url 'health'
10
+ end
11
+
12
+ def authenticated
13
+ endpoint_url 'authenticated'
14
+ end
15
+
16
+ def categories
17
+ endpoint_url 'categories'
18
+ end
19
+
20
+ def draws(category)
21
+ endpoint_url "draws/#{category}"
22
+ end
23
+
24
+ def offers(params)
25
+ latitude = params.delete(:latitude)
26
+ longitude = params.delete(:longitude)
27
+
28
+ params = params.select { |k, v| !v.blank? }
29
+
30
+ endpoint_url("offers/#{latitude},#{longitude}") + "?#{params.to_param}"
31
+ end
32
+
33
+ def offer(id)
34
+ endpoint_url "offers/#{id}"
35
+ end
36
+
37
+ def claim_offer(offer_id, latitude, longitude)
38
+ endpoint_url "offers/#{offer_id}/claim/#{latitude},#{longitude}"
39
+ end
40
+
41
+ def endpoint_url(path, version = API_VERSION, format = 'json')
42
+ [REALM, version, [path, format].join('.')].join('/')
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ module ThinkNear
2
+ class Offer < OpenStruct
3
+ def self.find(id_or_params)
4
+ if id_or_params.is_a? String
5
+ new Client.get_offer(id_or_params)['offer']
6
+ elsif id_or_params.is_a? Hash
7
+ Client.get_offers(id_or_params).map do |item|
8
+ new item['offer']
9
+ end
10
+ end
11
+ end
12
+
13
+ def attributes
14
+ @table
15
+ end
16
+
17
+ def claim(latitude, longitude)
18
+ self.class.new Client.claim_offer(id, latitude, longitude)['offer']
19
+ end
20
+ end
21
+ end
data/lib/think_near.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'ostruct'
2
+
3
+ require 'think_near/connection'
4
+ require 'think_near/endpoint'
5
+ require 'think_near/client'
6
+ require 'think_near/category'
7
+ require 'think_near/draw'
8
+ require 'think_near/offer'
9
+
10
+ module ThinkNear
11
+ API_VERSION = '1.0'.freeze
12
+ REALM = 'http://api-sandbox.thinknear.com'
13
+
14
+ class ThinkNearError < StandardError; end
15
+ class Unauthorized < ThinkNearError; end
16
+ class NotFound < ThinkNearError; end
17
+ class ServerError < ThinkNearError; end
18
+ class Unavailable < ThinkNearError; end
19
+ class DecodeError < ThinkNearError; end
20
+ class NoConnectionEstablished < ThinkNearError; end
21
+ end
data/lib/thinknear.rb ADDED
@@ -0,0 +1,2 @@
1
+ # So you can require 'thinknear' instead of 'think_near'
2
+ require 'think_near'
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'thinknear'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Thinknear" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
data/thinknear.gemspec ADDED
@@ -0,0 +1,76 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{thinknear}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Denis Barushev"]
12
+ s.date = %q{2011-03-16}
13
+ s.email = %q{barushev@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE.txt",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".rspec",
21
+ ".rvmrc",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/think_near.rb",
29
+ "lib/think_near/.client.rb.swn",
30
+ "lib/think_near/.client.rb.swo",
31
+ "lib/think_near/category.rb",
32
+ "lib/think_near/client.rb",
33
+ "lib/think_near/connection.rb",
34
+ "lib/think_near/draw.rb",
35
+ "lib/think_near/endpoint.rb",
36
+ "lib/think_near/offer.rb",
37
+ "lib/thinknear.rb",
38
+ "spec/spec_helper.rb",
39
+ "spec/thinknear_spec.rb",
40
+ "thinknear.gemspec"
41
+ ]
42
+ s.homepage = %q{http://github.com/denis/thinknear}
43
+ s.licenses = ["MIT"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = %q{1.6.2}
46
+ s.summary = %q{A ThinkNear Ruby Client}
47
+ s.test_files = [
48
+ "spec/spec_helper.rb",
49
+ "spec/thinknear_spec.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ s.specification_version = 3
54
+
55
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
56
+ s.add_runtime_dependency(%q<oauth>, [">= 0"])
57
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
58
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
59
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
60
+ s.add_development_dependency(%q<rcov>, [">= 0"])
61
+ else
62
+ s.add_dependency(%q<oauth>, [">= 0"])
63
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
64
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
65
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
66
+ s.add_dependency(%q<rcov>, [">= 0"])
67
+ end
68
+ else
69
+ s.add_dependency(%q<oauth>, [">= 0"])
70
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
71
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
72
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
73
+ s.add_dependency(%q<rcov>, [">= 0"])
74
+ end
75
+ end
76
+
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thinknear
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Denis Barushev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-16 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: oauth
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 2.3.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: jeweler
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.5.2
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rcov
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ description:
72
+ email: barushev@gmail.com
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files:
78
+ - LICENSE.txt
79
+ - README.rdoc
80
+ files:
81
+ - .document
82
+ - .rspec
83
+ - .rvmrc
84
+ - Gemfile
85
+ - Gemfile.lock
86
+ - LICENSE.txt
87
+ - README.rdoc
88
+ - Rakefile
89
+ - VERSION
90
+ - lib/think_near.rb
91
+ - lib/think_near/.client.rb.swn
92
+ - lib/think_near/.client.rb.swo
93
+ - lib/think_near/category.rb
94
+ - lib/think_near/client.rb
95
+ - lib/think_near/connection.rb
96
+ - lib/think_near/draw.rb
97
+ - lib/think_near/endpoint.rb
98
+ - lib/think_near/offer.rb
99
+ - lib/thinknear.rb
100
+ - spec/spec_helper.rb
101
+ - spec/thinknear_spec.rb
102
+ - thinknear.gemspec
103
+ has_rdoc: true
104
+ homepage: http://github.com/denis/thinknear
105
+ licenses:
106
+ - MIT
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3128978856162919585
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: "0"
127
+ requirements: []
128
+
129
+ rubyforge_project:
130
+ rubygems_version: 1.6.2
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: A ThinkNear Ruby Client
134
+ test_files:
135
+ - spec/spec_helper.rb
136
+ - spec/thinknear_spec.rb