Module: Six::Import::Model::ClassMethods

Defined in:
lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb

Instance Method Summary collapse

Instance Method Details

#blabla(h) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 58

def blabla(h)
  l = []
  h.each_pair do |key, value|
    l << self.from_hash(value.merge({:type => key.camelize.singularize}))
  end
  l
end

#from_hash(hash) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 91

def from_hash( hash )
  h = hash.dup
  h.each do |key, value|
    case value
      when Array
        h[key].map! { |e|
          Object.const_get(key.camelize.singularize).from_hash e }
      when Hash
        h[key] = Object.const_get(key.camelize).from_hash value
    end
  end
  n = nil
  previous = nil
  if h["id"]
    id = h["id"]
    h.delete "id"
    r = nil
    begin
      r = self.find(id)
    rescue
    end
    if r
      previous = r.inspect
      r.attributes = h
      n = r
    else
      n = self.new h
      n.id = id
    end
  else
    n = self.new h
  end
  n.type = h[:type] if h[:type] if n.respond_to?(:type)
  logger.info "#{n.class} #{n.id} #{n.changed?}: #{"#{n.inspect} - #{previous}" if n.changed?}"
  n.save if n.changed?# || n.new?
  n
end

#from_json(json) ⇒ Object



129
130
131
132
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 129

def from_json( json )
  hash = ActiveSupport::JSON.decode json
  self.from_hash hash
end

#from_xml(xml) ⇒ Object

The xml has a surrounding class tag (e.g. ship-to), but the hash has no counterpart (e.g. ‘ship_to’ => {} )



136
137
138
139
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 136

def from_xml( xml )
  hash = Hash.from_xml xml
  self.from_hash hash[self.to_s.underscore]
end

#impObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 66

def imp
  Six::Network::Panel.setlogger(logger)
  begin
    Six::Network::Panel.("", "")
  rescue ScriptError
  end

  path = "/#{self.to_s.pluralize.downcase}/exp/1.json"
  logger.debug "Path: #{path}"
  res = Six::Network::Panel.get(path)
  h = ActiveSupport::JSON.decode res.body
  k = []
  return k unless h

  case h
    when Array
      h.each do |b|
        k += blabla(b)
      end
    when Hash
      k += blabla h
  end
  k
end