ruby-promobox 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d4e531ad0f13cc26fa696d41b6f351cb16d7a974
4
+ data.tar.gz: ec7d65e6234fe848b7e407ba4610a1cda47ead9e
5
+ SHA512:
6
+ metadata.gz: 0b36ea840c238919de359b6ca6b63e2634f42bb38ba431c87cf63ea0b5bce980f6d51819ba159bbb37ddcd2ddbf7f07996e2b3e3e2b462f664ea6861b0727b87
7
+ data.tar.gz: d74dfa6fbaa1c37f489583ef443f491eba557c6817d42961558f3a978067504cd0090efbbbbe4d3e954a52f7cc6b640680235397b01d2b8cbe73e13bdc65963e
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2013 Laurent Arnoud <laurent.arnoud@af83.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,33 @@
1
+ ruby-promobox
2
+ =============
3
+
4
+ Usage
5
+ -----
6
+
7
+
8
+ ```ruby
9
+ @promobox = Promobox.new('apikey', 'demo@atipik.fr', 'password')
10
+ ```
11
+
12
+ Implemented methods:
13
+
14
+ ```ruby
15
+ @promobox.search(params)
16
+ @promobox.coupons(params)
17
+ @promobox.shops(params)
18
+ @promobox.coupon(id)
19
+ @promobox.shop(id)
20
+ ```
21
+
22
+ Resources
23
+ ---------
24
+
25
+ * http://promobox.fr/
26
+ * http://api.promobox.fr/doc/
27
+
28
+ Tests
29
+ -----
30
+
31
+ ``` ruby
32
+ bundle exec ruby test/promobox_test.rb
33
+ ```
data/lib/promobox.rb ADDED
@@ -0,0 +1,59 @@
1
+ # encoding: UTF-8
2
+ require 'digest/md5'
3
+ require 'digest/sha1'
4
+ require 'base64'
5
+ require 'uri'
6
+ require 'open-uri'
7
+ require 'multi_json'
8
+
9
+ class Promobox
10
+
11
+ API_VERSION = 'v3'
12
+ API_URL = "http://api.promobox.fr/api/#{API_VERSION}"
13
+
14
+ def initialize(api_key, login, password)
15
+ @api_key = api_key
16
+ @login = login
17
+ @password = password
18
+ @hash_password = Digest::MD5.digest("#{@password}{#{@api_key}}").unpack('H*').first
19
+ end
20
+
21
+ %w{coupons search shops}.each do |m|
22
+ define_method m do |*params|
23
+ url = build_query(m, params.first || {})
24
+ decode_url(url)
25
+ end
26
+ end
27
+
28
+ def coupon(id)
29
+ url = build_query("#{__method__}/#{id}")
30
+ decode_url(url)
31
+ end
32
+
33
+ def shop(id)
34
+ url = build_query("#{__method__}/#{id}")
35
+ decode_url(url)
36
+ end
37
+
38
+ private
39
+
40
+ def decode_url(url)
41
+ response = Kernel.open(url)
42
+ MultiJson.decode(response)
43
+ end
44
+
45
+ def build_query(action, params = {})
46
+ ts = Time.now.to_i
47
+ token = Base64.encode64(Digest::SHA1.digest("#{@hash_password}#{ts}#{@api_key}")).chomp
48
+ query = {
49
+ ts: ts.to_s,
50
+ login: @login,
51
+ token: token
52
+ }
53
+ auth_params = URI.encode_www_form query
54
+ encoded_params = URI.encode_www_form params
55
+ url = "#{API_URL}/#{action}?#{auth_params}"
56
+ url += "&#{encoded_params}" unless params.nil? || params.empty?
57
+ url
58
+ end
59
+ end
@@ -0,0 +1,140 @@
1
+ # encoding: UTF-8
2
+ require_relative 'test_helper'
3
+
4
+ describe Promobox do
5
+ include RR::Adapters::TestUnit
6
+
7
+ before do
8
+ @promobox = Promobox.new('apikey', 'demo@atipik.fr', 'password')
9
+ end
10
+
11
+ def promobox_stub(action, params={})
12
+ file = "#{action}.json"
13
+ file = "#{action}_#{URI.encode_www_form params}.json" unless params.empty?
14
+ fixture_file = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', file))
15
+ url = @promobox.send(:build_query, action, params)
16
+ stub(Kernel).open(url) { File.read fixture_file }
17
+ end
18
+
19
+ describe 'search' do
20
+ before do
21
+ promobox_stub('search')
22
+ end
23
+
24
+ it 'should respond' do
25
+ search = @promobox.search
26
+ search.must_be_instance_of Hash
27
+ search['data'].wont_be_empty
28
+ search['total_page'].must_equal 22
29
+ search['total_item'].must_equal 430
30
+ end
31
+ end
32
+
33
+ describe 'coupons' do
34
+ before do
35
+ promobox_stub('coupons')
36
+ end
37
+
38
+ it 'should respond' do
39
+ coupons = @promobox.coupons
40
+ coupons.must_be_instance_of Hash
41
+ coupons['data'].wont_be_empty
42
+ coupons['current_page'].must_equal 1
43
+ coupons['total_page'].must_equal 28
44
+ coupons['total_item'].must_equal 544
45
+ end
46
+ end
47
+
48
+ describe 'shops' do
49
+ describe 'without params' do
50
+ before do
51
+ promobox_stub('shops')
52
+ end
53
+
54
+ it 'should respond empty data' do
55
+ shops = @promobox.shops
56
+ shops.must_be_instance_of Hash
57
+ shops['data'].must_be_empty
58
+ shops['current_page'].must_equal 1
59
+ shops['total_page'].must_equal 0
60
+ shops['total_item'].must_equal 0
61
+ end
62
+ end
63
+
64
+ describe 'with params' do
65
+ before do
66
+ @params = {lat: '49.824837', lon: '3.280160'}
67
+ promobox_stub('shops', @params)
68
+ end
69
+
70
+ it 'should respond' do
71
+ shops = @promobox.shops(@params)
72
+ shops.must_be_instance_of Hash
73
+ shops['data'].wont_be_empty
74
+ shops['current_page'].must_equal 1
75
+ shops['total_page'].must_equal 260
76
+ shops['total_item'].must_equal 5187
77
+ end
78
+ end
79
+ end
80
+
81
+ describe 'coupon' do
82
+ it 'must have an id' do
83
+ proc { @promobox.coupon }.must_raise ArgumentError
84
+ end
85
+
86
+ describe 'invalid id' do
87
+ before do
88
+ promobox_stub('coupon/af83')
89
+ end
90
+
91
+ it 'should respond' do
92
+ coupon = @promobox.coupon 'af83'
93
+ coupon['status'].must_equal 'error'
94
+ coupon['status_code'].must_equal 404
95
+ end
96
+ end
97
+
98
+ describe 'valid id' do
99
+ before do
100
+ promobox_stub('coupon/7465')
101
+ end
102
+
103
+ it 'should respond' do
104
+ coupon = @promobox.coupon '7465'
105
+ coupon['status'].must_equal nil
106
+ coupon['id'].must_equal 7465
107
+ end
108
+ end
109
+ end
110
+
111
+ describe 'shop' do
112
+ it 'must have an id' do
113
+ proc { @promobox.shop }.must_raise ArgumentError
114
+ end
115
+
116
+ describe 'invalid id' do
117
+ before do
118
+ promobox_stub('shop/af83')
119
+ end
120
+
121
+ it 'should respond' do
122
+ shop = @promobox.shop 'af83'
123
+ shop['status'].must_equal 'error'
124
+ shop['status_code'].must_equal 404
125
+ end
126
+ end
127
+
128
+ describe 'valid id' do
129
+ before do
130
+ promobox_stub('shop/2004')
131
+ end
132
+
133
+ it 'should respond' do
134
+ shop = @promobox.shop '2004'
135
+ shop['status'].must_equal nil
136
+ shop['id'].must_equal 2004
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: UTF-8
2
+ require 'minitest/autorun'
3
+ require 'rr'
4
+ require 'promobox'
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-promobox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Laurent Arnoud
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rr
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.5
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.5
55
+ description: Ruby API promobox
56
+ email: laurent.arnoud@af83.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files:
60
+ - README.markdown
61
+ files:
62
+ - LICENSE
63
+ - README.markdown
64
+ - Gemfile
65
+ - lib/promobox.rb
66
+ - test/test_helper.rb
67
+ - test/promobox_test.rb
68
+ homepage: http://github.com/AF83/ruby-promobox
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.0.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Ruby lib for talking with promobox API
92
+ test_files:
93
+ - test/promobox_test.rb