Class: Api::Dimass::Operations::Base

Inherits:
Object
  • Object
show all
Defined in:
app/services/api/dimass/operations/base.rb

Overview

Service object: base.

Direct Known Subclasses

Article, Classification, PurchaseOrder, Stock

Instance Attribute Summary collapse

Delegated Instance Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, namespace, wsdl) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
18
19
20
# File 'app/services/api/dimass/operations/base.rb', line 11

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_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'app/services/api/dimass/operations/base.rb', line 9

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



9
10
11
# File 'app/services/api/dimass/operations/base.rb', line 9

def api_secret
  @api_secret
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



9
10
11
# File 'app/services/api/dimass/operations/base.rb', line 9

def api_url
  @api_url
end

#endpoint_urlObject (readonly)

Returns the value of attribute endpoint_url.



9
10
11
# File 'app/services/api/dimass/operations/base.rb', line 9

def endpoint_url
  @endpoint_url
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



9
10
11
# File 'app/services/api/dimass/operations/base.rb', line 9

def namespace
  @namespace
end

#wsdlObject (readonly)

Returns the value of attribute wsdl.



9
10
11
# File 'app/services/api/dimass/operations/base.rb', line 9

def wsdl
  @wsdl
end

#wsdl_contentObject (readonly)

Returns the value of attribute wsdl_content.



9
10
11
# File 'app/services/api/dimass/operations/base.rb', line 9

def wsdl_content
  @wsdl_content
end

Instance Method Details

#authenticated_endpoint_urlObject



44
45
46
47
48
# File 'app/services/api/dimass/operations/base.rb', line 44

def authenticated_endpoint_url
  uri = Addressable::URI.parse(endpoint_url)
  uri.query_values = { apikey: api_key, **signature_params }
  uri.to_s
end

#clientObject

Initialize a client provided a wsdl path for this api, e.g /papi/stock/1.0



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/api/dimass/operations/base.rb', line 51

def client
  # Example request is <endpoint_url>?signature=mycalculatedsignature&apikey=<myapikey>&nonce=<myusednonce>&timestamp=<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

#nonceObject

Generates a nonce suitable for Dimass
Dimass uses a 62 characters alphanumeric string as a nonce in their example



73
74
75
# File 'app/services/api/dimass/operations/base.rb', line 73

def nonce
  SecureRandom.hex(31)
end

#operationsObject

Alias for Client#operations

Returns:

  • (Object)

    Client#operations

See Also:



25
# File 'app/services/api/dimass/operations/base.rb', line 25

delegate :operations, to: :client

#refresh_wsdlObject



36
37
38
39
40
41
42
# File 'app/services/api/dimass/operations/base.rb', line 36

def refresh_wsdl
  File.open(wsdl_local_path, 'wb') do |file|
    file.write(retrieve_wsdl_content)
    file.flush
    file.fsync
  end
end

#retrieve_wsdl_contentObject

You can use this method to update the wsdl



28
29
30
31
32
33
34
# File 'app/services/api/dimass/operations/base.rb', line 28

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_paramsObject

Builds the signature params for the Dimass API



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/services/api/dimass/operations/base.rb', line 83

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

#timestampObject

Dimass uses an epoch timestamp in seconds



78
79
80
# File 'app/services/api/dimass/operations/base.rb', line 78

def timestamp
  Time.current.to_i
end

#wsdl_local_pathObject



67
68
69
# File 'app/services/api/dimass/operations/base.rb', line 67

def wsdl_local_path
  Rails.root.join('data', 'wsdl', 'dimass', "#{wsdl}.wsdl")
end