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

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

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.



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_keyObject (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_secretObject (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_urlObject (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_urlObject (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

#namespaceObject (readonly)

Returns the value of attribute namespace.



7
8
9
# File 'app/services/api/dimass/operations/base.rb', line 7

def namespace
  @namespace
end

#wsdlObject (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_contentObject (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_urlObject



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

#clientObject

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>&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



71
72
73
# File 'app/services/api/dimass/operations/base.rb', line 71

def nonce
  SecureRandom.hex(31)
end

#operationsObject

Alias for Client#operations

Returns:

  • (Object)

    Client#operations

See Also:



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

delegate :operations, to: :client

#refresh_wsdlObject



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_contentObject

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_paramsObject

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

#timestampObject

Dimass uses an epoch timestamp in seconds



76
77
78
# File 'app/services/api/dimass/operations/base.rb', line 76

def timestamp
  Time.current.to_i
end

#wsdl_local_pathObject



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