Class: GoCardlessPro::Services::BaseService

Inherits:
Object
  • Object
show all
Defined in:
lib/gocardless_pro/services/base_service.rb

Overview

Base Service that all services inherit from.

Instance Method Summary collapse

Constructor Details

#initialize(api_service) ⇒ BaseService

Create a new service instance to make requests against

Parameters:



11
12
13
# File 'lib/gocardless_pro/services/base_service.rb', line 11

def initialize(api_service)
  @api_service = api_service
end

Instance Method Details

#envelope_keyObject

Get the envelope key for the given service. Children are expected to implement this method.

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/gocardless_pro/services/base_service.rb', line 25

def envelope_key
  raise NotImplementedError
end

#make_request(method, path, options = {}) ⇒ Object

Make a request to the API using the API service instance

Parameters:

  • method (Symbol)

    the method to use to make the request

  • path (String)

    the URL (without the base domain) to make the request to

  • options (Hash) (defaults to: {})

    the options hash - either the query parameters for a GET, or the body if POST/PUT



20
21
22
# File 'lib/gocardless_pro/services/base_service.rb', line 20

def make_request(method, path, options = {})
  @api_service.make_request(method, path, options.merge(envelope_key: envelope_key))
end

#sub_url(url, param_map) ⇒ Object

take a URL with placeholder params and substitute them out for the actual value

Parameters:

  • url (String)

    the URL with placeholders in

  • param_map (Hash)

    a hash of placeholders and their actual values (which will be escaped)



32
33
34
35
36
# File 'lib/gocardless_pro/services/base_service.rb', line 32

def sub_url(url, param_map)
  param_map.reduce(url) do |new_url, (param, value)|
    new_url.gsub(":#{param}", CGI.escape(value))
  end
end