Module: NetSuite::Utilities
- Extended by:
- Utilities
- Included in:
- Utilities
- Defined in:
- lib/netsuite/utilities.rb,
lib/netsuite/utilities/data_center.rb
Defined Under Namespace
Classes: DataCenter
Instance Method Summary collapse
- #append_memo(ns_record, added_memo, opts = {}) ⇒ Object
- #backoff(options = {}) ⇒ Object
-
#clear_cache! ⇒ Object
TODO need structured logger for various statements.
- #data_center_url(*args) ⇒ Object
- #find_record(record, names, opts = {}) ⇒ Object
- #get_item(ns_item_internal_id, opts = {}) ⇒ Object
- #get_record(record_klass, id, opts = {}) ⇒ Object
- #netsuite_server_time ⇒ Object
-
#normalize_datetime_from_netsuite(datetime) ⇒ Object
use when displaying times from a NS record.
-
#normalize_datetime_to_netsuite(datetime) ⇒ Object
use when sending times to NS.
- #request_failed?(ns_object) ⇒ Boolean
Instance Method Details
#append_memo(ns_record, added_memo, opts = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/netsuite/utilities.rb', line 13 def append_memo(ns_record, added_memo, opts = {}) opts[:skip_if_exists] ||= false memo_key = if ns_record.class == NetSuite::Records::Customer :comments else :memo end return if opts[:skip_if_exists] && ns_record.send(memo_key) && ns_record.send(memo_key).include?(added_memo) if ns_record.send(memo_key) ns_record.send(:"#{memo_key}=", "#{ns_record.send(memo_key)}. #{added_memo}") else ns_record.send(:"#{memo_key}=", added_memo.to_s) end ns_record end |
#backoff(options = {}) ⇒ Object
40 41 42 43 44 45 46 47 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 98 99 100 101 102 |
# File 'lib/netsuite/utilities.rb', line 40 def backoff( = {}) # TODO the default backoff attempts should be customizable the global config [:attempts] ||= 8 count = 0 begin count += 1 yield rescue Exception => e exceptions_to_retry = [ Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EHOSTUNREACH, EOFError, Wasabi::Resolver::HTTPError, Savon::SOAPFault, Savon::InvalidResponseError, Zlib::BufError, Savon::HTTPError ] # available in ruby > 1.9 if defined?(Net::ReadTimeout) exceptions_to_retry << Net::ReadTimeout end # available in ruby > 2.2.0 if defined?(OpenSSL::SSL::SSLErrorWaitReadable) exceptions_to_retry << OpenSSL::SSL::SSLErrorWaitReadable end if !exceptions_to_retry.include?(e.class) raise end # whitelist certain SOAPFaults; all other network errors should automatically retry if e.is_a?(Savon::SOAPFault) # https://github.com/stripe/stripe-netsuite/issues/815 if !e..include?("Only one request may be made against a session at a time") && !e..include?('java.util.ConcurrentModificationException') && !e..include?('com.netledger.common.exceptions.NLDatabaseOfflineException') && !e..include?('com.netledger.database.NLConnectionUtil$NoCompanyDbsOnlineException') && !e..include?('An unexpected error occurred.') && !e..include?('Session invalidation is in progress with different thread') && !e..include?('SuiteTalk concurrent request limit exceeded. Request blocked.') && !e..include?('The Connection Pool is not intialized.') && # it looks like NetSuite mispelled their error message... !e..include?('The Connection Pool is not intiialized.') raise end end if count >= [:attempts] raise end # log.warn("concurrent request failure", sleep: count, attempt: count) sleep(count) retry end end |
#clear_cache! ⇒ Object
TODO need structured logger for various statements
7 8 9 10 11 |
# File 'lib/netsuite/utilities.rb', line 7 def clear_cache! @netsuite_get_record_cache = {} @netsuite_find_record_cache = {} DataCenter.clear_cache! end |
#data_center_url(*args) ⇒ Object
213 214 215 |
# File 'lib/netsuite/utilities.rb', line 213 def data_center_url(*args) DataCenter.get(*args) end |
#find_record(record, names, opts = {}) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/netsuite/utilities.rb', line 173 def find_record(record, names, opts = {}) field_name = opts[:field_name] names = [ names ] if names.is_a?(String) # FIXME: Records that have the same name but different types will break # the cache names.each do |name| @netsuite_find_record_cache ||= {} if @netsuite_find_record_cache.has_key?(name) return @netsuite_find_record_cache[name] end # sniff for an email-like input; useful for employee/customer searches if !field_name && /@.*\./ =~ name field_name = 'email' end field_name ||= 'name' # TODO remove backoff when it's built-in to search search = backoff { record.search({ basic: [ { field: field_name, operator: 'contains', value: name, } ] }) } if search.results.first return @netsuite_find_record_cache[name] = search.results.first end end nil end |
#get_item(ns_item_internal_id, opts = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/netsuite/utilities.rb', line 117 def get_item(ns_item_internal_id, opts = {}) # TODO add additional item types! ns_item = NetSuite::Utilities.get_record(NetSuite::Records::InventoryItem, ns_item_internal_id, opts) ns_item ||= NetSuite::Utilities.get_record(NetSuite::Records::AssemblyItem, ns_item_internal_id, opts) ns_item ||= NetSuite::Utilities.get_record(NetSuite::Records::NonInventorySaleItem, ns_item_internal_id, opts) ns_item ||= NetSuite::Utilities.get_record(NetSuite::Records::NonInventoryResaleItem, ns_item_internal_id, opts) ns_item ||= NetSuite::Utilities.get_record(NetSuite::Records::DiscountItem, ns_item_internal_id, opts) ns_item ||= NetSuite::Utilities.get_record(NetSuite::Records::OtherChargeSaleItem, ns_item_internal_id, opts) ns_item ||= NetSuite::Utilities.get_record(NetSuite::Records::ServiceSaleItem, ns_item_internal_id, opts) ns_item ||= NetSuite::Utilities.get_record(NetSuite::Records::GiftCertificateItem, ns_item_internal_id, opts) ns_item ||= NetSuite::Utilities.get_record(NetSuite::Records::KitItem, ns_item_internal_id, opts) ns_item ||= NetSuite::Utilities.get_record(NetSuite::Records::SerializedInventoryItem, ns_item_internal_id, opts) if ns_item.nil? fail NetSuite::RecordNotFound, "item with ID #{ns_item_internal_id} not found" end ns_item end |
#get_record(record_klass, id, opts = {}) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/netsuite/utilities.rb', line 137 def get_record(record_klass, id, opts = {}) opts[:external_id] ||= false if opts[:cache] @netsuite_get_record_cache ||= {} @netsuite_get_record_cache[record_klass.to_s] ||= {} if @netsuite_get_record_cache[record_klass.to_s].has_key?(id.to_i) return @netsuite_get_record_cache[record_klass.to_s][id.to_i] end end begin # log.debug("get record", netsuite_record_type: record_klass.name, netsuite_record_id: id) ns_record = if opts[:external_id] backoff { record_klass.get(external_id: id) } else backoff { record_klass.get(id) } end if opts[:cache] @netsuite_get_record_cache[record_klass.to_s][id.to_i] = ns_record end return ns_record rescue ::NetSuite::RecordNotFound # log.warn("record not found", ns_record_type: record_klass.name, ns_record_id: id) if opts[:cache] @netsuite_get_record_cache[record_klass.to_s][id.to_i] = nil end return nil end end |
#netsuite_server_time ⇒ Object
35 36 37 38 |
# File 'lib/netsuite/utilities.rb', line 35 def netsuite_server_time server_time_response = NetSuite::Utilities.backoff { NetSuite::Configuration.connection.call(:get_server_time) } server_time_response.body[:get_server_time_response][:get_server_time_result][:server_time] end |
#normalize_datetime_from_netsuite(datetime) ⇒ Object
use when displaying times from a NS record
238 239 240 241 242 243 244 |
# File 'lib/netsuite/utilities.rb', line 238 def normalize_datetime_from_netsuite(datetime) # the code below eliminates the TimeZone offset then shifts the date forward 2 hours (7200 seconds) # this ensures that ActiveRecord is given a UTC0 DateTime with the exact hour that # was displayed in the NS UI (CST time zone), which will result in the correct display on the web side datetime.new_offset(0) + datetime.offset + Rational(2, 24) end |
#normalize_datetime_to_netsuite(datetime) ⇒ Object
use when sending times to NS
227 228 229 230 231 232 233 234 235 |
# File 'lib/netsuite/utilities.rb', line 227 def normalize_datetime_to_netsuite(datetime) # normalize the time to UCT0 # add 6 hours (21600 seconds) of padding (CST offset) # to force the same time to be displayed in the NS UI is_dst = Time.parse(datetime.to_s).dst? datetime.new_offset(0) + datetime.offset + Rational(is_dst ? 5 : 6, 24) end |
#request_failed?(ns_object) ⇒ Boolean
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/netsuite/utilities.rb', line 104 def request_failed?(ns_object) return false if ns_object.errors.nil? || ns_object.errors.empty? warnings = ns_object.errors.select { |x| x.type == "WARN" } errors = ns_object.errors.select { |x| x.type == "ERROR" } # warnings.each do |warn| # log.warn(warn.message, code: warn.code) # end return errors.size > 0 end |