Class: Broker::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/broker/payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Payload

Returns a new instance of Payload.



38
39
40
41
42
43
44
45
# File 'lib/broker/payload.rb', line 38

def initialize(opt={})
  @response = nil
  @pkg = OpenStruct.new(file: opt[:file],
                        dbid: opt[:dbid],
                        app: opt[:app],
                        app_key: opt[:app_key],
                        table: opt[:table])                  
end

Instance Attribute Details

#pkgObject

Returns the value of attribute pkg.



6
7
8
# File 'lib/broker/payload.rb', line 6

def pkg
  @pkg
end

#resultsObject

Returns the value of attribute results.



6
7
8
# File 'lib/broker/payload.rb', line 6

def results
  @results
end

Class Method Details

.chunk(arr) ⇒ Object



33
34
35
# File 'lib/broker/payload.rb', line 33

def chunk(arr)
  3.times { yield arr.pop }
end

.parse(single_path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/broker/payload.rb', line 10

def parse(single_path)
  
  attrs = {
    file: single_path,
    dbid: nil,
    app: nil,
    app_key: nil,
    table: nil
  }
  
  # TODO: Rewrite!
  # Sloppy, need a more elegant way to pop values off the stack
  slots = single_path.split("/")
  slots.pop
  t = slots.pop
  a = slots.pop        
  
  _id = Broker.lookup_tbid({app: a, table: t})
  attrs[:dbid], attrs[:app], attrs[:app_key], attrs[:table] = _id, Broker.lookup_appname(a), a, t
  
  new(attrs)
end

Instance Method Details

#capture_response(results) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/broker/payload.rb', line 61

def capture_response(results)
  if results.respond_to?(:capitalize)
    @response = results
  else
    parse_response(results)
    puts "Captured Payload Result: #{@response.inspect}"
  end
end

#commit(session) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/broker/payload.rb', line 47

def commit(session)
  begin
    capture_response(session.fire_event(self))
  rescue => e
    # Need to log the failed attempt somewhere
    puts "#{pkg.file} failed to import!"
    puts e.message
    puts e.backtrace.join("\n")
    capture_response("Failed to Import")
    return nil
  end
  true
end

#parse_response(results) ⇒ Object

Quickbase API returns 2D array with results First Element -> [num created, num imported, num updated, raw xml, update_id] Second Element -> [invalid records that didn’t get imported]



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/broker/payload.rb', line 74

def parse_response(results)
  details = results.shift
  res = {
    records_imported: details[1],
    records_created: details[0],
    records_updated: details[2],
    update_id: details[4],
    invalid_records: results.shift
  }
  @response = res
end