social_rebate 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in social_rebate.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 daifu
2
+
3
+ MIT License
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Makefile ADDED
@@ -0,0 +1,2 @@
1
+ rebuild:
2
+ gem build social_rebate.gemspec && gem uninstall social_rebate && gem install social_rebate
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # SocialRebate
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'social_rebate'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install social_rebate
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ require "social_rebate/version"
2
+ require "httparty"
3
+ require "json"
4
+ require "uri"
5
+
6
+ require "social_rebate/connection"
@@ -0,0 +1,107 @@
1
+ module SocialRebate
2
+ class Connection
3
+ include HTTParty
4
+
5
+ base_uri "socialrebate.net"
6
+ format :json
7
+ STATUS = ['required', 'VERIFIED', 'VOID', 'COUPON']
8
+
9
+ class ResponseError < StandardError; end
10
+
11
+ def initialize(creds={})
12
+ validate_creds(creds)
13
+
14
+ @configuration = {}
15
+ SocialRebate::Connection.api_token_keys.each do |key|
16
+ @configuration[key] = creds[key].to_s
17
+ end
18
+ @store_key = creds[:store_key]
19
+ @configuration[:format] = 'json'
20
+ end
21
+
22
+ def self.api_token_keys
23
+ [:api_key, :api_secret].freeze
24
+ end
25
+
26
+ def self.api_required_keys
27
+ [:api_key, :api_secret, :store_key].freeze
28
+ end
29
+
30
+ def validate_creds(creds)
31
+ SocialRebate::Connection.api_required_keys.each do |key|
32
+ raise ResponseError.new("Required key: #{key} missing") unless creds[key]
33
+ end
34
+ end
35
+
36
+ def request(method, url, options={})
37
+ unless api_token_keys_valid?
38
+ raise ResponseError.new("Please set api_key and api_secret correctly")
39
+ end
40
+ options[:body] = options[:body].to_json if options[:body]
41
+ parsed_response(self.class.__send__(method, url, options))
42
+ end
43
+
44
+ def parsed_response(response)
45
+ unless response.success?
46
+ raise ResponseError.new(response)
47
+ end
48
+ response.parsed_response
49
+ end
50
+
51
+ def api_token_keys_valid?
52
+ return SocialRebate::Connection.api_token_keys.detect {|key| @configuration[key] == ''} == nil
53
+ end
54
+
55
+ def serialize_param(params)
56
+ params.sort.map {|key, value| URI.escape("#{key}=#{value}")}.join('&')
57
+ end
58
+
59
+ def get(url, params={})
60
+ url = "#{url}?#{serialize_param(@configuration)}"
61
+ unless params.empty?
62
+ url = "#{url}&#{serialize_param(params)}"
63
+ end
64
+ request(:get, url)
65
+ end
66
+
67
+ def post(url, body={}, headers={})
68
+ body.merge!(@configuration).merge!({:store_key => @store_key})
69
+
70
+ options = {}
71
+ options[:body] = body
72
+ options[:headers] = set_headers(headers)
73
+ request(:post, url, options)
74
+ end
75
+
76
+ def put(url, body={}, headers={})
77
+ check_body_params(body)
78
+ check_url(url)
79
+
80
+ body = body.merge!(@configuration).merge!({:store_key => @store_key})
81
+ options = {}
82
+ options[:body] = body
83
+ options[:headers] = set_headers(headers)
84
+ request(:put, url, options)
85
+ end
86
+
87
+ def set_headers(headers)
88
+ hash = {}
89
+ hash = headers unless headers.empty?
90
+ hash['content-type'] = "application/json"
91
+ hash['Accept'] = "application/json"
92
+ hash
93
+ end
94
+
95
+ def check_body_params(body)
96
+ if !body.key?(:status) || !SocialRebate::Connection::STATUS.include?(body[:status])
97
+ raise ResponseError.new("Missing or incorrect status param")
98
+ end
99
+ end
100
+
101
+ def check_url(url)
102
+ if url !~ /\/orders\/[0-9a-z-A-Z]+\/$/i
103
+ raise ResponseError.new("Incorrect put url: ../api/v2/orders/<your order id>/")
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,3 @@
1
+ module SocialRebate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require File.expand_path('../lib/social_rebate/version', __FILE__)
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "social_rebate"
8
+ spec.version = SocialRebate::VERSION
9
+ spec.authors = ["daifu"]
10
+ spec.email = ["daifu.ye@gmail.com"]
11
+ spec.description = %q{This is the api wrapper for social rebate}
12
+ spec.summary = %q{Social Rebate}
13
+ spec.homepage = "http://www.finditparts.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", ">= 2.11.0"
24
+ spec.add_development_dependency "debugger"
25
+
26
+ spec.add_dependency "httparty", ">= 0.9.0"
27
+ spec.add_dependency "json" , "~> 1.8.0"
28
+
29
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe SocialRebate::Connection do
4
+ before :each do
5
+ @creds = {:api_key => "your_api_key", :api_secret => "your_api_secret", :store_key => "your_store_key"}
6
+ @sr = SocialRebate::Connection.new(@creds)
7
+ @creds_params = "api_key=your_api_key&api_secret=your_api_secret&format=json"
8
+ @headers = {'content-type' => 'application/json', 'Accept' => 'application/json'}
9
+ end
10
+
11
+ context "GET" do
12
+ describe "connection method" do
13
+ it "should get last x orders for which social rebate has created a rebate" do
14
+ @sr.stub(:request).with(:get,"/api/v2/orders/?"+@creds_params+"&limit=20&offset=20")
15
+ @sr.get("/api/v2/orders/", {:limit => 20, :offset => 20})
16
+ end
17
+
18
+ it "should raise exception with incorrect keys" do
19
+ expect {
20
+ @sr.get("/api/v2/orders/")
21
+ }.to raise_error(SocialRebate::Connection::ResponseError)
22
+ end
23
+ end
24
+ end
25
+
26
+ context "POST" do
27
+ describe "creating orders" do
28
+ before :each do
29
+ @body = {:order_email => 'test@fip.com', :total_purchase => 10, :order_id => 1}
30
+ end
31
+ it "should create an order" do
32
+ @sr.stub(:request).with(:post, "/api/v2/orders/", {:body => @body.merge(@creds).merge(:format => 'json'), :headers => @headers})
33
+ @sr.post("/api/v2/orders/", @body)
34
+ end
35
+
36
+ it "should raise exception with incorrect keys" do
37
+ expect {
38
+ @sr.post('/api/v2/orders/', @body)
39
+ }.to raise_error(SocialRebate::Connection::ResponseError)
40
+ end
41
+ end
42
+ end
43
+
44
+ context "PUT" do
45
+ describe "updating orders" do
46
+ before :each do
47
+ @body = {:status => "VOID", :order_email => 'test@fip.com', :total_purchase => '30.0', :order_id => 1}
48
+ end
49
+
50
+ it "should update an order" do
51
+ @sr.stub(:request).with(:put, "/api/v2/orders/token/", {:body => @body.merge(@creds).merge(:format => 'json'), :headers => @headers})
52
+ @sr.put("/api/v2/orders/token/", @body)
53
+ end
54
+
55
+ it "should raise exception with incorrect keys" do
56
+ expect {
57
+ @sr.put("/api/v2/orders/token/", @body)
58
+ }.to raise_error(SocialRebate::Connection::ResponseError)
59
+ end
60
+
61
+ it "should raise exception if status is not correct" do
62
+ @body[:status] = "not_valid"
63
+ expect {
64
+ @sr.put("/api/v2/orders/token/", @body)
65
+ }.to raise_error(SocialRebate::Connection::ResponseError)
66
+ end
67
+
68
+ it "should raise exception if url is incorrect" do
69
+ expect {
70
+ @sr.put("/api/v2/orders/", @body)
71
+ }.to raise_error(SocialRebate::Connection::ResponseError)
72
+ end
73
+ end
74
+ end
75
+
76
+ end
@@ -0,0 +1,15 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ Dir[File.dirname(__FILE__)+'/../lib/social_rebate/*.rb'].each{|file| require file}
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+
10
+ # Run specs in random order to surface order dependencies. If you find an
11
+ # order dependency and want to debug it, you can fix the order by providing
12
+ # the seed, which is printed after each run.
13
+ # --seed 1234
14
+ config.order = 'random'
15
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social_rebate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - daifu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 2.11.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.11.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: debugger
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: httparty
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 0.9.0
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 0.9.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: json
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.8.0
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.8.0
110
+ description: This is the api wrapper for social rebate
111
+ email:
112
+ - daifu.ye@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - Makefile
122
+ - README.md
123
+ - Rakefile
124
+ - lib/social_rebate.rb
125
+ - lib/social_rebate/connection.rb
126
+ - lib/social_rebate/version.rb
127
+ - social_rebate.gemspec
128
+ - spec/connection_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: http://www.finditparts.com
131
+ licenses:
132
+ - MIT
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 1.8.24
152
+ signing_key:
153
+ specification_version: 3
154
+ summary: Social Rebate
155
+ test_files:
156
+ - spec/connection_spec.rb
157
+ - spec/spec_helper.rb