Module: Soaspec::RestParameters

Included in:
RestHandler
Defined in:
lib/soaspec/exchange_handlers/rest_parameters.rb

Overview

Methods to define parameters specific to REST handler

Instance Method Summary collapse

Instance Method Details

#after_responseObject

Pass block to perform after every response is retrieved

Examples:

Throw exception if response body has an ‘error’ element equal to true

after_response do |response, handler|
  raise Soaspec::ResponseError if handler.value_from_path(response, 'error')
end


80
81
82
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 80

def after_response
  define_method('after_response') { |response, _self| yield response, self }
end

#base_url(url) ⇒ Object

Defines method ‘base_url_value’ containing base URL used in REST requests

Parameters:

  • url (String)

    Base Url to use in REST requests. Suburl is appended to this

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 8

def base_url(url)
  raise ArgumentError, "Base Url passed must be a String for #{self} but was #{url.class}" unless url.is_a?(String)

  define_method('base_url_value') { ERB.new(url).result(binding) }
  # URL used if subclassing handler that sets this previously
  define_singleton_method('parent_url') { ERB.new(url).result(binding) }
end

#basic_auth(user: nil, password: nil) ⇒ Object

Define basic authentication

Parameters:

  • user (String) (defaults to: nil)

    Username to use

  • password (String) (defaults to: nil)

    Password to use

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 51

def basic_auth(user: nil, password: nil)
  raise ArgumentError, "Must pass both 'user' and 'password' for #{self}" unless user && password

  define_method('basic_auth_params') do
    { user: user, password: password }
  end
end

#basic_auth_file(path_to_filename) ⇒ Object

Pass path to YAML file containing Basic Auth parameters (i.e, both username and password)

Parameters:

  • path_to_filename (String)

    Will have Soaspec.credentials_folder appended to it if set



61
62
63
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 61

def basic_auth_file(path_to_filename)
  basic_auth load_credentials_hash(path_to_filename)
end

#client_idString

Returns Client id obtained from credentials file.

Returns:

  • (String)

    Client id obtained from credentials file



42
43
44
45
46
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 42

def client_id
  raise '@client_id is not set. Set by specifying credentials file with "oauth2_file FILENAME" before this is called' unless @client_id

  @client_id
end

#headers(headers) ⇒ Object

Parameters:

  • headers (Hash)

    Hash of REST headers used in RestClient



66
67
68
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 66

def headers(headers)
  define_method('rest_client_headers') { headers }
end

#oauth2(params) ⇒ Object

Will create access_token method based on passed parameters

Parameters:

  • params (Hash)

    OAuth 2 parameters

Options Hash (params):

  • URL (token_url)

    to retrieve OAuth token from. @Note this can be set globally instead of here

  • Client (client_id)

    ID

  • Client (client_secret)

    Secret

  • Username (username)

    used in password grant

  • Password (password)

    used in password grant

  • Security (security_token)

    Token used in password grant



24
25
26
27
28
29
30
31
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 24

def oauth2(params)
  # @!method oauth_obj Object to handle oauth2
  define_method('oauth_obj') { OAuth2.new(params, api_username) }
  # @!method access_token Retrieve OAuth2 access token
  define_method('access_token') { oauth_obj.access_token }
  # @!method instance_url Retrieve instance url from OAuth request
  define_method('instance_url') { oauth_obj.instance_url }
end

#oauth2_file(path_to_filename) ⇒ Object

Pass path to YAML file containing OAuth2 parameters

Parameters:

  • path_to_filename (String)

    Will have Soaspec.credentials_folder appended to it if set



35
36
37
38
39
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 35

def oauth2_file(path_to_filename)
  oauth_parameters = load_credentials_hash(path_to_filename)
  @client_id = oauth_parameters[:client_id] if oauth_parameters[:client_id]
  oauth2 oauth_parameters
end

#pascal_keys(set) ⇒ Object

Convert each key from snake_case to PascalCase



71
72
73
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 71

def pascal_keys(set)
  define_method('pascal_keys?') { set }
end

#retry_on_exceptions(exception_list = [RestClient::RestHandler], pause: 1, count: 3) ⇒ Object

Parameters:

  • exception_list (Array) (defaults to: [RestClient::RestHandler])

    List of exceptions to retry response on. Default is any REST exception

  • pause (Integer) (defaults to: 1)

    Time to wait before retrying

  • count (Integer) (defaults to: 3)

    Times to retry



88
89
90
91
92
93
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 88

def retry_on_exceptions(exception_list = [RestClient::RestHandler], pause: 1,
                        count: 3)
  define_method('retry_on_exceptions') { exception_list }
  define_method('retry_pause_time') { pause }
  define_method('retry_exception_limit') { count }
end