Module: Collmex::Api
- Defined in:
- lib/collmex/api.rb,
lib/collmex/api.rb,
lib/collmex/api.rb
Defined Under Namespace
Classes: AccBal, AccbalGet, Accdoc, AccdocGet, Cmxknd, Cmxord2, CustomerGet, Line, Login, Message, SalesOrderGet
Class Method Summary collapse
- .is_a_collmex_api_line_obj?(obj) ⇒ Boolean
- .line_class_exists?(class_name) ⇒ Boolean
- .parse_currency(str) ⇒ Object
- .parse_field(value, type, opts = nil) ⇒ Object
- .parse_line(line) ⇒ Object
- .stringify(data, type) ⇒ Object
- .stringify_currency(data) ⇒ Object
Class Method Details
.is_a_collmex_api_line_obj?(obj) ⇒ Boolean
6 7 8 |
# File 'lib/collmex/api.rb', line 6 def self.is_a_collmex_api_line_obj? obj obj.class.name =~ /Collmex\:\:Api/ end |
.line_class_exists?(class_name) ⇒ Boolean
10 11 12 13 14 15 |
# File 'lib/collmex/api.rb', line 10 def self.line_class_exists?(class_name) klass = Collmex::Api.const_get(class_name) return klass.is_a?(Class) rescue NameError return false end |
.parse_currency(str) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/collmex/api.rb', line 48 def self.parse_currency(str) str = str.to_s case str when /\A-?\d*[\,|.]\d{0,2}\z/ then (str.gsub(',','.').to_f * 100).to_i when /\A-?\d+\z/ then str.to_i when /\A-?((\d){1,3})*([\.]\d{3})+([,]\d{2})\z/ then (str.gsub('.','').gsub(',','.').to_f * 100).to_i when /\A-?((\d){1,3})*([\,]\d{3})+([.]\d{2})\z/ then (str.gsub(',','').to_f * 100).to_i when /\A-?((\d){1,3})*([\.\,]\d{3})+\z/ then str.gsub(',','').gsub('.','').to_i * 100 else str.to_i end end |
.parse_field(value, type, opts = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/collmex/api.rb', line 37 def self.parse_field(value, type, opts = nil) case type when :string then value.to_s when :date then Date.parse(value.to_s) unless value.nil? when :int then value.to_i unless value.nil? when :integer then value.to_i unless value.nil? when :float then value.to_s.gsub(',','.').to_f unless value.nil? when :currency then Collmex::Api.parse_currency(value) unless value.nil? end end |
.parse_line(line) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/collmex/api.rb', line 17 def self.parse_line(line) if line.is_a?(Array) and line.first.is_a?(String) identifyer = line.first.split(/_|-/).map { |s| s.downcase.capitalize }.join if self.line_class_exists?(identifyer) Collmex::Api.const_get(identifyer).new(line) else raise "Could not find a Collmex::Api::Line class for \"#{identifyer}\" (\"#{line.first}\")" end elsif line.is_a?(String) && parsed_line = CSV.parse_line(line, Collmex.csv_opts) identifyer = parsed_line.first.split(/_|-/).map { |s| s.downcase.capitalize }.join if self.line_class_exists?(identifyer) Collmex::Api.const_get(identifyer).new(parsed_line) else raise "Could not find a Collmex::Api::Line class for \"#{identifyer}\" (\"#{parsed_line.first}\")" end else raise "Could not parse a Collmex::Api Line from #{line.inspect}" end end |
.stringify(data, type) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/collmex/api.rb', line 60 def self.stringify(data, type) case type when :integer then (data.nil?)? data.to_s : data.to_i.to_s when :string then data.to_s when :float then sprintf("%.2f",data).gsub('.',',') when :currency then Collmex::Api.stringify_currency(data) when :date then data.strftime("%Y%m%d") unless data.nil? end end |
.stringify_currency(data) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/collmex/api.rb', line 70 def self.stringify_currency(data) case when data.is_a?(Integer) then sprintf("%.2f",(data.to_f / 100)).gsub('.',',') when data.is_a?(Float) then sprintf("%.2f",(data.to_f)).gsub('.',',') when data.is_a?(String) int = self.parse_currency(data) sprintf("%.2f",(int.to_f / 100)).gsub('.',',') else data end end |