lib/soaspec/exchange_handlers/rest_accessors.rb in soaspec-0.1.0 vs lib/soaspec/exchange_handlers/rest_accessors.rb in soaspec-0.1.1
- old
+ new
@@ -1,93 +1,93 @@
-module Soaspec
- # Accessors specific to REST handler
- module RestAccessors
-
- # 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
- def base_url(url)
- define_method('base_url_value') do
- url
- end
- end
-
- # Will create access_token method based on passed parameters
- # @param [Hash] params Params client_id: nil, client_secret: nil, token_url: nil, username: nil, password: nil, security_token: nil
- def oauth2(params)
- # Payload to add to o-auth request dependent on params provided
- define_method('oauth_payload') do |updated_params|
- payload = { client_id: updated_params[:client_id], client_secret: updated_params[:client_secret] }
- payload.merge(if updated_params[:password] && updated_params[:username]
- {
- grant_type: 'password',
- username: updated_params[:username],
- password: updated_params[:security_token] ? (updated_params[:password] + updated_params[:security_token]) : updated_params[:password],
- multipart: true
- }
- else
- { grant_type: 'client_credentials' }
- end)
- end
- # Method to send request to get oauth token based on parameters
- define_method('oauth_response') do
- raise 'client_id and client_secret not set' unless params[:client_id] && params[:client_secret]
- params[:username] = api_username || ERB.new(params[:username]).result(binding) if params[:username]
- params[:security_token] = ERB.new(params[:security_token]).result(binding) if params[:security_token]
- params[:token_url] = ERB.new(params[:token_url]).result(binding) if params[:token_url]
- params[:password] = ERB.new(params[:password]).result(binding) if params[:password]
- payload = oauth_payload(params)
- request_message = if Soaspec.debug_oauth?
- "request_params: #{payload}"
- else
- params[:username] ? "User '#{params[:username]}'" : 'client_credentials'
- end
- Soaspec::SpecLogger.info request_message
- retry_count = 0
- begin
- response = RestClient.post(params[:token_url], payload, cache_control: 'no_cache', verify_ssl: false)
- rescue RestClient::Exception => e
- Soaspec::SpecLogger.info("oauth_error: #{e.message}")
- Soaspec::SpecLogger.info("oauth_response: #{e.response}")
- retry_count += 1
- retry if retry_count < 3
- raise e
- end
- if Soaspec.debug_oauth?
- Soaspec::SpecLogger.info("response_headers: #{response.headers}")
- Soaspec::SpecLogger.info("response_body: #{response.body}")
- end
- JSON.parse(response)
- end
-
- define_method('access_token') do
- oauth_response['access_token']
- end
- define_method('instance_url') do
- oauth_response['instance_url']
- 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
-
- # @param [Hash] headers Hash of REST headers used in RestClient
- def headers(headers)
- define_method('rest_client_headers') do
- headers
- end
- end
-
- # Convert each key from snake_case to PascalCase
- def pascal_keys(set)
- define_method('pascal_keys?') do
- set
- end
- end
- end
+module Soaspec
+ # Accessors specific to REST handler
+ module RestAccessors
+
+ # 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
+ def base_url(url)
+ define_method('base_url_value') do
+ url
+ end
+ end
+
+ # Will create access_token method based on passed parameters
+ # @param [Hash] params Params client_id: nil, client_secret: nil, token_url: nil, username: nil, password: nil, security_token: nil
+ def oauth2(params)
+ # Payload to add to o-auth request dependent on params provided
+ define_method('oauth_payload') do |updated_params|
+ payload = { client_id: updated_params[:client_id], client_secret: updated_params[:client_secret] }
+ payload.merge(if updated_params[:password] && updated_params[:username]
+ {
+ grant_type: 'password',
+ username: updated_params[:username],
+ password: updated_params[:security_token] ? (updated_params[:password] + updated_params[:security_token]) : updated_params[:password],
+ multipart: true
+ }
+ else
+ { grant_type: 'client_credentials' }
+ end)
+ end
+ # Method to send request to get oauth token based on parameters
+ define_method('oauth_response') do
+ raise 'client_id and client_secret not set' unless params[:client_id] && params[:client_secret]
+ params[:username] = api_username || ERB.new(params[:username]).result(binding) if params[:username]
+ params[:security_token] = ERB.new(params[:security_token]).result(binding) if params[:security_token]
+ params[:token_url] = ERB.new(params[:token_url]).result(binding) if params[:token_url]
+ params[:password] = ERB.new(params[:password]).result(binding) if params[:password]
+ payload = oauth_payload(params)
+ request_message = if Soaspec.debug_oauth?
+ "request_params: #{payload}"
+ else
+ params[:username] ? "User '#{params[:username]}'" : 'client_credentials'
+ end
+ Soaspec::SpecLogger.info request_message
+ retry_count = 0
+ begin
+ response = RestClient.post(params[:token_url], payload, cache_control: 'no_cache', verify_ssl: false)
+ rescue RestClient::Exception => e
+ Soaspec::SpecLogger.info("oauth_error: #{e.message}")
+ Soaspec::SpecLogger.info("oauth_response: #{e.response}")
+ retry_count += 1
+ retry if retry_count < 3
+ raise e
+ end
+ if Soaspec.debug_oauth?
+ Soaspec::SpecLogger.info("response_headers: #{response.headers}")
+ Soaspec::SpecLogger.info("response_body: #{response.body}")
+ end
+ JSON.parse(response)
+ end
+
+ define_method('access_token') do
+ oauth_response['access_token']
+ end
+ define_method('instance_url') do
+ oauth_response['instance_url']
+ 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
+
+ # @param [Hash] headers Hash of REST headers used in RestClient
+ def headers(headers)
+ define_method('rest_client_headers') do
+ headers
+ end
+ end
+
+ # Convert each key from snake_case to PascalCase
+ def pascal_keys(set)
+ define_method('pascal_keys?') do
+ set
+ end
+ end
+ end
end
\ No newline at end of file