promise_pay 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: 657fe7151abb7b6dd76fbdb0e610697bf0f4e831
4
+ data.tar.gz: 2244b5986a100793fae53900ea53aebcc39ade4c
5
+ SHA512:
6
+ metadata.gz: bee824f5f09fb802a33f336d8c6e9434ae1b195f2eddf0d61d7838c4ed6d2a2a6804e696afcc6341a841a856057bfea3b12c5ccad931795f97a0fa9c51fe80ba
7
+ data.tar.gz: 7d3bed8cbd8f7019952ebb1d028dddcac849e16aacf1910f42c7f8aa126e56e3e058688b82c790f0a98b8ac1cba209151cf52f6d7e51b73dfd01beab0ad22892
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in promise_pay.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Liam Norton
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/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # PromisePay
2
+
3
+ PromisePay API calls wrapped in a gem
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'promise_pay'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install promise_pay
18
+
19
+ Generate your PromizePay API key and the rails promise_pay initializer:
20
+
21
+ $ rails generate promise_pay:init EMAIL PASSWORD
22
+
23
+ You're set to go!
24
+
25
+ ## Extra Info
26
+
27
+ To use PromisePay's test API, set the following in `config/initializers/promise_pay.rb`:
28
+
29
+ PromisePay.env = :test
30
+
31
+ Along with your test environments api user/key. You can generate all this by running:
32
+
33
+ $ rails generate promise_pay:init EMAIL PASSWORD --test
34
+
35
+ Note this will overwrite anything in `config/initializers/promise_pay.rb`
36
+
37
+ ## Usage
38
+
39
+ ```ruby
40
+ # A new PromisePay::Session that generates a session token
41
+ session = PromisePay::Session.new(session_params)
42
+ session.amount = 10
43
+ session.token => nil
44
+ session.request_token => "8cfd23e3-196e-4a45-ab16-d1213094871e"
45
+ session.token => "8cfd23e3-196e-4a45-ab16-d1213094871e"
46
+
47
+ # Query PromisePay for a user (12345) returning a PromisePay::User object
48
+ user = PromisePay::User.find(12345)
49
+ user.email => "email@addr"
50
+
51
+ # Query PromisePay for an item (1s345) returning a PromisePay::Item object
52
+ item = PromisePay::Item.find("1s345")
53
+ item.amount => 10
54
+
55
+ ```
56
+
57
+ ## TODO:
58
+
59
+ 1. Write .find_all methods
60
+ 2. Move all errors into shared file
61
+ 3. Don't enforce full params match on session request (?)
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it ( http://github.com/<my-github-username>/promise_pay/fork )
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+
5
+ desc "Run all specs"
6
+ RSpec::Core::RakeTask.new('spec')
@@ -0,0 +1,43 @@
1
+ require "rails/generators/base"
2
+ require "promise_pay"
3
+
4
+ module PromisePay
5
+ module Generators
6
+ class InitGenerator < Rails::Generators::Base
7
+ source_root File.expand_path("../../templates", __FILE__)
8
+
9
+ argument :email, type: :string
10
+ argument :password, type: :string
11
+
12
+ class_option :test, type: :boolean, default: :false, description: "Initalize to test.api.promisepay.com"
13
+
14
+ desc "Request PromisePay API key and setup initializer"
15
+
16
+ def env
17
+ @env ||= options.test? ? :test : :production
18
+ end
19
+
20
+ def marketplace
21
+ @marketplace ||= (
22
+ PromisePay.env = env
23
+ PromisePay::Marketplace.new(user: email, password: password)
24
+ )
25
+ end
26
+
27
+ def token
28
+ @token ||= (
29
+ begin
30
+ marketplace.request_token
31
+ rescue PromisePay::RequestError
32
+ puts "WARNING: token generation failed (Check your credentials)"
33
+ "token"
34
+ end
35
+ )
36
+ end
37
+
38
+ def copy_initializer
39
+ template "promise_pay_initializer.rb", "config/initializers/promise_pay.rb"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ require "promise_pay"
2
+
3
+ PromisePay.api_user = "<%= email %>"
4
+ PromisePay.api_key = "<%= token %>"
5
+ PromisePay.env = :<%= env %>
@@ -0,0 +1,15 @@
1
+ module PromisePay
2
+ class Collection
3
+ attr_reader :collection_class
4
+
5
+ def initialize(collection_class)
6
+ @collection_class = collection_class
7
+ end
8
+
9
+ def parse
10
+ p collection_class.new
11
+ tmp = collection_class.new.fetch_all
12
+ p tmp
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,72 @@
1
+ require "promise_pay/lib/dynamic_accessors"
2
+ require "json"
3
+
4
+ module PromisePay
5
+ class Item
6
+ attr_reader :id
7
+
8
+ def initialize(id = nil, options = {})
9
+ @id = id
10
+ assign_instance_variables({'item' => options}) unless options.empty?
11
+ end
12
+
13
+ class << self
14
+ def find(id)
15
+ new(id).find
16
+ end
17
+
18
+ def find_all
19
+ new.find_all
20
+ end
21
+ end
22
+
23
+ def find
24
+ assign_instance_variables(resource_result)
25
+ self
26
+ end
27
+
28
+ def find_all
29
+ resource_result.map do |result|
30
+ self.class.new(nil, result)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def resource_result
37
+ request = PromisePay::Request.new(path: api_resource)
38
+ response = request.execute
39
+ JSON.parse(response)["items"]
40
+ end
41
+
42
+ def api_resource
43
+ "items/#{id}"
44
+ end
45
+
46
+ def assign_instance_variables(result)
47
+ result.each do |attribute, value|
48
+ if value.is_a?(Hash)
49
+ value.each { |att, val| initialize_property(att, val) }
50
+ else
51
+ initialize_property(attribute, value)
52
+ end
53
+ end
54
+ self
55
+ end
56
+
57
+ def initialize_property(attribute, value)
58
+ attribute = attribute.gsub(/s$/, '_id') if ["buyers","sellers"].include? attribute
59
+ Lib::DynamicAccessors.define_accessor(attribute, value, self) unless accessor_defined?(attribute)
60
+ set_property(attribute, value)
61
+ end
62
+
63
+ def accessor_defined?(attribute)
64
+ respond_to?(attribute) && respond_to?("#{attribute}=")
65
+ end
66
+
67
+ def set_property(attribute, value)
68
+ setter_method = "#{attribute}="
69
+ self.send(setter_method, value)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,42 @@
1
+ module PromisePay
2
+ module Lib
3
+ module DynamicAccessors
4
+ class << self
5
+ def define_accessor(attribute, value, object)
6
+ klass = object.class
7
+ if attribute.to_s.end_with? "_at"
8
+ define_date_based_accessors(attribute, value, klass)
9
+ else
10
+ define_standard_accessors(attribute, value, klass)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def define_date_based_accessors(attribute, value, klass)
17
+ klass.class_eval %Q"
18
+ def #{attribute}=(value)
19
+ @#{attribute} = value.nil? ? nil : Time.parse(value).to_i
20
+ end
21
+
22
+ def #{attribute}
23
+ @#{attribute}.nil? ? nil : Time.at(@#{attribute})
24
+ end
25
+ "
26
+ end
27
+
28
+ def define_standard_accessors(attribute, value, klass)
29
+ klass.class_eval %Q"
30
+ def #{attribute}=(value)
31
+ @#{attribute} = value
32
+ end
33
+
34
+ def #{attribute}
35
+ @#{attribute}
36
+ end
37
+ "
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,33 @@
1
+ require "json"
2
+
3
+ module PromisePay
4
+ class Marketplace
5
+
6
+ attr_reader :token
7
+
8
+ def initialize(options = {})
9
+ @user = options.fetch :user
10
+ @password = options.fetch :password
11
+ end
12
+
13
+ def request_token
14
+ response = PromisePay::Request.new(
15
+ path: api_resource,
16
+ user: user,
17
+ password: password
18
+ ).execute
19
+
20
+ @token = JSON.parse(response)["token"]
21
+ token
22
+ end
23
+
24
+ private
25
+
26
+ def api_resource
27
+ "request_token"
28
+ end
29
+
30
+ attr_reader :user
31
+ attr_reader :password
32
+ end
33
+ end
@@ -0,0 +1,54 @@
1
+ require "rest_client"
2
+
3
+ module PromisePay
4
+ class Request
5
+
6
+ def initialize(options = {})
7
+ @path = options.fetch :path
8
+ @user = options.fetch :user, PromisePay.api_user
9
+ @password = options.fetch :password, PromisePay.api_key
10
+ @request = build_request
11
+ end
12
+
13
+ def execute
14
+ begin
15
+ response = request.execute
16
+ rescue RestClient::Unauthorized, RestClient::BadRequest => e
17
+ raise RequestError, e.message
18
+ end
19
+
20
+ response
21
+ end
22
+
23
+ private
24
+
25
+ def build_request
26
+ RestClient::Request.new(
27
+ method: :get,
28
+ url: endpoint,
29
+ user: user,
30
+ password: password,
31
+ headers: { accept: :json, content_type: :json }
32
+ )
33
+ end
34
+
35
+ def endpoint
36
+ host + path
37
+ end
38
+
39
+ def host
40
+ if PromisePay.env && PromisePay.env == :test
41
+ TEST_HOST
42
+ else
43
+ API_HOST
44
+ end
45
+ end
46
+
47
+ attr_reader :path
48
+ attr_reader :user
49
+ attr_reader :password
50
+ attr_reader :request
51
+ end
52
+
53
+ class RequestError < StandardError; end
54
+ end
@@ -0,0 +1,106 @@
1
+ require "json"
2
+ require 'active_support/core_ext/object'
3
+
4
+ module PromisePay
5
+ class Session
6
+
7
+ attr_reader :token
8
+ attr_accessor :current_user_id
9
+ attr_accessor :currency
10
+ attr_accessor :item_name
11
+ attr_accessor :amount
12
+ attr_accessor :seller_lastname
13
+ attr_accessor :seller_firstname
14
+ attr_accessor :buyer_lastname
15
+ attr_accessor :buyer_firstname
16
+ attr_accessor :seller_email
17
+ attr_accessor :buyer_email
18
+ attr_accessor :external_item_id
19
+ attr_accessor :external_seller_id
20
+ attr_accessor :external_buyer_id
21
+ attr_accessor :fee_ids
22
+ attr_accessor :payment_type_id
23
+
24
+ def initialize(options = {})
25
+ @current_user_id = options.fetch :current_user_id, nil
26
+ @currency = options.fetch :currency, nil
27
+ @item_name = options.fetch :item_name, nil
28
+ @amount = options.fetch :amount, nil
29
+ @seller_lastname = options.fetch :seller_lastname, nil
30
+ @seller_firstname = options.fetch :seller_firstname, nil
31
+ @buyer_lastname = options.fetch :buyer_lastname, nil
32
+ @buyer_firstname = options.fetch :buyer_firstname, nil
33
+ @seller_email = options.fetch :seller_email, nil
34
+ @buyer_email = options.fetch :buyer_email, nil
35
+ @external_item_id = options.fetch :external_item_id, nil
36
+ @external_seller_id = options.fetch :external_seller_id, nil
37
+ @external_buyer_id = options.fetch :external_buyer_id, nil
38
+ @fee_ids = options.fetch :fee_ids, nil
39
+ @payment_type_id = options.fetch :payment_type_id, nil
40
+ end
41
+
42
+ def request_token
43
+ enforce_valid_params!
44
+
45
+ request = PromisePay::Request.new(path: api_resource)
46
+ response = request.execute
47
+
48
+ @token = JSON.parse(response)["token"]
49
+ token
50
+ end
51
+
52
+ private
53
+
54
+ def api_resource
55
+ "request_session_token?#{query_string}"
56
+ end
57
+
58
+ def query_string
59
+ {
60
+ current_user_id: current_user_id,
61
+ currency: currency,
62
+ item_name: item_name,
63
+ amount: amount,
64
+ seller_lastname: seller_lastname,
65
+ seller_firstname: seller_firstname,
66
+ buyer_lastname: buyer_lastname,
67
+ buyer_firstname: buyer_firstname,
68
+ seller_email: seller_email,
69
+ buyer_email: buyer_email,
70
+ external_item_id: external_item_id,
71
+ external_seller_id: external_seller_id,
72
+ external_buyer_id: external_buyer_id,
73
+ fee_ids: fee_ids,
74
+ payment_type_id: payment_type_id
75
+ }.to_param
76
+ end
77
+
78
+ def enforce_valid_params!
79
+ required_params.each do |attr|
80
+ if self.send(attr).blank?
81
+ raise InvalidSessionRequest, "Missing #{attr} value"
82
+ end
83
+ end
84
+ end
85
+
86
+ def required_params
87
+ [
88
+ :current_user_id,
89
+ :currency,
90
+ :item_name,
91
+ :amount,
92
+ :seller_lastname,
93
+ :seller_firstname,
94
+ :buyer_lastname,
95
+ :buyer_firstname,
96
+ :seller_email,
97
+ :buyer_email,
98
+ :external_item_id,
99
+ :external_seller_id,
100
+ :external_buyer_id,
101
+ :fee_ids,
102
+ :payment_type_id
103
+ ]
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,67 @@
1
+ require "promise_pay/lib/dynamic_accessors"
2
+ require "json"
3
+
4
+ module PromisePay
5
+ class User
6
+ attr_reader :id
7
+
8
+ def initialize(id = nil, options = {})
9
+ @id = id
10
+ assign_instance_variables({'user' => options}) unless options.empty?
11
+ end
12
+
13
+ class << self
14
+ def find(id)
15
+ new(id).find
16
+ end
17
+
18
+ def find_all
19
+ new.find_all
20
+ end
21
+ end
22
+
23
+ def find
24
+ assign_instance_variables(resource_result)
25
+ self
26
+ end
27
+
28
+ def find_all
29
+ resource_result["users"].map do |result|
30
+ self.class.new(nil, result)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def resource_result
37
+ request = PromisePay::Request.new(path: api_resource)
38
+ response = request.execute
39
+ JSON.parse(response)
40
+ end
41
+
42
+ def api_resource
43
+ "users/#{id}"
44
+ end
45
+
46
+ def assign_instance_variables(result)
47
+ result["user"].each do |attribute, value|
48
+ initialize_property(attribute, value)
49
+ end
50
+ self
51
+ end
52
+
53
+ def initialize_property(attribute, value)
54
+ Lib::DynamicAccessors.define_accessor(attribute, value, self) unless accessor_defined?(attribute)
55
+ set_property(attribute, value)
56
+ end
57
+
58
+ def accessor_defined?(attribute)
59
+ respond_to?(attribute) && respond_to?("#{attribute}=")
60
+ end
61
+
62
+ def set_property(attribute, value)
63
+ setter_method = "#{attribute}="
64
+ self.send(setter_method, value)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module PromisePay
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ require "promise_pay/item"
2
+ require "promise_pay/marketplace"
3
+ require "promise_pay/request"
4
+ require "promise_pay/session"
5
+ require "promise_pay/user"
6
+ require "promise_pay/version"
7
+ require "promise_pay/collection"
8
+
9
+ module PromisePay
10
+ API_HOST = "https://secure.api.promisepay.com/"
11
+ TEST_HOST = "https://test.api.promisepay.com/"
12
+
13
+ class << self
14
+ attr_accessor :api_user
15
+ attr_accessor :api_key
16
+ attr_accessor :env
17
+ end
18
+
19
+ class InvalidRequest < StandardError; end
20
+ class InvalidSessionRequest < StandardError; end
21
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'promise_pay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "promise_pay"
8
+ spec.version = PromisePay::VERSION
9
+ spec.authors = ["Liam Norton"]
10
+ spec.email = ["liam.norton@flippa.com"]
11
+ spec.summary = %q{PromisePay gem}
12
+ spec.description = %q{PromisePay gem for API calls wrapped in Ruby}
13
+ spec.homepage = "https://github.com/iamliamnorton/promise_pay"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.5"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "rest_client"
25
+ spec.add_runtime_dependency "json"
26
+ end
@@ -0,0 +1,61 @@
1
+ require "spec_helper"
2
+
3
+ describe PromisePay::Item do
4
+ let(:request) { double("PromisePay::Request", execute: sample_response) }
5
+
6
+ before do
7
+ PromisePay::Request.stub(:new) { request }
8
+ end
9
+
10
+ describe ".find" do
11
+ let(:sample_response) { File.read("./spec/support/fixtures/item/find.json") }
12
+ let(:item_id) { "wef9834tg" }
13
+
14
+ it "returns a PromisePay::Item object" do
15
+ expect(described_class.find(item_id)).to be_a_kind_of PromisePay::Item
16
+ end
17
+
18
+ it "returned object has correctly assigned attributes" do
19
+ promise_pay_item = described_class.find(item_id)
20
+ expect(promise_pay_item.id).to eq "wef9834tg"
21
+ expect(promise_pay_item.name).to eq "ItemName"
22
+ expect(promise_pay_item.amount).to eq 10
23
+ expect(promise_pay_item.state).to eq "pending"
24
+ end
25
+
26
+ it "formats created_at and updated_at as Time" do
27
+ promise_pay_item = described_class.find(item_id)
28
+ expect(promise_pay_item.created_at).to be_a_kind_of Time
29
+ expect(promise_pay_item.updated_at).to be_a_kind_of Time
30
+ end
31
+
32
+ it "instantiates PromisePay::Request with the correct path" do
33
+ valid_path = "items/#{item_id}"
34
+ PromisePay::Request.should_receive(:new).with(path: valid_path)
35
+ described_class.find(item_id)
36
+ end
37
+ end
38
+
39
+ describe ".find_all" do
40
+ let(:sample_response) { File.read("./spec/support/fixtures/item/find_all.json") }
41
+
42
+ it "returns an array of PromisePay::Item objects" do
43
+ result = described_class.find_all
44
+ expect(result).to be_a_kind_of Array
45
+ expect(result.first).to be_a_kind_of PromisePay::Item
46
+ end
47
+
48
+ it "returned objects have correctly assigned attributes" do
49
+ promise_pay_user = described_class.find_all.first
50
+ expect(promise_pay_user.id).to eq "wef9834tg"
51
+ expect(promise_pay_user.name).to eq "ItemName"
52
+ expect(promise_pay_user.amount).to eq 10
53
+ end
54
+
55
+ it "instantiates PromisePay::Request with the correct path" do
56
+ valid_path = "items/"
57
+ PromisePay::Request.should_receive(:new).with(path: valid_path)
58
+ described_class.find_all
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe PromisePay::Marketplace do
4
+ let(:request) { double("RestClient::Request", execute: sample_response) }
5
+
6
+ describe ".request_token" do
7
+ let(:sample_response) { File.read("./spec/support/fixtures/token_generation.json") }
8
+ let(:user) { "test@email.com" }
9
+ let(:password) { "password" }
10
+
11
+ before do
12
+ PromisePay::Request.stub(:new) { request }
13
+ end
14
+
15
+ it "instantiates PromisePay::Request with the correct path" do
16
+ valid_path = "request_token"
17
+
18
+ PromisePay::Request.should_receive(:new).with(
19
+ path: valid_path,
20
+ user: user,
21
+ password: password
22
+ )
23
+
24
+ described_class.new(user: user, password: password).request_token
25
+ end
26
+
27
+ it "returns the generated marketplace token" do
28
+ described_class.new(user: user, password: password).request_token.should eq("123abc")
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ describe PromisePay::Request do
4
+ let(:request) { described_class.new(path: "users/") }
5
+
6
+ before do
7
+ PromisePay.api_user = "some@email"
8
+ PromisePay.api_key = "generated_key_123"
9
+ end
10
+
11
+ describe "#initialize" do
12
+ it "takes an mandatory path arguement" do
13
+ expect { described_class.new(user: "user@email.com", password: "password") }.
14
+ to raise_error KeyError
15
+ end
16
+
17
+ it "takes optional user and password arguements" do
18
+ expect(request).to be_an_instance_of PromisePay::Request
19
+ end
20
+
21
+ it "uses the test api when configured" do
22
+ expect { PromisePay.env = :test }.
23
+ to change { request.send(:endpoint).start_with? PromisePay::TEST_HOST }.
24
+ from(false).to(true)
25
+ end
26
+
27
+ it "builds a RestClient::Request" do
28
+ expect(request.send(:request)).to be_an_instance_of RestClient::Request
29
+ end
30
+ end
31
+
32
+ describe "#execute" do
33
+ it "calls the RestClient::Request to be exectued" do
34
+ rest_client_request = request.send(:request)
35
+ rest_client_request.should_receive(:execute)
36
+ request.execute
37
+ end
38
+
39
+ it "re-raises RestClient::Unauthorized exceptions" do
40
+ RestClient::Request.any_instance.stub(:execute) { raise RestClient::Unauthorized }
41
+ expect { request.execute }.to raise_error PromisePay::RequestError
42
+ end
43
+
44
+ it "re-raises RestClient::BadRequest exceptions" do
45
+ RestClient::Request.any_instance.stub(:execute) { raise RestClient::BadRequest }
46
+ expect { request.execute }.to raise_error PromisePay::RequestError
47
+ end
48
+
49
+ it "raises exception when no api credentials are present" do
50
+ PromisePay.api_user = nil
51
+ expect { request.execute }.to raise_error PromisePay::RequestError
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,61 @@
1
+ require "spec_helper"
2
+
3
+ describe PromisePay::Session do
4
+ valid_params = {
5
+ current_user_id: "1",
6
+ currency: "USD",
7
+ item_name: "ItemName",
8
+ amount: "10",
9
+ seller_firstname: "Alex",
10
+ seller_lastname: "Jones",
11
+ buyer_firstname: "Sam",
12
+ buyer_lastname: "Smith",
13
+ seller_email: "seller@email.com",
14
+ buyer_email: "buyer@email.com",
15
+ external_item_id: "wef9834tg",
16
+ external_seller_id: "1",
17
+ external_buyer_id: "2",
18
+ fee_ids: "3bfc26e3-093d-4f75-ac06-d2023294882b",
19
+ payment_type_id: "1"
20
+ }
21
+
22
+ let(:request) { double("RestClient::Request", execute: sample_response) }
23
+
24
+ describe ".request_token" do
25
+ let(:sample_response) { File.read("./spec/support/fixtures/token_generation.json") }
26
+ let(:session) { described_class.new(valid_params) }
27
+
28
+ context "with valid params" do
29
+ before do
30
+ PromisePay::Request.stub(:new) { request }
31
+ end
32
+
33
+ it "returns the generated session token from PromisePay" do
34
+ expect(session.request_token).to eq("123abc")
35
+ end
36
+
37
+ it "assigns the token to #token attribute" do
38
+ expect { session.request_token }.to change { session.token }.
39
+ from(nil).to("123abc")
40
+ end
41
+
42
+ it "instantiates PromisePay::Request with the correct path" do
43
+ valid_path = "request_session_token?#{valid_params.to_param}"
44
+ PromisePay::Request.should_receive(:new).with(path: valid_path)
45
+ session.request_token
46
+ end
47
+ end
48
+
49
+ context "with invalid params" do
50
+ valid_params.each do |param|
51
+ let(:invalid_params) { valid_params.reject { |k, v| k == param[0] } }
52
+ let(:session) { described_class.new(invalid_params) }
53
+
54
+ it "raises an error if #{param[0]} option is missing" do
55
+ expect { session.request_token }.
56
+ to raise_error PromisePay::InvalidSessionRequest
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,60 @@
1
+ require "spec_helper"
2
+
3
+ describe PromisePay::User do
4
+ let(:request) { double("PromisePay::Request", execute: sample_response) }
5
+
6
+ before do
7
+ PromisePay::Request.stub(:new) { request }
8
+ end
9
+
10
+ describe ".find" do
11
+ let(:sample_response) { File.read("./spec/support/fixtures/user/find.json") }
12
+ let(:user_id) { 1 }
13
+
14
+ it "returns a PromisePay::User object" do
15
+ expect(described_class.find(user_id)).to be_a_kind_of PromisePay::User
16
+ end
17
+
18
+ it "returned object has correctly assigned attributes" do
19
+ promise_pay_user = described_class.find(user_id)
20
+ expect(promise_pay_user.id).to eq "1"
21
+ expect(promise_pay_user.email).to eq "seller@email.com"
22
+ expect(promise_pay_user.website).to be_nil
23
+ end
24
+
25
+ it "formats created_at and updated_at as Time" do
26
+ promise_pay_user = described_class.find(user_id)
27
+ expect(promise_pay_user.created_at).to be_a_kind_of Time
28
+ expect(promise_pay_user.updated_at).to be_a_kind_of Time
29
+ end
30
+
31
+ it "instantiates PromisePay::Request with the correct path" do
32
+ valid_path = "users/#{user_id}"
33
+ PromisePay::Request.should_receive(:new).with(path: valid_path)
34
+ described_class.find(user_id)
35
+ end
36
+ end
37
+
38
+ describe ".find_all" do
39
+ let(:sample_response) { File.read("./spec/support/fixtures/user/find_all.json") }
40
+
41
+ it "returns an array of PromisePay::User objects" do
42
+ result = described_class.find_all
43
+ expect(result).to be_a_kind_of Array
44
+ expect(result.first).to be_a_kind_of PromisePay::User
45
+ end
46
+
47
+ it "returned objects have correctly assigned attributes" do
48
+ promise_pay_user = described_class.find_all.first
49
+ expect(promise_pay_user.id).to eq "2"
50
+ expect(promise_pay_user.email).to eq "buyer@email.com"
51
+ expect(promise_pay_user.website).to be_nil
52
+ end
53
+
54
+ it "instantiates PromisePay::Request with the correct path" do
55
+ valid_path = "users/"
56
+ PromisePay::Request.should_receive(:new).with(path: valid_path)
57
+ described_class.find_all
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,6 @@
1
+ require "promise_pay"
2
+ require "promise_pay/item"
3
+ require "promise_pay/marketplace"
4
+ require "promise_pay/request"
5
+ require "promise_pay/session"
6
+ require "promise_pay/user"
@@ -0,0 +1,20 @@
1
+ {
2
+ "links" : {
3
+ "items_buyers" : "/items/{items.id}/buyers",
4
+ "items_sellers" : "/items/{items.id}/sellers"
5
+ },
6
+ "items" : {
7
+ "id" : "wef9834tg",
8
+ "name" : "ItemName",
9
+ "created_at" : "2014-07-08T17:10:11.304+10:00",
10
+ "updated_at" : "2014-07-08T17:13:38.599+10:00",
11
+ "amount" : 10,
12
+ "status" : 22000,
13
+ "href" : "/items/wef9834tg",
14
+ "state" : "pending",
15
+ "links" : {
16
+ "buyers" : "2",
17
+ "sellers" : "1"
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,27 @@
1
+ {
2
+ "links" : {
3
+ "items_buyers" : "/items/{items.id}/buyers",
4
+ "items_sellers" : "/items/{items.id}/sellers"
5
+ },
6
+ "meta" : {
7
+ "limit" : 10,
8
+ "offset" : 0,
9
+ "total" : 1
10
+ },
11
+ "items" : [
12
+ {
13
+ "id" : "wef9834tg",
14
+ "name" : "ItemName",
15
+ "created_at" : "2014-07-08T17:10:11.304+10:00",
16
+ "updated_at" : "2014-07-08T17:13:38.599+10:00",
17
+ "amount" : 10,
18
+ "status" : 22000,
19
+ "href" : "/items/wef9834tg",
20
+ "state" : "pending",
21
+ "links" : {
22
+ "buyers" : "2",
23
+ "sellers" : "1"
24
+ }
25
+ }
26
+ ]
27
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "token" : "123abc"
3
+ }
@@ -0,0 +1,32 @@
1
+ {
2
+ "links" : {
3
+ "users_items" : "/users/{users.id}/items"
4
+ },
5
+ "user" : {
6
+ "id" : "1",
7
+ "email" : "seller@email.com",
8
+ "firstname" : "Alex",
9
+ "lastname" : "Jones",
10
+ "companyname" : null,
11
+ "created_at" : "2014-07-08T17:10:11.232+10:00",
12
+ "updated_at" : "2014-07-08T17:10:11.232+10:00",
13
+ "abn" : null,
14
+ "phone" : null,
15
+ "fax" : null,
16
+ "website" : null,
17
+ "mobile" : null,
18
+ "fullname" : "Alex Jones",
19
+ "href" : "/users/1",
20
+ "address" : {
21
+ "addressline1" : null,
22
+ "addressline2" : null,
23
+ "city" : null,
24
+ "state" : null,
25
+ "postcode" : null,
26
+ "country" : "Australia"
27
+ },
28
+ "links" : {
29
+ "items" : ["wef9834tg"]
30
+ }
31
+ }
32
+ }
@@ -0,0 +1,68 @@
1
+ {
2
+ "links" : {
3
+ "users_items" : "/users/{users.id}/items"
4
+ },
5
+ "meta" : {
6
+ "limit" : 10,
7
+ "offset" : 0,
8
+ "total" : 2
9
+ },
10
+ "users" : [
11
+ {
12
+ "id" : "2",
13
+ "email" : "buyer@email.com",
14
+ "firstname" : "Sam",
15
+ "lastname" : "Smith",
16
+ "companyname" : null,
17
+ "created_at" : "2014-07-08T17:10:10.929+10:00",
18
+ "updated_at":"2014-07-08T17:10:10.929+10:00",
19
+ "abn" : null,
20
+ "phone" : null,
21
+ "fax" : null,
22
+ "website" : null,
23
+ "mobile" : null,
24
+ "fullname" : "Sam Smith",
25
+ "href" : "/users/2",
26
+ "address" : {
27
+ "addressline1" : null,
28
+ "addressline2" : null,
29
+ "city" : null,
30
+ "state" : null,
31
+ "postcode" : null,
32
+ "country" : "Australia"
33
+ },
34
+ "links" : {
35
+ "items" : [
36
+ "wef9834tg"
37
+ ]
38
+ }
39
+ },
40
+ {
41
+ "id" : "1",
42
+ "email" : "seller@email.com",
43
+ "firstname" : "Alex",
44
+ "lastname" : "Jones",
45
+ "companyname" : null,
46
+ "created_at" : "2014-07-08T17:10:11.232+10:00",
47
+ "updated_at" : "2014-07-08T17:10:11.232+10:00",
48
+ "abn" : null,
49
+ "phone" : null,
50
+ "fax" : null,
51
+ "website" : null,
52
+ "mobile" : null,
53
+ "fullname" : "Alex Jones",
54
+ "href" : "/users/1",
55
+ "address" : {
56
+ "addressline1" : null,
57
+ "addressline2" : null,
58
+ "city" : null,
59
+ "state" : null,
60
+ "postcode" : null,
61
+ "country" : "Australia"
62
+ },
63
+ "links" : {
64
+ "items" : ["wef9834tg"]
65
+ }
66
+ }
67
+ ]
68
+ }
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: promise_pay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Liam Norton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest_client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: PromisePay gem for API calls wrapped in Ruby
70
+ email:
71
+ - liam.norton@flippa.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/generators/promise_pay/init_generator.rb
82
+ - lib/generators/templates/promise_pay_initializer.rb
83
+ - lib/promise_pay.rb
84
+ - lib/promise_pay/collection.rb
85
+ - lib/promise_pay/item.rb
86
+ - lib/promise_pay/lib/dynamic_accessors.rb
87
+ - lib/promise_pay/marketplace.rb
88
+ - lib/promise_pay/request.rb
89
+ - lib/promise_pay/session.rb
90
+ - lib/promise_pay/user.rb
91
+ - lib/promise_pay/version.rb
92
+ - promise_pay.gemspec
93
+ - spec/promise_pay/item_spec.rb
94
+ - spec/promise_pay/marketplace_spec.rb
95
+ - spec/promise_pay/request_spec.rb
96
+ - spec/promise_pay/session_spec.rb
97
+ - spec/promise_pay/user_spec.rb
98
+ - spec/spec_helper.rb
99
+ - spec/support/fixtures/item/find.json
100
+ - spec/support/fixtures/item/find_all.json
101
+ - spec/support/fixtures/token_generation.json
102
+ - spec/support/fixtures/user/find.json
103
+ - spec/support/fixtures/user/find_all.json
104
+ homepage: https://github.com/iamliamnorton/promise_pay
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.1.9
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: PromisePay gem
128
+ test_files:
129
+ - spec/promise_pay/item_spec.rb
130
+ - spec/promise_pay/marketplace_spec.rb
131
+ - spec/promise_pay/request_spec.rb
132
+ - spec/promise_pay/session_spec.rb
133
+ - spec/promise_pay/user_spec.rb
134
+ - spec/spec_helper.rb
135
+ - spec/support/fixtures/item/find.json
136
+ - spec/support/fixtures/item/find_all.json
137
+ - spec/support/fixtures/token_generation.json
138
+ - spec/support/fixtures/user/find.json
139
+ - spec/support/fixtures/user/find_all.json