groupon 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/groupon.rb +48 -0
- data/lib/groupon/client.rb +38 -0
- data/test/groupon_test.rb +31 -0
- data/test/helper.rb +43 -0
- metadata +147 -0
data/lib/groupon.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'hashie'
|
3
|
+
|
4
|
+
directory = File.expand_path(File.dirname(__FILE__))
|
5
|
+
|
6
|
+
Hash.send :include, Hashie::HashExtensions
|
7
|
+
|
8
|
+
module Groupon
|
9
|
+
|
10
|
+
VERSION = "0.0.1".freeze
|
11
|
+
|
12
|
+
def self.divisions
|
13
|
+
Groupon::Client.new.divisions
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.deals(options={})
|
17
|
+
Groupon::Client.new.deals(options)
|
18
|
+
end
|
19
|
+
|
20
|
+
# config/initializers/gowalla.rb (for instance)
|
21
|
+
#
|
22
|
+
# Groupon.configure do |config|
|
23
|
+
# config.api_key = 'api_key'
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# elsewhere
|
27
|
+
#
|
28
|
+
# client = Groupon::Client.new
|
29
|
+
def self.configure
|
30
|
+
yield self
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
class << self
|
35
|
+
attr_accessor :api_key
|
36
|
+
end
|
37
|
+
|
38
|
+
class GrouponError < StandardError
|
39
|
+
attr_reader :data
|
40
|
+
|
41
|
+
def initialize(data)
|
42
|
+
@data = data
|
43
|
+
super
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
require File.join(directory, 'groupon', 'client')
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Groupon
|
2
|
+
class Client
|
3
|
+
include HTTParty
|
4
|
+
base_uri "http://www.groupon.com/api/v1"
|
5
|
+
format :json
|
6
|
+
|
7
|
+
def initialize(options={})
|
8
|
+
api_key = options[:api_key] || Groupon.api_key
|
9
|
+
self.class.headers({'X-GrouponToken' => api_key }) unless api_key.nil?
|
10
|
+
end
|
11
|
+
|
12
|
+
def divisions
|
13
|
+
self.class.get("/divisions.json").divisions
|
14
|
+
end
|
15
|
+
|
16
|
+
def deals(query={})
|
17
|
+
division = query.delete(:division)
|
18
|
+
path = division ? "/#{division}" : ""
|
19
|
+
path += "/deals.json"
|
20
|
+
self.class.get(path, :query => query).deals
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def self.get(*args); handle_response super end
|
25
|
+
def self.post(*args); handle_response super end
|
26
|
+
|
27
|
+
def self.handle_response(response)
|
28
|
+
case response.code
|
29
|
+
when 500...600; raise GrouponError.new(Hashie::Mash.new(response).status)
|
30
|
+
else; response
|
31
|
+
end
|
32
|
+
|
33
|
+
Hashie::Mash.new(response)
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class GrouponTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "Groupon API" do
|
6
|
+
|
7
|
+
should "return a list of divisions - cities where Groupon is live" do
|
8
|
+
stub_get("/divisions.json", "divisions.json")
|
9
|
+
divisions = Groupon.divisions
|
10
|
+
divisions.size.should == 61
|
11
|
+
divisions.last.name.should == 'Wichita'
|
12
|
+
divisions.last.location.latitude.should == 37.6922
|
13
|
+
end
|
14
|
+
|
15
|
+
should "return a list of deals" do
|
16
|
+
stub_get("/deals.json?lat=32.781100000000002&lng=-96.9791", "deals.json")
|
17
|
+
deals = Groupon.deals(:lat => "32.781100000000002", :lng => "-96.9791")
|
18
|
+
deals.size.should == 1
|
19
|
+
deals.first.discount_percent.should == 50
|
20
|
+
end
|
21
|
+
|
22
|
+
should "return a list of deals for a specified division" do
|
23
|
+
stub_get("/dallas/deals.json", "deals.json")
|
24
|
+
deals = Groupon.deals(:division => :dallas)
|
25
|
+
deals.size.should == 1
|
26
|
+
deals.first.discount_percent.should == 50
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
require 'redgreen'
|
6
|
+
require 'matchy'
|
7
|
+
require 'fakeweb'
|
8
|
+
|
9
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
10
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
11
|
+
require 'groupon'
|
12
|
+
|
13
|
+
|
14
|
+
# Set the default allow_net_connect option--usually you'll want this off.
|
15
|
+
# You don't usually want your test suite to make HTTP connections, do you?
|
16
|
+
FakeWeb.allow_net_connect = false
|
17
|
+
|
18
|
+
class Test::Unit::TestCase
|
19
|
+
end
|
20
|
+
|
21
|
+
def fixture_file(filename)
|
22
|
+
return '' if filename == ''
|
23
|
+
file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
|
24
|
+
File.read(file_path)
|
25
|
+
end
|
26
|
+
|
27
|
+
def groupon_url(url, options={})
|
28
|
+
url =~ /^http/ ? url : "http://www.groupon.com/api/v1#{url}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def stub_request(method, url, filename, status=nil)
|
32
|
+
options = {:body => ""}
|
33
|
+
options.merge!({:body => fixture_file(filename)}) if filename
|
34
|
+
options.merge!({:body => status.last}) if status
|
35
|
+
options.merge!({:status => status}) if status
|
36
|
+
|
37
|
+
FakeWeb.register_uri(method, groupon_url(url), options)
|
38
|
+
end
|
39
|
+
|
40
|
+
def stub_get(*args); stub_request(:get, *args) end
|
41
|
+
def stub_post(*args); stub_request(:post, *args) end
|
42
|
+
|
43
|
+
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: groupon
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Wynn Netherland
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-23 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
segments:
|
26
|
+
- 0
|
27
|
+
- 1
|
28
|
+
- 3
|
29
|
+
version: 0.1.3
|
30
|
+
name: hashie
|
31
|
+
prerelease: false
|
32
|
+
requirement: *id001
|
33
|
+
type: :runtime
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
- 1
|
42
|
+
- 0
|
43
|
+
version: 0.1.0
|
44
|
+
name: httparty
|
45
|
+
prerelease: false
|
46
|
+
requirement: *id002
|
47
|
+
type: :runtime
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 2
|
55
|
+
- 10
|
56
|
+
- 1
|
57
|
+
version: 2.10.1
|
58
|
+
name: shoulda
|
59
|
+
prerelease: false
|
60
|
+
requirement: *id003
|
61
|
+
type: :development
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
- 4
|
70
|
+
- 0
|
71
|
+
version: 0.4.0
|
72
|
+
name: matchy
|
73
|
+
prerelease: false
|
74
|
+
requirement: *id004
|
75
|
+
type: :development
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
segments:
|
82
|
+
- 1
|
83
|
+
- 2
|
84
|
+
- 5
|
85
|
+
version: 1.2.5
|
86
|
+
name: fakeweb
|
87
|
+
prerelease: false
|
88
|
+
requirement: *id005
|
89
|
+
type: :development
|
90
|
+
- !ruby/object:Gem::Dependency
|
91
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
name: yard
|
99
|
+
prerelease: false
|
100
|
+
requirement: *id006
|
101
|
+
type: :development
|
102
|
+
description: Wrapper for the Groupon API
|
103
|
+
email: wynn.netherland@gmail.com
|
104
|
+
executables: []
|
105
|
+
|
106
|
+
extensions: []
|
107
|
+
|
108
|
+
extra_rdoc_files: []
|
109
|
+
|
110
|
+
files:
|
111
|
+
- lib/groupon/client.rb
|
112
|
+
- lib/groupon.rb
|
113
|
+
has_rdoc: true
|
114
|
+
homepage: http://github.com/pengwynn/groupon
|
115
|
+
licenses: []
|
116
|
+
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
version: "0"
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
segments:
|
134
|
+
- 1
|
135
|
+
- 3
|
136
|
+
- 6
|
137
|
+
version: 1.3.6
|
138
|
+
requirements: []
|
139
|
+
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 1.3.6
|
142
|
+
signing_key:
|
143
|
+
specification_version: 3
|
144
|
+
summary: Wrapper for the Groupon API
|
145
|
+
test_files:
|
146
|
+
- test/helper.rb
|
147
|
+
- test/groupon_test.rb
|