module Soaspec::RestParameters

Methods to define parameters specific to REST handler

Public Instance Methods

after_response() { |response, self| ... } click to toggle source

Pass block to perform after every response is retrieved @example 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
# 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) click to toggle source

Defines method 'base_url_value' containing base URL used in REST requests @param [String] url Base Url to use in REST requests. Suburl is appended to this

# 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) click to toggle source

Define basic authentication @param [String] user Username to use @param [String] password Password to use

# 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) click to toggle source

Pass path to YAML file containing Basic Auth parameters (i.e, both username and password) @param [String] path_to_filename Will have Soaspec.credentials_folder appended to it if set

# 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_id() click to toggle source

@return [String] Client id obtained from credentials file

# 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) click to toggle source

@param [Hash] headers Hash of REST headers used in RestClient

# File lib/soaspec/exchange_handlers/rest_parameters.rb, line 66
def headers(headers)
  define_method('rest_client_headers') { headers }
end
oauth2(params) click to toggle source

Will create access_token method based on passed parameters @param [Hash] params OAuth 2 parameters @option params [token_url] URL to retrieve OAuth token from. @Note this can be set globally instead of here @option params [client_id] Client ID @option params [client_secret] Client Secret @option params [username] Username used in password grant @option params [password] Password used in password grant @option params [security_token] Security Token used in password grant

# 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) click to toggle source

Pass path to YAML file containing OAuth2 parameters @param [String] path_to_filename Will have Soaspec.credentials_folder appended to it if set

# 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) click to toggle source

Convert each key from snake_case to PascalCase

# 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) click to toggle source

@param [Array] exception_list List of exceptions to retry response on. Default is

any REST exception

@param [Integer] pause Time to wait before retrying @param [Integer] count Times to retry

# 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

Private Instance Methods

load_credentials_hash(filename) click to toggle source

Load credentials hash from a YAML using Soaspec.credentials_folder if set, adding '.yml' if not set @return [Hash] Hash with credentials in it

# File lib/soaspec/exchange_handlers/rest_parameters.rb, line 99
def load_credentials_hash(filename)
  raise ArgumentError, "Filename passed must be a String for #{self} but was #{filename.class}" unless filename.is_a?(String)

  full_path = Soaspec.credentials_folder ? File.join(Soaspec.credentials_folder, filename) : filename
  full_path += '.yml' unless full_path.end_with?('.yml') # Automatically add 'yml' extension
  raise "No file at #{full_path}. 'Soaspec.credentials_folder' is '#{Soaspec.credentials_folder}'" unless File.exist? full_path

  file_hash = YAML.load_file(full_path)
  raise "File at #{full_path} is not a hash" unless file_hash.is_a? Hash

  file_hash.transform_keys_to_symbols
end