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
-
#after_response ⇒ Object
Pass block to perform after every response is retrieved.
-
#base_url(url) ⇒ Object
Defines method ‘base_url_value’ containing base URL used in REST requests.
-
#basic_auth(user: nil, password: nil) ⇒ Object
Define basic authentication.
-
#basic_auth_file(path_to_filename) ⇒ Object
Pass path to YAML file containing Basic Auth parameters (i.e, both username and password).
-
#client_id ⇒ String
Client id obtained from credentials file.
- #headers(headers) ⇒ Object
-
#oauth2(params) ⇒ Object
Will create access_token method based on passed parameters.
-
#oauth2_file(path_to_filename) ⇒ Object
Pass path to YAML file containing OAuth2 parameters.
-
#pascal_keys(set) ⇒ Object
Convert each key from snake_case to PascalCase.
- #retry_on_exceptions(exception_list = [RestClient::RestHandler], pause: 1, count: 3) ⇒ Object
Instance Method Details
#after_response ⇒ Object
Pass block to perform after every response is retrieved
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
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
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)
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_id ⇒ String
Returns 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
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
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
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
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 |