Class: Gemgento::MagentoApi
- Inherits:
-
Object
- Object
- Gemgento::MagentoApi
- Defined in:
- app/services/gemgento/magento_api.rb
Overview
Class Method Summary collapse
-
.api_login(force_new_session = false) ⇒ Void
Log into the Magento API and set the session and client.
-
.client_config ⇒ Hash
Define the client configuration.
-
.create_call(function, message = {}) ⇒ Hash
Make an API call to Magento and get the response.
-
.enforce_savon_array(subject) ⇒ Array
Enforce an array on the subject.
-
.enforce_savon_string(subject) ⇒ String
Enforce string on subject.
- .log_call(function, message, response) ⇒ Object
-
.replace_empty_strings(subject) ⇒ Hash
Fill in missing empty strings.
- .sanitize_message(message) ⇒ Object
-
.wsdl_url ⇒ String
URL of the WSDL definition.
Class Method Details
.api_login(force_new_session = false) ⇒ Void
Log into the Magento API and set the session and client.
9 10 11 12 |
# File 'app/services/gemgento/magento_api.rb', line 9 def self.api_login(force_new_session = false) @client = Savon.client(client_config) @session = Session.get(@client, force_new_session) end |
.client_config ⇒ Hash
Define the client configuration.
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
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, = {}) api_login if !defined? @client [: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: } end response = @client.call(function, message: ) log_call(function, , 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 ---' api_login(true) create_call(function, ) 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.
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.
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, , response) logger = Logger.new('log/magento_api.log') if response.success? && Gemgento::Config[:magento][:debug] logger.debug "SUCCESS - Function: #{function} - Message: #{ } - Response: #{response.body}" elsif !response.success? logger.error "FAIL - Function: #{function} - 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’ }.
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.() sanitized_keys = %w[cc_number cc_cid cc_exp_year cc_exp_month] if .is_a?(Hash) .each do |key, val| next if val.nil? if sanitized_keys.include? key.to_s [key] = '*' * val.size else [key] = (val) end end elsif .is_a?(Array) .each_with_index do |val, i| [i] = (val) end end return end |
.wsdl_url ⇒ String
URL of the WSDL definition. Created from settings in /config/gemgento_config.yml.
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 |