Class: PachubeStream::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pachube-stream/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(conn, api_key, options = {}) ⇒ Client



7
8
9
10
11
12
# File 'lib/pachube-stream/client.rb', line 7

def initialize(conn, api_key, options = {})
  @conn = conn
  @api_key = api_key
  @options = options
  @requests = {}
end

Instance Method Details

#call_block_for_request(request, parsed_response) ⇒ Object

finds the correct callback based on the token which has the method call in its sig



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pachube-stream/client.rb', line 64

def call_block_for_request(request, parsed_response)
  if parsed_response["body"].nil?
    request.on_complete_block.call(parsed_response) if request.on_complete_block
  else
   case request.token.gsub(/:.*/, "")
    when "subscribe"
      request.on_datastream_block.call(parsed_response) if request.on_datastream_block
    when "get"
      request.on_get_block.call(parsed_response) if request.on_get_block
    end
  end
end

#call_error_for_request_block(request, parsed_response) ⇒ Object



77
78
79
# File 'lib/pachube-stream/client.rb', line 77

def call_error_for_request_block(request, parsed_response)
  request.on_error_block.call(parsed_response) if request.on_error_block
end

#parse_response(response) ⇒ Hash



39
40
41
42
43
44
45
46
47
# File 'lib/pachube-stream/client.rb', line 39

def parse_response(response)
  begin
    Yajl::Parser.parse(response)
  rescue Exception => e
    receive_error("#{e.class}: " + [e.message, e.backtrace].flatten.join("\n\t"))
    @conn.close_connection
    return
  end
end

#process_data(response) ⇒ Hash



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pachube-stream/client.rb', line 18

def process_data(response)
  parsed_response = parse_response(response)
  status_and_no_ok = parsed_response["status"] && parsed_response["status"] != 200
  if request = @requests[parsed_response["token"]]
    if status_and_no_ok
      call_error_for_request_block(request, parsed_response)
    else
      call_block_for_request(request, parsed_response)
    end
  else
    if status_and_no_ok
      receive_error(parsed_response)
    else
      @conn.on_response_block.call(parsed_response) if @conn.on_response_block 
    end
  end
end

#receive_error(error) ⇒ Object



49
50
51
# File 'lib/pachube-stream/client.rb', line 49

def receive_error(error)
  @conn.on_error_block.call(error) if @conn.on_error_block
end

#send_request(method, resource, html_request = {}, token = nil, &block) ⇒ Object

we send the request and also keep the request with the token as its key so we can attach callback to requests



55
56
57
58
59
60
# File 'lib/pachube-stream/client.rb', line 55

def send_request(method, resource, html_request = {}, token = nil, &block)
  @request = Request.new(@api_key, method, resource, html_request, token)
  @requests[@request.token] = @request
  @conn.send_data(@request.to_json)
  @request
end