Class: Apiotics::Extract

Inherits:
Object
  • Object
show all
Defined in:
lib/apiotics/extract.rb

Class Method Summary collapse

Class Method Details

.fire(object, interfaces, a, time0 = nil, time1 = nil) ⇒ Object



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
# File 'lib/apiotics/extract.rb', line 62

def self.fire(object, interfaces, a, time0= nil, time1= nil)
  if Apiotics.configuration.targets[object.class.parent.name] != nil
    k = "#{object.class.parent.name}::#{object.class.parent.name}".constantize
    i = "#{object.class.parent.name}".underscore.gsub(" ","_").downcase + "_id"
    msg = {
      "action" => a,
      "instance" => k.find(object.send(i)).apiotics_instance,
      "driver" => object.class.name.to_s,
      "interface" => interfaces,
    }
    if time0 != nil
      msg["time0"] = time0
      msg["time1"] = time1
    end
    c = Apiotics::Client.new
    c.send(msg.to_json)
    object.skip_extract = true
    interfaces.each do |key, value|
      object.send(("#{key}_ack=").to_sym, false)
      object.send(("#{key}_complete=").to_sym, false)
      object.send(("#{key}_action=").to_sym, a)
    end
    object.save
    object.skip_extract = false
    unless Apiotics.configuration.local_logging == false
      Extract.save_log(object, interfaces, a)
    end
  end
end

.get(object, time0 = nil, time1 = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/apiotics/extract.rb', line 51

def self.get(object, time0= nil, time1= nil)
  object.accessed_fields.each do |field|
    v = Array.new
    if time0 != nil
      Extract.fire(object, field, object.send(field), "get", time0, time1)
    else
      Extract.fire(object, field, object.send(field), "get")
    end
  end
end

.is_target(object, k) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/apiotics/extract.rb', line 43

def self.is_target(object, k)
  if Apiotics.configuration.targets[object.class.parent.name][object.class.name.demodulize.to_s].include?(k) || Apiotics.configuration.targets[object.class.parent.name][object.class.name.demodulize.to_s].include?(k.titleize)
    return true
  else
    return false
  end
end

.save_log(object, interfaces, a) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/apiotics/extract.rb', line 94

def self.save_log(object, interfaces, a)
  interfaces.each do |key, value|
    klass = "#{object.class.name.underscore}_#{key}_log".classify.constantize
    r = klass.new
    r.send(("#{key}_action=").to_sym, a)
    r.send(("#{key}=".to_sym), value)
    r.send(("#{key}_ack=").to_sym, false)
    r.send(("#{key}_complete=").to_sym, false)
    r.send(("#{object.class.name.demodulize.underscore.gsub("/","_")}_id=").to_sym, object.id)
    puts r.inspect
    r.save
  end
end

.send(object) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/apiotics/extract.rb', line 7

def self.send(object)
  interfaces = Hash.new
  puts object.previous_changes
  object.previous_changes.each do |k,v|
    if Apiotics::Extract.is_target(object, k)
      interfaces[k] = v[1].to_s
    end
  end
  if interfaces != {}
    Extract.fire(object, interfaces, "set-request")
    if Apiotics.configuration.portal == "https://test.apiotics.com" #not a permanent solution.
      if Apiotics.configuration.parents != {}
        interfaces.each do |k,v|
          if Apiotics.configuration.parents[object.class.parent.name][object.class.name.demodulize][k.to_s] != nil
            m =  "#{object.class.parent}".underscore.gsub(" ","_").downcase
            target_class = Apiotics.configuration.parents[object.class.parent.name][object.class.name.demodulize][k.to_s]["driver"].downcase.constantize
            target = object.m.target_class
            i = Apiotics.configuration.parents[object.class.parent.name][object.class.name.demodulize][k.to_s]["interface"].downcase.constantize
            if Apiotics.configuration.parents[object.class.parent.name][object.class.name.demodulize][k.to_s].keys < 3
              target.i = v
              target.skip_extract = true
              target.save
              target.skip_extract = false
            else
              target.i = Apiotics.configuration.parents[object.class.parent.name][object.class.name.demodulize][k.to_s][v.to_s]
              target.skip_extract = true
              target.save
              target.skip_extract = false
            end
          end
        end
      end
    end
  end
end