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

#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)


7
8
9
10
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 7

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) }
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)


46
47
48
49
50
51
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 46

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



55
56
57
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 55

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



38
39
40
41
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 38

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



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

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



20
21
22
23
24
25
26
27
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 20

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.response['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



31
32
33
34
35
# File 'lib/soaspec/exchange_handlers/rest_parameters.rb', line 31

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



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

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