Class: Rfid::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/rfid/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device, cards) ⇒ Event

Returns a new instance of Event.



9
10
11
12
13
14
15
16
# File 'lib/rfid/event.rb', line 9

def initialize(device, cards)
  @key = device.to_s.strip
  @device_id = (Rfid.config.device_id || @key)
  @cards = Array.wrap(cards)
  @url = File.join(Rfid.config.api_url, "/events/#{@device_id}.json")
  @time = Time.now.strftime("%Y%m%d%H%M%S")
  @mode = 1
end

Instance Attribute Details

#cardsObject

Returns the value of attribute cards.



7
8
9
# File 'lib/rfid/event.rb', line 7

def cards
  @cards
end

#device_idObject

Returns the value of attribute device_id.



7
8
9
# File 'lib/rfid/event.rb', line 7

def device_id
  @device_id
end

#keyObject

Returns the value of attribute key.



7
8
9
# File 'lib/rfid/event.rb', line 7

def key
  @key
end

Instance Method Details

#attributesObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/rfid/event.rb', line 18

def attributes
  { 
    :device_id => @device_id, 
    :cards => @cards, 
    :time => @time, 
    :mode => @mode, 
    :url => @url,
    :key => @key
  }
end

#saveObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rfid/event.rb', line 29

def save
  Rfid.push_event({:event => 'checkout', :data => attributes})
  
  curl = Curl::Easy.new(@url) do |c|
    c.headers["User-Agent"] = "rfid-gem-#{Rfid::VERSION}"
    c.verbose = true
    c.timeout = 25
  end
  
  body = @cards.inject([]) do |arr, card|
    arr << Curl::PostField.content('event[cards][]', card)
  end
  
  body << Curl::PostField.content('event[mode]', @mode)
  body << Curl::PostField.content('event[time]', @time)
  body << Curl::PostField.content('event[hash]', hash)
  
  begin
    curl.http_post(*body)
    parse(curl.response_code, curl.body_str)
  rescue Exception => e
    error!(e.message, e.backtrace.join("\n"))
  end
end

#to_sObject



54
55
56
# File 'lib/rfid/event.rb', line 54

def to_s
  "<Event device: #{@device_id}, cards: #{@cards.join(',')}, time: #{@time}>"
end