Class: EbayTrader::SaxHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ebay_trader/sax_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ SaxHandler

Returns a new instance of SaxHandler.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ebay_trader/sax_handler.rb', line 15

def initialize(args = {})
  @stack = []
  @stack.push(HashWithIndifferentAccess.new)
  @path = []
  @hash = nil
  @attributes = {}

  @skip_type_casting = args[:skip_type_casting] || []
  @skip_type_casting = [@skip_type_casting] unless @skip_type_casting.is_a?(Array)
  @skip_type_casting.map! { |key| format(key.to_s) }

  @known_arrays = args[:known_arrays] || []
  @known_arrays = [@known_arrays] unless @known_arrays.is_a?(Array)
  @known_arrays.map! { |key| format(key.to_s) }
end

Instance Attribute Details

#known_arraysObject (readonly)

Returns the value of attribute known_arrays.



13
14
15
# File 'lib/ebay_trader/sax_handler.rb', line 13

def known_arrays
  @known_arrays
end

#pathObject

Returns the value of attribute path.



11
12
13
# File 'lib/ebay_trader/sax_handler.rb', line 11

def path
  @path
end

#skip_type_castingObject (readonly)

Returns the value of attribute skip_type_casting.



12
13
14
# File 'lib/ebay_trader/sax_handler.rb', line 12

def skip_type_casting
  @skip_type_casting
end

#stackObject

Returns the value of attribute stack.



10
11
12
# File 'lib/ebay_trader/sax_handler.rb', line 10

def stack
  @stack
end

Instance Method Details

#append(key, value) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ebay_trader/sax_handler.rb', line 113

def append(key, value)
  key = key.to_s
  h = @stack.last
  if h.key?(key)
    v = h[key]
    if v.is_a?(Array)
      v << value
    else
      h[key] = [v, value]
    end
  else
    if known_arrays.include?(key)
      h[key] = [value]
    else
      h[key] = value
    end
  end
end

#attr(name, value) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/ebay_trader/sax_handler.rb', line 100

def attr(name, value)
  return if name.to_s.downcase == 'xmlns'
  last = path.last
  return if last.nil?

  name = name[0].upcase + name[1...name.length]
  @attributes[name] = value
end

#cdata(value) ⇒ Object



94
95
96
97
98
# File 'lib/ebay_trader/sax_handler.rb', line 94

def cdata(value)
  key = format_key(path.last)
  parent = @stack[-2]
  parent[key] = value
end

#end_element(_) ⇒ Object



45
46
47
48
# File 'lib/ebay_trader/sax_handler.rb', line 45

def end_element(_)
  @stack.pop
  @path.pop
end

#error(message, line, column) ⇒ Object

Raises:

  • (Exception)


109
110
111
# File 'lib/ebay_trader/sax_handler.rb', line 109

def error(message, line, column)
  raise Exception.new("#{message} at #{line}:#{column}")
end

#start_element(name) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/ebay_trader/sax_handler.rb', line 35

def start_element(name)
  @attributes.clear
  name = name.to_s
  path.push(name)

  hash = HashWithIndifferentAccess.new
  append(format_key(name), hash)
  stack.push(hash)
end

#text(value) ⇒ Object



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
# File 'lib/ebay_trader/sax_handler.rb', line 50

def text(value)
  key = format_key(path.last)
  auto_cast = !(skip_type_casting.include?(path.last) || skip_type_casting.include?(key.to_s))
  parent = @stack[-2]

  # If 'CurrencyID' is a defined attribute we are dealing with money type
  if @attributes.key?('CurrencyID')
    currency = @attributes.delete('CurrencyID')
    value = BigDecimal.new(value)
    if EbayTrader.configuration.price_type == :money && EbayTrader.is_money_gem_installed?
      value = Money.new((value * 100).round.to_i, currency)
    else
      @attributes['Currency'] = currency
      value = value.to_f if EbayTrader.configuration.price_type == :float
      value = (value * 100).round.to_i if EbayTrader.configuration.price_type == :fixnum
    end
  end

  if auto_cast && value.is_a?(String)
    case
      when value.downcase == 'false' then value = false
      when value.downcase == 'true'  then value = true
      when value.match(/^[0-9]+$/) then value = value.to_i
      when value.match(/^[0-9]+[.][0-9]+$/) then value = value.to_f
      when value.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}[T ][0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9a-z]+)?$/i)
        value = Time.parse(value)
    end
  end

  if parent[key].is_a?(Array)
    parent[key].pop if parent[key].last.is_a?(Hash) && parent[key].last.empty?
    parent[key] << value
  else
    parent[key] = value
  end

  unless @attributes.empty?
    @attributes.each_pair do |attr_key, attr_value|
      attr_key_element_name = format_key("#{path.last}#{attr_key}")
      parent[attr_key_element_name] = attr_value
    end
  end
end

#to_hashObject



31
32
33
# File 'lib/ebay_trader/sax_handler.rb', line 31

def to_hash
  stack[0]
end