Class: Conveyor::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/conveyor/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, channel, port = 8011) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
# File 'lib/conveyor/client.rb', line 5

def initialize host, channel, port = 8011
  @host    = host
  @port    = port
  @channel = channel
  connect!
end

Instance Method Details

#connect!Object



12
13
14
# File 'lib/conveyor/client.rb', line 12

def connect!
  @conn = Net::HTTP.start(@host, @port)
end

#create_channelObject



16
17
18
# File 'lib/conveyor/client.rb', line 16

def create_channel
  @conn.put("/channels/#{@channel}", nil, {'Content-Type' => 'application/octet-stream'})
end

#get(id) ⇒ Object



24
25
26
# File 'lib/conveyor/client.rb', line 24

def get id
  @conn.get("/channels/#{@channel}/#{id}").body
end

#get_nearest_after_timestamp(timestamp) ⇒ Object



61
62
63
# File 'lib/conveyor/client.rb', line 61

def get_nearest_after_timestamp timestamp
  @conn.get("/channels/#{@channel}?after=#{timestamp.to_i}").body
end

#get_next(group = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/conveyor/client.rb', line 28

def get_next group=nil
  if group
    @conn.get("/channels/#{@channel}?next&group=#{group}").body
  else
    @conn.get("/channels/#{@channel}?next").body
  end
end

#get_next_n(n = 10, group = nil) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/conveyor/client.rb', line 40

def get_next_n n = 10, group = nil
  if group
    JSON.parse(@conn.get("/channels/#{@channel}?next&n=#{n}&group=#{group}").body)
  else
    JSON.parse(@conn.get("/channels/#{@channel}?next&n=#{n}").body)
  end
end

#post(content) ⇒ Object



20
21
22
# File 'lib/conveyor/client.rb', line 20

def post content
  @conn.post("/channels/#{@channel}", content, {'Content-Type' => 'application/octet-stream', 'Date' => Time.now.gmtime.to_s})
end

#rewind(*opts) ⇒ Object



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

def rewind *opts
  opts = opts.inject{|h,m| m.merge(h)}
  if opts.key?(:id) && opts.key?(:group)
    @conn.post("/channels/#{@channel}?rewind_id=#{opts[:id]}&group=#{opts[:group]}", nil)
  elsif opts.key?(:id)
    @conn.post("/channels/#{@channel}?rewind_id=#{opts[:id]}", nil)
  elsif opts.key?(:group) && opts.key?(:time)
    @conn.post("/channels/#{@channel}?rewind_time=#{opts[:time].to_i}&group=#{opts[:group]}", nil)
  elsif opts.key?(:time)
    @conn.post("/channels/#{@channel}?rewind_time=#{opts[:time].to_i}", nil)
  end
end

#statusObject



36
37
38
# File 'lib/conveyor/client.rb', line 36

def status
  JSON::parse(@conn.get("/channels/#{@channel}").body)
end