big-door 0.0.0

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.
Files changed (42) hide show
  1. data/.bundle/config +2 -0
  2. data/.document +5 -0
  3. data/.gitignore +22 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE +20 -0
  6. data/README.rdoc +17 -0
  7. data/Rakefile +59 -0
  8. data/VERSION +1 -0
  9. data/features/big-door.feature +26 -0
  10. data/features/step_definitions/big-door_steps.rb +83 -0
  11. data/features/support/env.rb +5 -0
  12. data/lib/big-door.rb +127 -0
  13. data/lib/big-door/base.rb +45 -0
  14. data/lib/big-door/currency.rb +25 -0
  15. data/lib/big-door/named_transaction.rb +25 -0
  16. data/lib/big-door/named_transaction_group.rb +27 -0
  17. data/lib/big-door/request.rb +7 -0
  18. data/lib/big-door/response_codes.rb +266 -0
  19. data/lib/big-door/user.rb +49 -0
  20. data/spec/.rspec +1 -0
  21. data/spec/big-door_spec.rb +105 -0
  22. data/spec/spec_helper.rb +19 -0
  23. data/spec/support/vcr.rb +7 -0
  24. data/vcr_cassettes/NamedTransaction/NamedTransaction.yml +213 -0
  25. data/vcr_cassettes/NamedTransaction/specific.yml +6043 -0
  26. data/vcr_cassettes/award/award_summary.yml +34 -0
  27. data/vcr_cassettes/currency/currency.yml +52 -0
  28. data/vcr_cassettes/currency/currency_type.yml +31 -0
  29. data/vcr_cassettes/currency/specific.yml +2980 -0
  30. data/vcr_cassettes/good/good_summary.yml +34 -0
  31. data/vcr_cassettes/level/level_summary.yml +34 -0
  32. data/vcr_cassettes/named_transaction/named_transactions.yml +107 -0
  33. data/vcr_cassettes/named_transaction_group/all.yml +145 -0
  34. data/vcr_cassettes/named_transaction_group/specific.yml +3171 -0
  35. data/vcr_cassettes/user/add_points.yml +36 -0
  36. data/vcr_cassettes/user/all.yml +473 -0
  37. data/vcr_cassettes/user/create.yml +393 -0
  38. data/vcr_cassettes/user/currency_balance.yml +738 -0
  39. data/vcr_cassettes/user/new.yml +5660 -0
  40. data/vcr_cassettes/user/new_currency_balance.yml +470 -0
  41. data/vcr_cassettes/user/specific.yml +213 -0
  42. metadata +175 -0
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_WITHOUT: ""
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source :gemcutter
2
+
3
+ gem "httparty", "~>0.6.0"
4
+ gem "json", "~>1.4.3"
5
+ gem "uuidtools", "~>2.1.1"
6
+
7
+ group :test do
8
+ gem 'fakeweb', '~>1.2.8'
9
+ gem 'vcr', '~>1.0.3'
10
+ gem 'rspec', '2.0.0.beta.19'
11
+ gem 'cucumber', '0.8.5'
12
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Michael Lavrisha
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,17 @@
1
+ = big-door
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Michael Lavrisha. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "big-door"
8
+ gem.summary = %Q{Big Door Media gem}
9
+ gem.description = %Q{A gem to interact with Big Door Media's API}
10
+ gem.email = "mlavrisha@trada.com"
11
+ gem.homepage = "http://github.com/vrish88/big-door"
12
+ gem.authors = ["Michael Lavrisha"]
13
+ gem.add_development_dependency "rspec", "~> 2.0.0.beta.19"
14
+ gem.add_development_dependency "cucumber", "~> 0.8.5"
15
+ gem.add_runtime_dependency "httparty", "~>0.6.0"
16
+ gem.add_runtime_dependency "json", "~>1.4.3"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ #require 'spec/rake/spectask'
25
+ #Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ # spec.libs << 'lib' << 'spec'
27
+ # spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ #end
29
+ #
30
+ #Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ # spec.libs << 'lib' << 'spec'
32
+ # spec.pattern = 'spec/**/*_spec.rb'
33
+ # spec.rcov = true
34
+ #end
35
+ #
36
+ #task :spec => :check_dependencies
37
+
38
+ # begin
39
+ # require 'cucumber/rake/task'
40
+ # Cucumber::Rake::Task.new(:features)
41
+ #
42
+ # task :features => :check_dependencies
43
+ # rescue LoadError
44
+ # task :features do
45
+ # abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
46
+ # end
47
+ # end
48
+
49
+ task :default => :spec
50
+
51
+ require 'rake/rdoctask'
52
+ Rake::RDocTask.new do |rdoc|
53
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
54
+
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = "big-door #{version}"
57
+ rdoc.rdoc_files.include('README*')
58
+ rdoc.rdoc_files.include('lib/**/*.rb')
59
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,26 @@
1
+ Feature: Provide API access to Big Door media
2
+ As a developer
3
+ I want to access Big Door media's API
4
+
5
+ Scenario: Sending a request to get award summary
6
+ Given I have API credentials
7
+ When I make a request for "award_summary"
8
+ Then the response should be "[]"
9
+
10
+ Scenario: Creating an end user
11
+ Given I have API credentials
12
+ When I create a user named "testers"
13
+ Then I should receive a "BigDoor::User" object
14
+
15
+ Scenario: Checking the currency balance for a user
16
+ Given I have API credentials
17
+ And I have a user
18
+ When I check the user's "currency_balances"
19
+ Then the response should be "[]"
20
+
21
+ Scenario: Adding points to a user's account
22
+ Given I have API credentials
23
+ And I have a user
24
+ And I have a "NamedTransactionGroup" named "Not so basic"
25
+ When I add "30" points to users's account
26
+ Then their account should have "30" more points
@@ -0,0 +1,83 @@
1
+ # Givens
2
+ Given /^I have API credentials$/ do
3
+ @big_door = BigDoor::Base.new(:app_key => '0d38d9cd41b0440bbf22c0720be58b27', :secret_key => '5d0b2aed7a7149a9aae97d33f6c23b83')
4
+ end
5
+
6
+ Given /^I have a user$/ do
7
+ VCR.use_cassette('user/all', :record => :new_episodes) do
8
+ @user = @big_door.get_end_user.first
9
+ end
10
+ end
11
+
12
+ Given /^I have a "([^"]*)" named "([^"]*)"$/ do |action, name|
13
+ if action == "NamedTransaction"
14
+ VCR.use_cassette("NamedTransaction/specific", :record => :new_episodes) do
15
+ @transaction = BigDoor::NamedTransaction.find(:pub_title => name)
16
+ end
17
+ elsif action == 'NamedTransactionGroup'
18
+ VCR.use_cassette("named_transaction_group/specific", :record => :new_episodes) do
19
+ @grp_trans = BigDoor::NamedTransactionGroup.find(:pub_title__startswith => name)
20
+ end
21
+ @grp_trans.should_not eql(nil)
22
+ end
23
+ end
24
+
25
+ # Whens
26
+ When /^I make a request for "([^\"]*)"$/ do |arg1|
27
+ VCR.use_cassette('award/award_summary', :record => :new_episodes) do
28
+ @response = @big_door.get_award_summary
29
+ end
30
+ end
31
+
32
+ When /^I create a user named "([^\"]*)"$/ do |name|
33
+ VCR.use_cassette('user/new', :record => :new_episodes) do
34
+ @response = BigDoor::User.find(name)
35
+ end
36
+ @response.class.should eql(BigDoor::User)
37
+ end
38
+
39
+ When /^I check the user's "([^\"]*)"$/ do |action|
40
+ VCR.use_cassette('user/all', :record => :new_episodes) do
41
+ @response = @user.send action
42
+ end
43
+ end
44
+
45
+ When /^I add "([^\"]*)" points to users's account$/ do |points|
46
+ VCR.use_cassette('user/currency_balance', :match_requests_on => {:path => /end_user\/#{@user.end_user_login}\/currency_balance/}) do
47
+ @old_points = @user.get_currency_balance(@currency)
48
+ end
49
+
50
+ VCR.use_cassette('user/add_points', :record => :all, :match_requests_on => {:path => /named_transaction_group\/#{@grp_trans.id}\/execute\/#{@user.end_user_login}/}) do
51
+ @user.add_points(@grp_trans, points.to_i)
52
+ end
53
+ end
54
+
55
+ # Thens
56
+ Then /^I should have "([^\"]*)" new user$/ do |num|
57
+ @response = @big_door.get_end_user
58
+ end
59
+
60
+ Then /^I should receive a "([^\"]*)" object$/ do |received_object|
61
+ if @response.is_a? Array
62
+ @response.first.class.should eql(received_object.constantize)
63
+ else
64
+ @response.class.should eql(eval(received_object))
65
+ end
66
+ end
67
+
68
+ Then /^I should receive a "([^\"]*)" response code$/ do |response_code|
69
+ @response.code.should eql(response_code.to_i)
70
+ end
71
+
72
+ Then /^the response should be "([^"]*)"$/ do |arg1|
73
+ @response.should eql(eval(arg1))
74
+ end
75
+
76
+ Then /^their account should have "([^\"]*)" more points$/ do |points|
77
+ new_points = {}
78
+ VCR.use_cassette('user/new_currency_balance', :match_requests_on => {:path => /end_user\/#{@user.end_user_login}\/currency_balance/}) do
79
+ new_points = @user.get_currency_balance(@currency)
80
+ end
81
+
82
+ new_points["curr_balance"].to_i.should eql(@old_points['curr_balance'].to_i + points.to_i)
83
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'big-door'
3
+ require 'ruby-debug'
4
+ require 'spec/expectations'
5
+ require 'spec/support/vcr.rb'
data/lib/big-door.rb ADDED
@@ -0,0 +1,127 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'json'
4
+ require 'uri'
5
+ require 'cgi'
6
+ require 'uuidtools'
7
+
8
+ module BigDoor
9
+ BASE_URL = 'http://api.bigdoor.com'.freeze
10
+ BASE_URI = 'api/publisher'.freeze
11
+
12
+ class BigDoorError < StandardError; end
13
+
14
+ module ClassMethods
15
+ def self.included(base)
16
+ base.extend ClassMethods
17
+ end
18
+
19
+ def self.app_key
20
+ @app_key
21
+ end
22
+
23
+ def self.secret_key
24
+ @secret_key
25
+ end
26
+
27
+ def parse_out_classes(content)
28
+ output = []
29
+ unless content.is_a? Array
30
+ content = Array[content]
31
+ end
32
+ content.each do |result|
33
+ begin
34
+ output << case result["resource_name"]
35
+ when 'end_user'
36
+ User.new(result)
37
+ when 'currency'
38
+ Currency.new(result)
39
+ when 'named_transaction'
40
+ NamedTransaction.new(result)
41
+ when 'named_transaction_group'
42
+ NamedTransactionGroup.new(result)
43
+ else
44
+ result
45
+ end
46
+ rescue
47
+ debugger
48
+ end
49
+ end
50
+
51
+ output.length == 1 ? output.first : output
52
+ end
53
+
54
+ def perform_request(*args)
55
+ request_type, action, args = args
56
+ raise BigDoorError, "Unknown request type" unless ['get', 'post', 'put', 'delete'].include? request_type
57
+
58
+ query = args
59
+ params = {}
60
+ query = {} if (query.is_a? Array and query.empty?) or query.nil?
61
+ action << '/' + query.delete(:id).to_s if query.has_key? :id
62
+
63
+ if ['post', 'put'].include? request_type
64
+ params[:body] = query
65
+ params[:body][:time] = "%.2f" % Time.now.to_f
66
+ params[:body][:token] = SecureRandom.hex
67
+ query = {}
68
+ end
69
+
70
+ path = [BASE_URI, ClassMethods.app_key, action].join('/')
71
+ params[:query] = query
72
+ params[:query][:time] = params[:body][:time] rescue "%.2f" % Time.now.to_f
73
+ params[:query][:sig] = calculate_sha2_hash(path, params)
74
+ params[:query][:format] = 'json'
75
+ url = [BASE_URL, path].join('/')
76
+ parse_response(BigDoor::Request.send(request_type, url, params))
77
+ # BigDoor::Request.send(request_type, url, params)
78
+ end
79
+
80
+ private
81
+ def parse_response(response)
82
+ if response.response.class.ancestors.include? Net::HTTPClientError
83
+ raise BigDoorError, "#{response.response.code} #{response.response.message} - #{response.headers['bdm-reason-phrase'].to_s}"
84
+ end
85
+
86
+ if response.parsed_response.is_a? Numeric
87
+ response_code = BigDoor::ResponseCodes.find response.parsed_response
88
+ if response_code[:is_error]
89
+ raise BigDoorError, "#{response_code[:code]} #{response_code[:response_condition]} - #{response_code[:reason_phrase]}"
90
+ else
91
+ return true
92
+ end
93
+ elsif response.parsed_response.first.is_a? Array
94
+ content = response.parsed_response.first
95
+ else
96
+ content = ([] << response.parsed_response.first)
97
+ end
98
+ parse_out_classes(content)
99
+ end
100
+
101
+ def calculate_sha2_hash(path, query)
102
+ path = '/' + path
103
+ Digest::SHA2.new(bitlen = 256).update(path + concat_query(query[:query]) + concat_query(query[:body]) + ClassMethods.secret_key).to_s
104
+ end
105
+
106
+ def concat_query(query)
107
+ str = ''
108
+ unless query.nil?
109
+ query.keys.map(&:to_s).sort.each do |key|
110
+ str << key.to_s + query[key.to_sym].to_s unless [:sig, :format].include?(key)
111
+ end
112
+ end
113
+ str
114
+ end
115
+
116
+ def to_url_params(hash)
117
+ elements = []
118
+ hash.each_pair do |key, val|
119
+ elements << "#{CGI::escape(key.to_s)}=#{CGI::escape(val.to_s)}"
120
+ end
121
+ elements.join('&')
122
+ end
123
+ end
124
+ end
125
+
126
+ directory = File.expand_path(File.dirname(__FILE__))
127
+ Dir[File.join(directory, "big-door", "*.rb").to_s].each {|file| require file }
@@ -0,0 +1,45 @@
1
+ module BigDoor
2
+ class Base
3
+ include ClassMethods
4
+
5
+ def initialize(*args)
6
+ options = args.last
7
+ ClassMethods.module_eval do
8
+ instance_variable_set('@app_key', options[:app_key])
9
+ end
10
+ ClassMethods.module_eval do
11
+ instance_variable_set('@secret_key', options[:secret_key])
12
+ end
13
+ end
14
+
15
+ # def get_award_summary
16
+ # perform_get('award_summary')
17
+ # end
18
+ #
19
+ # def get_transaction_summary
20
+ # perform_get('transaction_summary')
21
+ # end
22
+ #
23
+ # def get_level_summary
24
+ # perform_get('level_summary')
25
+ # end
26
+ #
27
+ # def get_good_summary
28
+ # perform_get('good_summary')
29
+ # end
30
+ #
31
+ # def get_currency_type
32
+ # perform_get('currency_type')
33
+ # end
34
+ #
35
+ # def get_currency
36
+ # perform_get('currency')
37
+ # end
38
+
39
+ def method_missing(name, *args)
40
+ name, request_type, method_name = name.to_s.match(/(put|post|get|delete)_(.+)/).to_a
41
+ super(name, args) if name.nil? or request_type.nil? or method_name.nil?
42
+ perform_request request_type, method_name, args.first
43
+ end
44
+ end
45
+ end