paypal_client 0.1.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: 01521c538a5f17d341615b44eeb0b6375d378eee
4
+ data.tar.gz: 4cc16db9b1daa24a4275d87f60755ae7174bf547
5
+ SHA512:
6
+ metadata.gz: 6f90fcbd2b1bb7824e08daaee89c6efff9f01849d4a7d08d1699439a76f0f858da9d286b4c20fcc52adaa8503f3bd594c3123ddf218f96bf83c85f3a33628496
7
+ data.tar.gz: fda01a2b8fd05cf1a1985e406418e631f7b2329e9d86126aef06a4f5f7af1651acc204c7114a59aaf53ed9e9d66026dd5d16aa43e47749a39684f0048406d82c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.8
5
+ - 2.4.5
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.16.1
8
+ matrix:
9
+ fast_finish: true
10
+ script:
11
+ - bundle exec rspec
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dennis.vandervliet@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in paypal_client.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ paypal_client (0.1.0)
5
+ activesupport (~> 5)
6
+ faraday (~> 0.15)
7
+ faraday_middleware (~> 0.12)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (5.2.2)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ ast (2.4.0)
18
+ concurrent-ruby (1.1.4)
19
+ diff-lcs (1.3)
20
+ faraday (0.15.4)
21
+ multipart-post (>= 1.2, < 3)
22
+ faraday_middleware (0.12.2)
23
+ faraday (>= 0.7.4, < 1.0)
24
+ i18n (1.2.0)
25
+ concurrent-ruby (~> 1.0)
26
+ jaro_winkler (1.5.1)
27
+ minitest (5.11.3)
28
+ multipart-post (2.0.0)
29
+ parallel (1.12.1)
30
+ parser (2.5.3.0)
31
+ ast (~> 2.4.0)
32
+ powerpack (0.1.2)
33
+ rainbow (3.0.0)
34
+ rake (10.5.0)
35
+ rspec (3.8.0)
36
+ rspec-core (~> 3.8.0)
37
+ rspec-expectations (~> 3.8.0)
38
+ rspec-mocks (~> 3.8.0)
39
+ rspec-core (3.8.0)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-expectations (3.8.2)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-mocks (3.8.0)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.8.0)
47
+ rspec-support (3.8.0)
48
+ rubocop (0.61.1)
49
+ jaro_winkler (~> 1.5.1)
50
+ parallel (~> 1.10)
51
+ parser (>= 2.5, != 2.5.1.1)
52
+ powerpack (~> 0.1)
53
+ rainbow (>= 2.2.2, < 4.0)
54
+ ruby-progressbar (~> 1.7)
55
+ unicode-display_width (~> 1.4.0)
56
+ ruby-progressbar (1.10.0)
57
+ thread_safe (0.3.6)
58
+ tzinfo (1.2.5)
59
+ thread_safe (~> 0.1)
60
+ unicode-display_width (1.4.0)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ bundler (~> 1.16)
67
+ paypal_client!
68
+ rake (~> 10.0)
69
+ rspec (~> 3.0)
70
+ rubocop
71
+
72
+ BUNDLED WITH
73
+ 1.16.1
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # PaypalClient
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/paypal_client`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'paypal_client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install paypal_client
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/paypal_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the PaypalClient project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/paypal_client/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'paypal_client'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module PaypalClient
2
+ VERSION = '0.1.1'.freeze
3
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'paypal_client/version'
4
+ require 'active_support/cache'
5
+
6
+ module PaypalClient
7
+ require 'faraday'
8
+ require 'faraday_middleware'
9
+
10
+ class Client
11
+ LIVE_URL = 'https://api.paypal.com'.freeze
12
+ SANDBOX_URL = 'https://api.sandbox.paypal.com'.freeze
13
+ VERSION = 'v1'.freeze
14
+
15
+ TOKEN_CACHE_KEY = 'paypal_oauth_token'.freeze
16
+ # We don't want to use a token that expired 1 second ago
17
+ # so we have them expiry 3600 seconds before they actually expire
18
+ TOKEN_EXPIRY_MARGIN = 60 * 60
19
+
20
+ def self.build
21
+ @client ||= Client.new(
22
+ client_id: ENV.fetch('PAYPAL_CLIENT_ID'),
23
+ client_secret: ENV.fetch('PAYPAL_CLIENT_SECRET'),
24
+ sandbox: ENV.fetch('PAYPAL_SANDBOX', true),
25
+ cache: ActiveSupport::Cache::MemoryStore.new,
26
+ version: VERSION
27
+ )
28
+ end
29
+
30
+ def initialize(client_id:, client_secret:, cache:, sandbox: true, version:, logger: nil)
31
+ @client_id = client_id
32
+ @client_secret = client_secret
33
+ @cache = cache
34
+ @sandbox = sandbox
35
+ @version = version
36
+ @logger = logger
37
+ end
38
+
39
+ def connection
40
+ @conn ||= Faraday.new(url: base_url) do |faraday|
41
+ faraday.headers = default_headers
42
+ faraday.response @logger if @logger
43
+ faraday.request :json
44
+ faraday.response :json, parser_options: { symbolize_names: true }
45
+
46
+ faraday.use ErrorMiddleware
47
+ faraday.adapter *adapter
48
+ end
49
+ end
50
+
51
+ %i[get patch post put delete].each do |http_method|
52
+ define_method http_method do |path, data = {}, headers = {}|
53
+ connection.public_send(http_method, merged_path(path), data, merged_headers(headers))
54
+ end
55
+ end
56
+
57
+ def auth_token(force: false)
58
+ return @cache.read(TOKEN_CACHE_KEY) if @cache.exist?(TOKEN_CACHE_KEY) && force == false
59
+
60
+ auth_response = authenticate
61
+ @cache.fetch(TOKEN_CACHE_KEY, expires_in: auth_response[:expires_in] - TOKEN_EXPIRY_MARGIN) do
62
+ auth_response[:access_token]
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def base_url
69
+ @sandbox ? SANDBOX_URL : LIVE_URL
70
+ end
71
+
72
+ def merged_headers(headers)
73
+ headers.merge(auth_header).merge(default_headers)
74
+ end
75
+
76
+ def merged_path(path)
77
+ File.join(@version, path).gsub(File::SEPARATOR, '/')
78
+ end
79
+
80
+ def auth_header
81
+ { authorization: "Bearer #{auth_token}" }
82
+ end
83
+
84
+ def default_headers
85
+ {
86
+ 'Accept': 'application/json',
87
+ 'Content-Type': 'application/json'
88
+ }
89
+ end
90
+
91
+ def authenticate
92
+ basic_auth = ["#{@client_id}:#{@client_secret}"].pack('m').delete("\r\n")
93
+ endpoint = [@version, 'oauth2', 'token'].join('/')
94
+
95
+ response = connection.post(endpoint,
96
+ 'grant_type=client_credentials',
97
+ authorization: "Basic #{basic_auth}", "Content-Type": 'application/x-www-form-urlencoded')
98
+ response.body
99
+ end
100
+
101
+ def adapter
102
+ Faraday.default_adapter
103
+ end
104
+ end
105
+
106
+ class ResponseError < StandardError; end
107
+ class NotFound < ResponseError; end
108
+
109
+ class ErrorMiddleware < Faraday::Response::Middleware
110
+ ERROR_MAP = {
111
+ 404 => NotFound
112
+ }.freeze
113
+
114
+ def on_complete(response)
115
+ key = response[:status].to_i
116
+ raise ERROR_MAP[key], response if ERROR_MAP.key? key
117
+ raise ResponseError, response if response.status >= 400
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,39 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'paypal_client/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'paypal_client'
7
+ spec.version = PaypalClient::VERSION
8
+ spec.authors = ['Dennis van der Vliet']
9
+ spec.email = ['dennis@wetransfer.com']
10
+
11
+ spec.summary = 'API Client for the PayPal REST API'
12
+ spec.description = 'Faraday based API client for the PayPal REST API. Includes support for saving the authentication token and limited release endpoints.'
13
+ spec.homepage = 'https://github.com/dennisvdvliet/paypal_client'
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
+ else
20
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
21
+ 'public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'activesupport', '~> 5'
32
+ spec.add_dependency 'faraday', '~> 0.15'
33
+ spec.add_dependency 'faraday_middleware', '~> 0.12'
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.16'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'rspec', '~> 3.0'
38
+ spec.add_development_dependency 'rubocop'
39
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paypal_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dennis van der Vliet
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.15'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.12'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Faraday based API client for the PayPal REST API. Includes support for
112
+ saving the authentication token and limited release endpoints.
113
+ email:
114
+ - dennis@wetransfer.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/paypal_client.rb
130
+ - lib/paypal_client/version.rb
131
+ - paypal_client.gemspec
132
+ homepage: https://github.com/dennisvdvliet/paypal_client
133
+ licenses: []
134
+ metadata:
135
+ allowed_push_host: https://rubygems.org
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.5.2.3
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: API Client for the PayPal REST API
156
+ test_files: []