Class: Gemgento::MagentoApi

Inherits:
Object
  • Object
show all
Defined in:
app/services/gemgento/magento_api.rb

Overview

Author:

  • Gemgento LLC

Class Method Summary collapse

Class Method Details

.api_login(force_new_session = false) ⇒ Void

Log into the Magento API and set the session and client.

Parameters:

  • force_new_session (Boolean) (defaults to: false)

Returns:

  • (Void)


9
10
11
12
# File 'app/services/gemgento/magento_api.rb', line 9

def self.(force_new_session = false)
  @client = Savon.client(client_config)
  @session = Session.get(@client, force_new_session)
end

.client_configHash

Define the client configuration.

Returns:

  • (Hash)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/gemgento/magento_api.rb', line 17

def self.client_config
  config = {
    wsdl: wsdl_url,
    log: Config[:magento][:debug],
    raise_errors: false,
    open_timeout: 300,
    read_timeout: 300
  }

  if !Config[:magento][:auth_username].blank? && !Config[:magento][:auth_password].blank?
    config[:basic_auth] = [Config[:magento][:auth_username].to_s, Config[:magento][:auth_password].to_s]
  end

  return config
end

.create_call(function, message = {}) ⇒ Hash

Make an API call to Magento and get the response

Parameters:

  • function (Symbol)

    The API call to make

  • message (Hash) (defaults to: {})

    Call parameters (does not need session)

Returns:

  • (Hash)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/services/gemgento/magento_api.rb', line 48

def self.create_call(function, message = {})
   if !defined? @client

  message[:sessionId] = @session
  Rails.logger.debug "Making Call - #{function}"

  magento_response = MagentoResponse.new

  # don't save some messages because they are just too big!
  if [:catalog_product_attribute_media_create].include? function
    magento_response.request = {function: function, message: ''}
  else
    magento_response.request = {function: function, message: message}
  end

  response = @client.call(function, message: message)
  log_call(function, message, response)

  if response.success?
    magento_response.success = true

    if [:customer_customer_list, :sales_order_list, :catalog_product_list].include? function
      magento_response.body = response.body[:"body_too_big"]
      magento_response.body_overflow = response.body[:"#{function}_response"]
    else
      magento_response.body = response.body[:"#{function}_response"]
    end

    # only save successful responses if debugging is enabled
    magento_response.save if Config[:magento][:debug]

  else
    magento_response.success = false
    magento_response.body = response.body[:fault]
    Rails.logger.error "Magento API Call failure - #{magento_response.body[:faultcode]} - #{magento_response.body[:faultstring]}"

    if !magento_response.body[:faultcode].nil? && magento_response.body[:faultcode].to_i == 5
      Rails.logger.debug '--- Attempting To Start New Session ---'
      (true)
      create_call(function, message)
    end

    magento_response.save
  end

  magento_response.body = replace_empty_strings(magento_response.body)
  magento_response.body_overflow = replace_empty_strings(magento_response.body_overflow) unless magento_response.body_overflow.nil?

  return magento_response
end

.enforce_savon_array(subject) ⇒ Array

Enforce an array on the subject. If the subject is not an array, it is inserted into a single element array.

Parameters:

  • subject (*)

Returns:

  • (Array)


141
142
143
144
145
146
147
# File 'app/services/gemgento/magento_api.rb', line 141

def self.enforce_savon_array(subject)
  if subject.is_a? Array
    subject
  else
    [subject]
  end
end

.enforce_savon_string(subject) ⇒ String

Enforce string on subject. If it’s not a String, an empty string is returned.

Parameters:

  • subject (*)

Returns:

  • (String)


129
130
131
132
133
134
135
# File 'app/services/gemgento/magento_api.rb', line 129

def self.enforce_savon_string(subject)
  if subject.is_a? String
    subject
  else
    ''
  end
end

.log_call(function, message, response) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'app/services/gemgento/magento_api.rb', line 99

def self.log_call(function, message, response)
  logger = Logger.new('log/magento_api.log')

  if response.success? && Gemgento::Config[:magento][:debug]
    logger.debug "SUCCESS - Function: #{function} - Message: #{sanitize_message message} - Response: #{response.body}"
  elsif !response.success?
    logger.error "FAIL - Function: #{function} - Message: #{sanitize_message message} - Response: #{response.body}"
  end
end

.replace_empty_strings(subject) ⇒ Hash

Fill in missing empty strings. Empty strings are represented by { :‘@xsi:type’ => ‘xsd:string’ }.

Parameters:

  • subject (Hash)

Returns:

  • (Hash)


113
114
115
116
117
118
119
120
121
122
123
# File 'app/services/gemgento/magento_api.rb', line 113

def self.replace_empty_strings(subject)
  if subject == { :'@xsi:type' => 'xsd:string' }
    return ''
  elsif subject.is_a?(Array)
    return subject.map{ |child| replace_empty_strings(child) }
  elsif subject.is_a?(Hash)
    return subject.each{ |key, value| subject[key] = replace_empty_strings(value) }
  else
    return subject
  end
end

.sanitize_message(message) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/services/gemgento/magento_api.rb', line 149

def self.sanitize_message(message)
  sanitized_keys = %w[cc_number cc_cid cc_exp_year cc_exp_month]

  if message.is_a?(Hash)
    message.each do |key, val|
      next if val.nil?

      if sanitized_keys.include? key.to_s
        message[key] = '*' * val.size
      else
        message[key] = sanitize_message(val)
      end
    end

  elsif message.is_a?(Array)
    message.each_with_index do |val, i|
      message[i] = sanitize_message(val)
    end
  end

  return message
end

.wsdl_urlString

URL of the WSDL definition. Created from settings in /config/gemgento_config.yml.

Returns:

  • (String)


36
37
38
39
40
41
# File 'app/services/gemgento/magento_api.rb', line 36

def self.wsdl_url
  url = Gemgento::Config[:magento][:url].gsub(/\/$/, '') # ensure there is no trailing slash
  url += "/index.php/api/v#{Gemgento::Config[:magento][:api_version]}_#{Gemgento::Config[:magento][:api_type]}/index/wsdl/1"

  return url
end