tangocard 0.1.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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDYyMGMwNGRhZmQ4ZjlhOGFjZjcwZDNlOTRhZjBkOWNiYjQ5M2IyMA==
5
+ data.tar.gz: !binary |-
6
+ YjZiYTE0ZTNkZGNiNzVlY2NkZTk4YTMxZWJjNzgwNzM5MTI5NjE0MA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OGVjODkzNWM1NWMwZTNkYzI2ZWY4ZjZiNzMzOWI4Zjc5YzFjMjNiOGJkNzFl
10
+ MmQyOWRmNDJlNjgyODcyZjNhNzFmM2M2ZTY0YjcwODJhNzM2ZmE3MTJhMjBi
11
+ NmVhMTE3MzUwODI3OWY2NzQyNDI5NTRhNmFiNTVjYTMxNmNkODA=
12
+ data.tar.gz: !binary |-
13
+ Y2Q1N2QzNTI1M2EyNWY5N2UyYzU4NWZlZGI0NmY4YTllMDM4YmY5ZGYxZjNk
14
+ NTNjMjIzMjNhMjE0ZTkyZTY4NTg4OTUwMWE5Yzk0NzRiNmIzZjA0ZTczYzE0
15
+ MDE0MWE2YmIyYjY5NTAzZDdjYmFiYzFhOGZhYThjMzhkYzYxNWI=
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Raphael Crawford-Marks and Smartly, Inc.
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.
@@ -0,0 +1,10 @@
1
+ = Tangocard
2
+
3
+ Ruby Wrapper for Tango Card RaaS API.
4
+
5
+ Tango Card provides a RaaS API for developers (https://github.com/tangocarddev/RaaS). This gem provides commonsense Ruby
6
+ objects to wrap the JSON endpoints of the RaaS API.
7
+
8
+ This project is developed and maintained by Smartly, Inc. - makers of http://bonus.ly.
9
+
10
+ This project uses the MIT-LICENSE.
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Tangocard'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,37 @@
1
+ require 'httparty'
2
+ require 'money'
3
+ require 'ostruct'
4
+
5
+ module Tangocard
6
+ class Configuration
7
+ attr_accessor :name, :key, :base_uri, :default_brands, :local_images, :sku_blacklist
8
+
9
+ def initialize
10
+ self.name = nil
11
+ self.key = nil
12
+ self.base_uri = "https://sandbox.tangocard.com"
13
+ self.default_brands = ['Tango Card']
14
+ self.local_images = {}
15
+ self.sku_blacklist = []
16
+ end
17
+ end
18
+
19
+ def self.configuration
20
+ @configuration ||= Configuration.new
21
+ end
22
+
23
+ def self.configure
24
+ yield(configuration) if block_given?
25
+ end
26
+ end
27
+
28
+ require 'tangocard/response'
29
+ require 'tangocard/raas'
30
+ require 'tangocard/account'
31
+ require 'tangocard/account_create_failed_exception'
32
+ require 'tangocard/account_not_found_exception'
33
+ require 'tangocard/brand'
34
+ require 'tangocard/order'
35
+ require 'tangocard/order_create_failed_exception'
36
+ require 'tangocard/order_not_found_exception'
37
+ require 'tangocard/reward'
@@ -0,0 +1,57 @@
1
+ # Wrapper for Tangocard RaaS Account
2
+ # https://github.com/tangocarddev/RaaS#account-resources
3
+ class Tangocard::Account
4
+ attr_reader :customer, :identifier, :email, :available_balance
5
+
6
+ private_class_method :new
7
+
8
+ # parsed_response={"success"=>true, "account"=>{"identifier"=>"test1", "email"=>"test@test.com", "customer"=>"bonusly", "available_balance"=>0}}
9
+ def self.find(customer, identifier)
10
+ response = Tangocard::Raas.show_account({'customer' => customer, 'identifier' => identifier})
11
+ if response.success?
12
+ new(response.parsed_response['account'])
13
+ else
14
+ raise Tangocard::AccountNotFoundException, "Tangocard - Error finding account: #{response.error_message}"
15
+ end
16
+ end
17
+
18
+ def self.create(customer, identifier, email)
19
+ response = Tangocard::Raas.create_account({'customer' => customer, 'identifier' => identifier, 'email' => email})
20
+ if response.success?
21
+ new(response.parsed_response['account'])
22
+ else
23
+ raise Tangocard::AccountCreateFailedException, "Tangocard - Error finding account: #{response.error_message}"
24
+ end
25
+ end
26
+
27
+ def self.find_or_create(customer, identifier, email)
28
+ begin
29
+ find(customer, identifier)
30
+ rescue Tangocard::AccountNotFoundException => e
31
+ create(customer, identifier, email)
32
+ end
33
+ end
34
+
35
+ # {"identifier"=>"test1", "email"=>"test@test.com", "customer"=>"bonusly", "available_balance"=>0}
36
+ def initialize(params)
37
+ @customer = params['customer']
38
+ @email = params['email']
39
+ @identifier = params['identifier']
40
+ @available_balance = params['available_balance'].to_i
41
+ end
42
+
43
+ def balance
44
+ @available_balance
45
+ end
46
+
47
+ def fund!(amount, client_ip, credit_card)
48
+ params = {
49
+ 'amount' => amount,
50
+ 'client_ip' => client_ip,
51
+ 'credit_card' => credit_card,
52
+ 'customer' => customer,
53
+ 'account_identifier' => identifier
54
+ }
55
+ Tangocard::Raas.fund_account(params)
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ class Tangocard::AccountCreateFailedException < Exception
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ class Tangocard::AccountNotFoundException < Exception
2
+
3
+ end
@@ -0,0 +1,40 @@
1
+ # Documentation: https://github.com/tangocarddev/RaaS
2
+ class Tangocard::Brand
3
+ attr_reader :description, :rewards
4
+
5
+ def self.all
6
+ result = Tangocard::Raas.rewards_index.parsed_response
7
+ result['brands'].map{|p| Tangocard::Brand.new(p)}
8
+ end
9
+
10
+ def self.default_brands
11
+ self.all.select{|b| Tangocard.configuration.default_brands.include?(b.description)}
12
+ end
13
+
14
+ def self.find(brand_name)
15
+ self.all.select{|b| b.description == brand_name}.first
16
+ end
17
+
18
+ def initialize(params)
19
+ @description = params['description']
20
+ @image_url = params['image_url']
21
+ @rewards = params['rewards'].map{|p| Tangocard::Reward.new(p)}
22
+ end
23
+
24
+ # Some brands don't have logo images provided by the API, so we do this.
25
+ def image_url
26
+ Tangocard.configuration.local_images[description] || @image_url
27
+ end
28
+
29
+ def purchasable_rewards(balance_in_cents)
30
+ rewards.select{|r| r.purchasable?(balance_in_cents) && !Tangocard.configuration.sku_blacklist.include?(r.sku)}
31
+ end
32
+
33
+ def has_purchasable_rewards?(balance_in_cents)
34
+ purchasable_rewards(balance_in_cents).any?
35
+ end
36
+
37
+ def variable_price?
38
+ rewards.select{|r| r.variable_price? }.any?
39
+ end
40
+ end
@@ -0,0 +1,95 @@
1
+ # Wrapper for Tangocard RaaS Order
2
+ # https://github.com/tangocarddev/RaaS#order-resources
3
+ class Tangocard::Order
4
+ attr_reader :order_id,
5
+ :account_identifier,
6
+ :customer,
7
+ :sku,
8
+ :amount,
9
+ :reward_message,
10
+ :reward_from,
11
+ :delivered_at,
12
+ :recipient
13
+
14
+ private_class_method :new
15
+
16
+ #{
17
+ # "success"=>true,
18
+ # "order"=> {
19
+ # "order_id"=>"113-08258652-15",
20
+ # "account_identifier"=>"ElliottTest",
21
+ # "customer"=>"ElliottTest",
22
+ # "sku"=>"APPL-E-1500-STD",
23
+ # "amount"=>1500,
24
+ # "reward_message"=>"testing",
25
+ # "reward_subject"=>"RaaS Sandbox Test",
26
+ # "reward_from"=>"Elliott",
27
+ # "delivered_at"=>"2013-08-15T17:42:18+00:00",
28
+ # "recipient"=> {
29
+ # "name"=>"Elliott",
30
+ # "email"=>"elliott@tangocard.com"
31
+ # },
32
+ # "reward"=> {
33
+ # "token"=>"520d12fa655b54.34581245",
34
+ # "number"=>"1111111111111256"
35
+ # }
36
+ # }
37
+ #}
38
+
39
+ def self.all(params = {})
40
+ response = Tangocard::Raas.orders_index(params)
41
+ if response.success?
42
+ response.parsed_response['orders'].map{|o| new(o)}
43
+ else
44
+ []
45
+ end
46
+ end
47
+
48
+ def self.find(order_id)
49
+ response = Tangocard::Raas.show_order({'order_id' => order_id})
50
+ if response.success?
51
+ new(response.parsed_response['order'])
52
+ else
53
+ raise Tangocard::OrderNotFoundException, "Order (#{order_id}) not found. Error message: #{response.error_message}"
54
+ end
55
+ end
56
+
57
+ def self.create(params)
58
+ response = Tangocard::Raas.create_order(params)
59
+ if response.success?
60
+ new(response.parsed_response['order'])
61
+ else
62
+ raise Tangocard::OrderCreateFailedException, "#{response.error_message}"
63
+ end
64
+ end
65
+
66
+ def initialize(params)
67
+ @order_id = params['order_id']
68
+ @account_identifier = params['account_identifier']
69
+ @customer = params['customer']
70
+ @sku = params['sku']
71
+ @amount = params['amount']
72
+ @reward_message = params['reward_message']
73
+ @reward_subject = params['reward_subject']
74
+ @reward_from = params['reward_from']
75
+ @delivered_at = params['delivered_at']
76
+ @recipient = params['recipient']
77
+ @reward = params['reward']
78
+ end
79
+
80
+ def reward
81
+ if @reward.nil?
82
+ begin
83
+ @reward = Tangocard::Order.find(self.order_id).reward
84
+ rescue Tangocard::OrderNotFoundException => e
85
+ nil
86
+ end
87
+ else
88
+ @reward
89
+ end
90
+ end
91
+
92
+ def identifier
93
+ @account_identifier
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ class Tangocard::OrderCreateFailedException < Exception
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ class Tangocard::OrderNotFoundException < Exception
2
+
3
+ end
@@ -0,0 +1,50 @@
1
+ # Documentation: https://github.com/tangocarddev/RaaS
2
+ class Tangocard::Raas
3
+ include HTTParty
4
+ base_uri Tangocard.configuration.base_uri
5
+
6
+ # https://github.com/tangocarddev/RaaS/blob/master/account_create.schema.json
7
+ def self.create_account(params)
8
+ Tangocard::Response.new(post('/raas/v1/accounts', {:body => params.to_json}.merge(basic_auth_param)))
9
+ end
10
+
11
+ def self.show_account(params)
12
+ Tangocard::Response.new(get("/raas/v1/accounts/#{params['customer']}/#{params['identifier']}", basic_auth_param))
13
+ end
14
+
15
+ # https://github.com/tangocarddev/RaaS/blob/master/fund_create.schema.json
16
+ def self.fund_account(params)
17
+ Tangocard::Response.new(post('/raas/v1/funds', {:body => params.to_json}.merge(basic_auth_param)))
18
+ end
19
+
20
+ def self.rewards_index
21
+ Tangocard::Response.new(get('/raas/v1/rewards', basic_auth_param))
22
+ end
23
+
24
+ # https://github.com/tangocarddev/RaaS/blob/master/order_create.schema.json
25
+ def self.create_order(params)
26
+ Tangocard::Response.new(post('/raas/v1/orders', {:body => params.to_json}.merge(basic_auth_param)))
27
+ end
28
+
29
+ def self.show_order(params)
30
+ Tangocard::Response.new(get("/raas/v1/orders/#{params['order_id']}", basic_auth_param))
31
+ end
32
+
33
+ def self.orders_index(params = {})
34
+ query_string = ""
35
+ if params.any?
36
+ query_string = "?"
37
+ params.keys.each_with_index do |k,i|
38
+ query_string += "&" unless i == 0
39
+ query_string += "#{k}=#{params[k]}"
40
+ end
41
+ end
42
+ Tangocard::Response.new(get("/raas/v1/orders#{query_string}", basic_auth_param))
43
+ end
44
+
45
+ private
46
+
47
+ def self.basic_auth_param
48
+ {:basic_auth => {:username => Tangocard.configuration.name, :password => Tangocard.configuration.key}}
49
+ end
50
+ end
@@ -0,0 +1,29 @@
1
+ # Tangocard RaaS Response Wrapper
2
+ # https://github.com/tangocarddev/RaaS#responses
3
+ class Tangocard::Response
4
+ attr_reader :raw_response
5
+
6
+ def initialize(raw_response)
7
+ @raw_response = raw_response
8
+ end
9
+
10
+ def parsed_response
11
+ raw_response.parsed_response
12
+ end
13
+
14
+ def code
15
+ raw_response.code
16
+ end
17
+
18
+ def success?
19
+ parsed_response['success']
20
+ end
21
+
22
+ def error_message
23
+ parsed_response['error_message']
24
+ end
25
+
26
+ def invalid_inputs
27
+ parsed_response['invalid_inputs']
28
+ end
29
+ end
@@ -0,0 +1,34 @@
1
+ # Documentation: https://github.com/tangocarddev/RaaS
2
+ class Tangocard::Reward
3
+ attr_reader :description, :sku, :currency_type, :unit_price, :available, :min_price, :max_price
4
+
5
+ def initialize(params)
6
+ @description = params['description']
7
+ @sku = params['sku']
8
+ @currency_type = params['currency_type']
9
+ @unit_price = params['unit_price'].to_i
10
+ @available = params['available']
11
+ @min_price = params['min_price'].to_i
12
+ @max_price = params['max_price'].to_i
13
+ end
14
+
15
+ def variable_price?
16
+ self.unit_price == -1
17
+ end
18
+
19
+ def purchasable?(balance_in_cents)
20
+ return false unless available
21
+
22
+ if variable_price?
23
+ min_price <= balance_in_cents
24
+ else
25
+ unit_price <= balance_in_cents
26
+ end
27
+ end
28
+
29
+ def price_in_usd(field_name)
30
+ return nil unless [:min_price, :max_price, :unit_price].include?(field_name)
31
+
32
+ Money.new(self.send(field_name), "USD")
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Tangocard
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tangocard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Raphael Crawford-Marks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.11.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.11.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: money
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 5.1.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 5.1.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ! "Tango Card provides a RaaS API for developers (https://github.com/tangocarddev/RaaS).
70
+ This gem\n provides commonsense Ruby objects to wrap the JSON
71
+ endpoints of the RaaS API."
72
+ email:
73
+ - raphael@bonus.ly
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - lib/tangocard/account.rb
79
+ - lib/tangocard/account_create_failed_exception.rb
80
+ - lib/tangocard/account_not_found_exception.rb
81
+ - lib/tangocard/brand.rb
82
+ - lib/tangocard/order.rb
83
+ - lib/tangocard/order_create_failed_exception.rb
84
+ - lib/tangocard/order_not_found_exception.rb
85
+ - lib/tangocard/raas.rb
86
+ - lib/tangocard/response.rb
87
+ - lib/tangocard/reward.rb
88
+ - lib/tangocard/version.rb
89
+ - lib/tangocard.rb
90
+ - MIT-LICENSE
91
+ - Rakefile
92
+ - README.rdoc
93
+ homepage: http://bonus.ly
94
+ licenses: []
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.1.4
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Ruby Wrapper for Tango Card RaaS API.
116
+ test_files: []