Class: Api::Dimass::Operations::Base
- Inherits:
-
Object
- Object
- Api::Dimass::Operations::Base
- Defined in:
- app/services/api/dimass/operations/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
readonly
Returns the value of attribute api_secret.
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#endpoint_url ⇒ Object
readonly
Returns the value of attribute endpoint_url.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#wsdl ⇒ Object
readonly
Returns the value of attribute wsdl.
-
#wsdl_content ⇒ Object
readonly
Returns the value of attribute wsdl_content.
Delegated Instance Attributes collapse
-
#operations ⇒ Object
Alias for Client#operations.
Instance Method Summary collapse
- #authenticated_endpoint_url ⇒ Object
-
#client ⇒ Object
Initialize a client provided a wsdl path for this api, e.g /papi/stock/1.0.
-
#initialize(path, namespace, wsdl) ⇒ Base
constructor
A new instance of Base.
-
#nonce ⇒ Object
Generates a nonce suitable for Dimass Dimass uses a 62 characters alphanumeric string as a nonce in their example.
- #refresh_wsdl ⇒ Object
-
#retrieve_wsdl_content ⇒ Object
You can use this method to update the wsdl.
-
#signature_params ⇒ Object
Builds the signature params for the Dimass API.
-
#timestamp ⇒ Object
Dimass uses an epoch timestamp in seconds.
- #wsdl_local_path ⇒ Object
Constructor Details
#initialize(path, namespace, wsdl) ⇒ Base
Returns a new instance of Base.
9 10 11 12 13 14 15 16 17 18 |
# File 'app/services/api/dimass/operations/base.rb', line 9 def initialize(path, namespace, wsdl) @config = Heatwave::Configuration.fetch(:dimass_api) @api_key = @config[:key] @api_secret = @config[:secret] @api_url = @config[:url] @endpoint_url = "#{api_url}#{path}" @namespace = namespace @wsdl = wsdl @wsdl_content = File.read(wsdl_local_path) end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
7 8 9 |
# File 'app/services/api/dimass/operations/base.rb', line 7 def api_key @api_key end |
#api_secret ⇒ Object (readonly)
Returns the value of attribute api_secret.
7 8 9 |
# File 'app/services/api/dimass/operations/base.rb', line 7 def api_secret @api_secret end |
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
7 8 9 |
# File 'app/services/api/dimass/operations/base.rb', line 7 def api_url @api_url end |
#endpoint_url ⇒ Object (readonly)
Returns the value of attribute endpoint_url.
7 8 9 |
# File 'app/services/api/dimass/operations/base.rb', line 7 def endpoint_url @endpoint_url end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
7 8 9 |
# File 'app/services/api/dimass/operations/base.rb', line 7 def namespace @namespace end |
#wsdl ⇒ Object (readonly)
Returns the value of attribute wsdl.
7 8 9 |
# File 'app/services/api/dimass/operations/base.rb', line 7 def wsdl @wsdl end |
#wsdl_content ⇒ Object (readonly)
Returns the value of attribute wsdl_content.
7 8 9 |
# File 'app/services/api/dimass/operations/base.rb', line 7 def wsdl_content @wsdl_content end |
Instance Method Details
#authenticated_endpoint_url ⇒ Object
42 43 44 45 46 |
# File 'app/services/api/dimass/operations/base.rb', line 42 def authenticated_endpoint_url uri = Addressable::URI.parse(endpoint_url) uri.query_values = { apikey: api_key, **signature_params } uri.to_s end |
#client ⇒ Object
Initialize a client provided a wsdl path for this api, e.g /papi/stock/1.0
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/services/api/dimass/operations/base.rb', line 49 def client # Example request is <endpoint_url>?signature=mycalculatedsignature&apikey=<myapikey>&nonce=<myusednonce>×tamp=<myusedtimestamp> Savon.client do |config| config.wsdl wsdl_content config.endpoint authenticated_endpoint_url config.log true config.log_level :debug config.pretty_print_xml true config.headers signature_params.stringify_keys # Important if you feed symbols then the http stack will camelize your headers config.namespace_identifier namespace if namespace config.env_namespace 'soapenv' config.ssl_version :TLSv1_2 yield(config) if block_given? end end |
#nonce ⇒ Object
Generates a nonce suitable for Dimass
Dimass uses a 62 characters alphanumeric string as a nonce in their example
71 72 73 |
# File 'app/services/api/dimass/operations/base.rb', line 71 def nonce SecureRandom.hex(31) end |
#operations ⇒ Object
Alias for Client#operations
23 |
# File 'app/services/api/dimass/operations/base.rb', line 23 delegate :operations, to: :client |
#refresh_wsdl ⇒ Object
34 35 36 37 38 39 40 |
# File 'app/services/api/dimass/operations/base.rb', line 34 def refresh_wsdl File.open(wsdl_local_path, 'wb') do |file| file.write(retrieve_wsdl_content) file.flush file.fsync end end |
#retrieve_wsdl_content ⇒ Object
You can use this method to update the wsdl
26 27 28 29 30 31 32 |
# File 'app/services/api/dimass/operations/base.rb', line 26 def retrieve_wsdl_content uri = Addressable::URI.parse(endpoint_url) uri.query_values = { apikey: api_key } wsdl_url = uri.to_s Rails.logger.info "Fetching WSDL from #{wsdl_url}" HTTP.get(wsdl_url).body.to_s end |
#signature_params ⇒ Object
Builds the signature params for the Dimass API
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/services/api/dimass/operations/base.rb', line 81 def signature_params hsh = { nonce:, timestamp:, apikey: api_key } raw_signature = [ hsh[:nonce], hsh[:timestamp], api_secret ].join require 'digest/sha1' hsh[:signature] = Digest::SHA1.hexdigest(raw_signature) hsh end |
#timestamp ⇒ Object
Dimass uses an epoch timestamp in seconds
76 77 78 |
# File 'app/services/api/dimass/operations/base.rb', line 76 def Time.current.to_i end |
#wsdl_local_path ⇒ Object
65 66 67 |
# File 'app/services/api/dimass/operations/base.rb', line 65 def wsdl_local_path Rails.root.join('data', 'wsdl', 'dimass', "#{wsdl}.wsdl") end |