Class: Simpal::Client
- Inherits:
-
Object
- Object
- Simpal::Client
- Defined in:
- lib/simpal/client.rb
Overview
Provides an API client for performing requests under an account within a particular environment.
PayPal API credentials can be obtained from the following URL:
=> https://developer.paypal.com/developer/applications
Instance Attribute Summary collapse
-
#client_id ⇒ String
readonly
The ‘Client ID’ from your PayPal account dashboard.
-
#client_secret ⇒ String
readonly
The ‘Client Secret’ from your PayPal account dashboard.
-
#sandbox ⇒ Boolean
readonly
‘true` when using the sandbox API, `false` when using the production API.
Instance Method Summary collapse
-
#connection ⇒ Faraday::Connection
The connection to use when executing an API request.
-
#headers ⇒ Hash
The default headers, which should be merged into every API request.
-
#initialize(client_id:, client_secret:, sandbox: false) ⇒ Client
constructor
Create a new API client.
-
#service_url ⇒ String
The URL for the PayPal API service.
Constructor Details
#initialize(client_id:, client_secret:, sandbox: false) ⇒ Client
Create a new API client.
28 29 30 31 32 |
# File 'lib/simpal/client.rb', line 28 def initialize(client_id:, client_secret:, sandbox: false) @client_id = client_id @client_secret = client_secret @sandbox = sandbox end |
Instance Attribute Details
#client_id ⇒ String (readonly)
Returns The ‘Client ID’ from your PayPal account dashboard.
12 13 14 |
# File 'lib/simpal/client.rb', line 12 def client_id @client_id end |
#client_secret ⇒ String (readonly)
Returns The ‘Client Secret’ from your PayPal account dashboard.
16 17 18 |
# File 'lib/simpal/client.rb', line 16 def client_secret @client_secret end |
#sandbox ⇒ Boolean (readonly)
Returns ‘true` when using the sandbox API, `false` when using the production API.
20 21 22 |
# File 'lib/simpal/client.rb', line 20 def sandbox @sandbox end |
Instance Method Details
#connection ⇒ Faraday::Connection
Returns The connection to use when executing an API request.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/simpal/client.rb', line 52 def connection @connection ||= Faraday.new(service_url, headers: headers) do |connection| connection.request :json connection.response :raise_error connection.response :json connection.use Simpal::Middleware::Headers connection.use Simpal::Middleware::Authorization, self connection.adapter :net_http end end |
#headers ⇒ Hash
Returns The default headers, which should be merged into every API request.
46 47 48 |
# File 'lib/simpal/client.rb', line 46 def headers { 'User-Agent' => "Simpal/#{Simpal::VERSION}" } end |
#service_url ⇒ String
Returns The URL for the PayPal API service.
36 37 38 39 40 41 42 |
# File 'lib/simpal/client.rb', line 36 def service_url if sandbox 'https://api-m.sandbox.paypal.com' else 'https://api-m.paypal.com' end end |