affirm-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,94 @@
1
+ require "helper"
2
+
3
+ RSpec.describe Affirm::Client do
4
+ context "new instance" do
5
+ it "creates Faraday connection" do
6
+ expect(subject.connection).to be_an_instance_of(Faraday::Connection)
7
+ end
8
+ end
9
+
10
+ context "with api keys set" do
11
+ before do
12
+ Affirm.configure do |config|
13
+ config.public_api_key = "abc"
14
+ config.private_api_key = "xyz"
15
+ end
16
+ end
17
+
18
+ it "sets json handlers" do
19
+ expect(
20
+ subject.connection.builder.handlers
21
+ ).to include(FaradayMiddleware::EncodeJson, FaradayMiddleware::ParseJson)
22
+ end
23
+
24
+ it "sets basic auth header" do
25
+ expect(subject.connection.headers["Authorization"]).to eq("Basic YWJjOnh5eg==")
26
+ end
27
+ end
28
+
29
+ context "post request" do
30
+ before(:all) do
31
+ @stubs = Faraday::Adapter::Test::Stubs.new do |stub|
32
+ stub.post("/api/v2/foo") { [200, {}, ""] }
33
+ stub.post("/api/v2/bar", '{"key":"value"}') { [200, {}, ""] }
34
+ end
35
+ end
36
+
37
+ after(:all) { @stubs.verify_stubbed_calls }
38
+
39
+ before do
40
+ subject.connection.builder.handlers.pop
41
+ subject.connection.adapter(:test, @stubs)
42
+ end
43
+
44
+ it "makes request to full url" do
45
+ response = subject.post("foo", {})
46
+ expect(response.env.url.to_s).to eq("https://api.affirm.com/api/v2/foo")
47
+ end
48
+
49
+ it "makes request to specified path with no leading slash specified" do
50
+ expect(subject.post("foo")).to be_success
51
+ end
52
+
53
+ it "makes request to specified path with leading slash specified" do
54
+ expect(subject.post("/foo")).to be_success
55
+ end
56
+
57
+ it "makes request with json data" do
58
+ expect(subject.post("/bar", { key: "value" })).to be_success
59
+ end
60
+ end
61
+
62
+ context "get request" do
63
+ before(:all) do
64
+ @stubs = Faraday::Adapter::Test::Stubs.new do |stub|
65
+ stub.get("/api/v2/foo") { [200, {}, ""] }
66
+ stub.get("/api/v2/bar?key=value") { [200, {}, ""] }
67
+ end
68
+ end
69
+
70
+ after(:all) { @stubs.verify_stubbed_calls }
71
+
72
+ before do
73
+ subject.connection.builder.handlers.pop
74
+ subject.connection.adapter(:test, @stubs)
75
+ end
76
+
77
+ it "makes request to full url" do
78
+ response = subject.get("foo", {})
79
+ expect(response.env.url.to_s).to eq("https://api.affirm.com/api/v2/foo")
80
+ end
81
+
82
+ it "makes request to specified path with no leading slash specified" do
83
+ expect(subject.get("foo")).to be_success
84
+ end
85
+
86
+ it "makes request to specified path with leading slash specified" do
87
+ expect(subject.get("/foo")).to be_success
88
+ end
89
+
90
+ it "makes request with params" do
91
+ expect(subject.get("/bar", { key: "value" })).to be_success
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,78 @@
1
+ require "helper"
2
+
3
+ RSpec.describe Affirm::Configuration do
4
+ after { restore_config }
5
+
6
+ context "when public_api_key is specified" do
7
+ before do
8
+ Affirm.configure do |config|
9
+ config.public_api_key = "abc"
10
+ end
11
+ end
12
+
13
+ it "returns public api key" do
14
+ expect(Affirm.configuration.public_api_key).to eq("abc")
15
+ end
16
+ end
17
+
18
+ context "when private_api_key is specified" do
19
+ before do
20
+ Affirm.configure do |config|
21
+ config.private_api_key = "xyz"
22
+ end
23
+ end
24
+
25
+ it "returns private api key" do
26
+ expect(Affirm.configuration.private_api_key).to eq("xyz")
27
+ end
28
+ end
29
+
30
+ context "when environment is not specified" do
31
+ before do
32
+ Affirm.configure {}
33
+ end
34
+
35
+ it "defaults to production" do
36
+ expect(Affirm.configuration.environment).to eq(:production)
37
+ end
38
+ end
39
+
40
+ context "when environment is set to production" do
41
+ before do
42
+ Affirm.configure do |config|
43
+ config.environment = :production
44
+ end
45
+ end
46
+
47
+ it "sets environment to production" do
48
+ expect(Affirm.configuration.environment).to eq(:production)
49
+ end
50
+
51
+ it "sets endpoint to production" do
52
+ expect(Affirm.configuration.endpoint).to eq("https://api.affirm.com")
53
+ end
54
+ end
55
+
56
+ context "when environment is set to sandbox" do
57
+ before do
58
+ Affirm.configure do |config|
59
+ config.environment = :sandbox
60
+ end
61
+ end
62
+
63
+ it "sets environment to sandbox" do
64
+ expect(Affirm.configuration.environment).to eq(:sandbox)
65
+ end
66
+
67
+ it "sets endpoint to sandbox" do
68
+ expect(Affirm.configuration.endpoint).to eq("https://sandbox.affirm.com")
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def restore_config
75
+ Affirm.configuration = nil
76
+ Affirm.configure {}
77
+ end
78
+ end
@@ -0,0 +1,43 @@
1
+ require "helper"
2
+
3
+ RSpec.describe Affirm::FailureResult do
4
+ let(:body) do
5
+ {
6
+ "status_code" => 400,
7
+ "type" => "invalid_request",
8
+ "code" => "invalid_field",
9
+ "message" => "An input field resulted in invalid request.",
10
+ "field" => "foo"
11
+ }
12
+ end
13
+
14
+ let(:response) do
15
+ double(:response, status: 400, success?: false, body: body)
16
+ end
17
+
18
+ subject { described_class.new(response) }
19
+
20
+ context "response" do
21
+ it "should have status" do
22
+ expect(subject.status).to eq(response.status)
23
+ end
24
+
25
+ it "should not be successful" do
26
+ expect(subject).not_to be_success
27
+ end
28
+ end
29
+
30
+ context "error object" do
31
+ %w(
32
+ status_code
33
+ type
34
+ code
35
+ message
36
+ field
37
+ ).each do |method|
38
+ it method do
39
+ expect(subject.error.public_send(method)).to eq(body[method])
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1 @@
1
+ {"status":"authorized","user_id":"6438-0406-EYXZ","created":"2015-01-20T23:48:00Z","void":false,"expires":"2015-02-03T23:48:01Z","events":[{"created":"2015-01-20T23:48:00Z","currency":"USD","amount":6997,"type":"auth","id":"EL0GNGQROSYLJSUT","transaction_id":"bOPUilxtKk8m6OsW"}],"payable":0,"currency":"USD","amount":6997,"pending":true,"details":{"merchant":{"public_api_key":"24CS8ZSDMVWE0Y4E","user_cancel_url":"http://qa-1011.shop-hers.com/404","user_confirmation_url":"http://qa-1011.shop-hers.com/404","name":"Shop-Hers"},"tax_amount":500,"billing":{"name":{"full":"John Doe","last":"Doe","first":"John","middle":"Jonny"}},"discounts":{"discount_name":{"discount_display_name":"Discount","discount_amount":500}},"items":{"ABC-123":{"sku":"ABC-123","item_url":"http://merchantsite.com/products/awesome-pants.html","display_name":"Awesome Pants","unit_price":1999,"qty":3,"item_type":"physical","item_image_url":"http://merchantsite.com/images/awesome-pants.jpg"}},"shipping":{"name":{"full":"John Doe","last":"Doe","first":"John","middle":"Jonny"},"address":{"city":"San Francisco","country":"USA","zipcode":"94112","line1":"325 Pacific Ave","line2":"","state":"CA"}},"currency":"USD","shipping_amount":1000,"total":6997},"id":"7RX0-ZTN4","under_dispute":false,"auth_hold":6997}
@@ -0,0 +1,7 @@
1
+ require "affirm"
2
+ require "byebug"
3
+ require "json"
4
+
5
+ def to_seconds(datetime)
6
+ Time.parse(datetime.to_s).to_i
7
+ end
@@ -0,0 +1,87 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # The settings below are suggested to provide a good initial experience
42
+ # with RSpec, but feel free to customize to your heart's content.
43
+ # These two settings work together to allow you to limit a spec run
44
+ # to individual examples or groups you care about by tagging them with
45
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
46
+ # get run.
47
+ config.filter_run :focus
48
+ config.run_all_when_everything_filtered = true
49
+
50
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
51
+ # For more details, see:
52
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
53
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
54
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
55
+ config.disable_monkey_patching!
56
+
57
+ # This setting enables warnings. It's recommended, but in some cases may
58
+ # be too noisy due to issues in dependencies.
59
+ config.warnings = true
60
+
61
+ # Many RSpec users commonly either run the entire suite or an individual
62
+ # file, and it's useful to allow more verbose output when running an
63
+ # individual spec file.
64
+ if config.files_to_run.one?
65
+ # Use the documentation formatter for detailed output,
66
+ # unless a formatter has already been configured
67
+ # (e.g. via a command-line flag).
68
+ config.default_formatter = 'doc'
69
+ end
70
+
71
+ # Print the 10 slowest examples and example groups at the
72
+ # end of the spec run, to help surface which specs are running
73
+ # particularly slow.
74
+ config.profile_examples = 10
75
+
76
+ # Run specs in random order to surface order dependencies. If you find an
77
+ # order dependency and want to debug it, you can fix the order by providing
78
+ # the seed, which is printed after each run.
79
+ # --seed 1234
80
+ config.order = :random
81
+
82
+ # Seed global randomization in this process using the `--seed` CLI option.
83
+ # Setting this allows you to use `--seed` to deterministically reproduce
84
+ # test failures related to randomization by passing the same `--seed` value
85
+ # as the one that triggered the failure.
86
+ Kernel.srand config.seed
87
+ end
@@ -0,0 +1,31 @@
1
+ require "helper"
2
+
3
+ RSpec.describe Affirm::SuccessResult do
4
+ let(:object) { double(:object, id: 1, amount: 500, void: false) }
5
+
6
+ subject { described_class.new(object) }
7
+
8
+ it "should be successful" do
9
+ expect(subject).to be_success
10
+ end
11
+
12
+ it "should have id" do
13
+ expect(subject.id).to eq(object.id)
14
+ end
15
+
16
+ it "should have amount" do
17
+ expect(subject.amount).to eq(object.amount)
18
+ end
19
+
20
+ it "should not be voided" do
21
+ expect(subject.void).to be_falsey
22
+ end
23
+
24
+ it "should raise no method error on missing method" do
25
+ expect { subject.does_not_exist }.to raise_error(NoMethodError)
26
+ end
27
+
28
+ it "should not respond to missing method" do
29
+ expect(subject).not_to respond_to(:does_not_exist)
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: affirm-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yury Velikanau
8
+ - Igor Pstyga
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-06-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.9.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.9.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: faraday_middleware
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.9.1
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.9.1
42
+ - !ruby/object:Gem::Dependency
43
+ name: virtus
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.0'
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 1.0.0
52
+ type: :runtime
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - "~>"
57
+ - !ruby/object:Gem::Version
58
+ version: '1.0'
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ description: Ruby client library for Affirm.com API
63
+ email:
64
+ - yury.velikanau@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - ".gitignore"
70
+ - ".rspec"
71
+ - ".travis.yml"
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - affirm.gemspec
78
+ - lib/affirm.rb
79
+ - lib/affirm/charge.rb
80
+ - lib/affirm/client.rb
81
+ - lib/affirm/configuration.rb
82
+ - lib/affirm/failure_result.rb
83
+ - lib/affirm/objects/address.rb
84
+ - lib/affirm/objects/billing.rb
85
+ - lib/affirm/objects/contact.rb
86
+ - lib/affirm/objects/discount.rb
87
+ - lib/affirm/objects/event.rb
88
+ - lib/affirm/objects/item.rb
89
+ - lib/affirm/objects/order.rb
90
+ - lib/affirm/objects/shipping.rb
91
+ - lib/affirm/responses/auth.rb
92
+ - lib/affirm/responses/capture.rb
93
+ - lib/affirm/responses/error.rb
94
+ - lib/affirm/responses/refund.rb
95
+ - lib/affirm/responses/update.rb
96
+ - lib/affirm/responses/void.rb
97
+ - lib/affirm/success_result.rb
98
+ - lib/affirm/version.rb
99
+ - spec/charge_object_interface.rb
100
+ - spec/charge_spec.rb
101
+ - spec/client_spec.rb
102
+ - spec/configuration_spec.rb
103
+ - spec/failure_result_spec.rb
104
+ - spec/fixtures/auth.json
105
+ - spec/helper.rb
106
+ - spec/spec_helper.rb
107
+ - spec/success_result_spec.rb
108
+ homepage: https://github.com/spectator/affirm
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - "~>"
119
+ - !ruby/object:Gem::Version
120
+ version: '2.1'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.8
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Ruby wrapper for Affirm.com API
132
+ test_files:
133
+ - spec/charge_object_interface.rb
134
+ - spec/charge_spec.rb
135
+ - spec/client_spec.rb
136
+ - spec/configuration_spec.rb
137
+ - spec/failure_result_spec.rb
138
+ - spec/fixtures/auth.json
139
+ - spec/helper.rb
140
+ - spec/spec_helper.rb
141
+ - spec/success_result_spec.rb