Class: Simpal::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_idString (readonly)

Returns The ‘Client ID’ from your PayPal account dashboard.

Returns:

  • (String)

    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_secretString (readonly)

Returns The ‘Client Secret’ from your PayPal account dashboard.

Returns:

  • (String)

    The ‘Client Secret’ from your PayPal account dashboard.



16
17
18
# File 'lib/simpal/client.rb', line 16

def client_secret
  @client_secret
end

#sandboxBoolean (readonly)

Returns ‘true` when using the sandbox API, `false` when using the production API.

Returns:

  • (Boolean)

    ‘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

#connectionFaraday::Connection

Returns The connection to use when executing an API request.

Returns:

  • (Faraday::Connection)

    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

#headersHash

Returns The default headers, which should be merged into every API request.

Returns:

  • (Hash)

    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_urlString

Returns The URL for the PayPal API service.

Returns:

  • (String)

    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