Module: Soaspec::RestAccessors
- Included in:
- RestHandler
- Defined in:
- lib/soaspec/exchange_handlers/rest_handler.rb
Overview
Accessors specific to REST handler
Instance Method Summary collapse
-
#base_url(url) ⇒ Object
Defines method ‘base_url_value’ containing base URL used in REST requests.
- #headers(headers) ⇒ Object
-
#oauth2(client_id: nil, client_secret: nil, token_url: nil, username: nil, password: nil, security_token: nil) ⇒ 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.
Instance Method Details
#base_url(url) ⇒ Object
Defines method ‘base_url_value’ containing base URL used in REST requests
19 20 21 22 23 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 19 def base_url(url) define_method('base_url_value') do url end end |
#headers(headers) ⇒ Object
84 85 86 87 88 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 84 def headers(headers) define_method('rest_client_headers') do headers end end |
#oauth2(client_id: nil, client_secret: nil, token_url: nil, username: nil, password: nil, security_token: nil) ⇒ Object
Will create access_token method based on passed parameters
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 26 def oauth2(client_id: nil, client_secret: nil, token_url: nil, username: nil, password: nil, security_token: nil) define_method('oauth_response') do username = ERB.new(username).result(binding) if username security_token = ERB.new(security_token).result(binding) if security_token token_url = ERB.new(token_url).result(binding) if token_url password = ERB.new(password).result(binding) if password payload = if password && username { grant_type: 'password', client_id: client_id, client_secret: client_secret, username: username, password: security_token ? (password + security_token) : password, multipart: true } else { grant_type: 'client_credentials', client_id: client_id, client_secret: client_secret } end retry_count = 0 begin Soaspec::SpecLogger.add_to 'request_params: ' + payload.to_s response = RestClient.post(token_url, payload, cache_control: 'no_cache', verify_ssl: false) rescue RestClient::Exception => e Soaspec::SpecLogger.add_to("oauth_error: #{e.}") Soaspec::SpecLogger.add_to("oauth_error: #{e.response}") retry_count += 1 retry if retry_count < 3 raise e end Soaspec::SpecLogger.add_to("response_headers: #{response.headers}") Soaspec::SpecLogger.add_to("response_body: #{response.body}") Soaspec::SpecLogger.add_to("response: #{response}") 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 |
#oauth2_file(path_to_filename) ⇒ Object
Pass path to YAML file containing OAuth2 parameters
75 76 77 78 79 80 81 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 75 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 |
#pascal_keys(set) ⇒ Object
Convert each key from snake_case to PascalCase
91 92 93 94 95 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 91 def pascal_keys(set) define_method('pascal_keys?') do set end end |