lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.33 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.34
- old
+ new
@@ -18,9 +18,53 @@
def base_url(url)
define_method('base_url_value') do
url
end
end
+
+ # Will create access_token method based on passed parameters
+ def oauth2(client_id: nil, client_secret: nil, token_url: nil, username: nil, password: nil, security_token: nil)
+ define_method('access_token') do
+ response = if password && username && security_token
+ RestClient.post(
+ token_url,
+ {
+ grant_type: 'password',
+ client_id: client_id,
+ client_secret: client_secret,
+ username: username,
+ password: (password + security_token),
+ multipart: true
+ },
+ cache_control: 'no_cache',
+ verify_ssl: false
+ )
+ else
+ RestClient.post(
+ token_url,
+ {
+ grant_type: 'client_credentials',
+ client_id: client_id,
+ client_secret: client_secret
+ },
+ cache_control: 'no_cache',
+ verify_ssl: false
+ )
+ end
+ JSON.parse(response)['access_token']
+ end
+ end
+
+ # Pass path to YAML file containing OAuth2 parameters
+ # @param [String] path_to_filename Will have Soaspec.credentials_folder appended to it if set
+ def oauth2_file(path_to_filename)
+ full_path = Soaspec.credentials_folder ? File.join(Soaspec.credentials_folder, path_to_filename + '.yml') : path_to_filename + '.yml'
+ file_hash = YAML.load_file(full_path)
+ raise 'File at ' + full_path + ' is not a hash ' unless file_hash.is_a? Hash
+ oauth_hash = file_hash.transform_keys_to_symbols
+ oauth2 **oauth_hash
+ end
+
end
# Wraps around Savon client defining default values dependent on the soap request
class RestHandler < ExchangeHandler
extend Soaspec::RestAccessors
\ No newline at end of file