Class: Xively::Command::Subscribe

Inherits:
Base
  • Object
show all
Defined in:
lib/xively-cli/command/subscribe.rb

Overview

Subscribe to a feed or datastream via the Xively Socket Server

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize, namespace

Constructor Details

This class inherits a constructor from Xively::Command::Base

Instance Method Details

#indexObject

subscribe

connect to a tcp socket for a feed or datastream

-k, –key API_KEY # your api key -f, –feed FEED # the feed id -d, –datastream DATASTREAM # the datastream id (optional)

Example:

$ xively subscribe -k ABCD1234 -f 504 -d 0 # subscribe to datastream $ xively subscribe -k ABCD1234 -f 504 # subscribe to feed



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/xively-cli/command/subscribe.rb', line 21

def index
  api_key = options[:key]
  feed_id = options[:feed]
  datastream_id = options[:datastream]

  validate_arguments!

  unless api_key && feed_id
    $stderr.puts Xively::Command::Help.usage_for_command("subscribe")
    exit(1)
  end

  resource = "/feeds/#{feed_id}"
  resource += "/datastreams/#{datastream_id}" if datastream_id

  puts "Subscribing to updates for #{resource}"

  subscribe = "{\"method\":\"subscribe\", \"resource\":\"#{resource}\", \"headers\":{\"X-ApiKey\":\"#{api_key}\"}}"
  s = TCPSocket.new 'api.xively.com', 8081
  s.puts subscribe
  while line = s.gets
    parse_data(line, s)
  end
  s.close

  puts "Connection closed"

  # EventMachine.run {
  #   EventMachine::connect 'api.xively.com', 8081, XivelySocket, options
  # }
rescue Interrupt => e
  puts "Closing connection"
  s.close
  raise e
end