Class: Dscli::API

Inherits:
Object
  • Object
show all
Defined in:
lib/dscli/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAPI

Returns a new instance of API.



8
9
10
11
12
13
# File 'lib/dscli/api.rb', line 8

def initialize
  storage = Dscli::Storage.new
  config = storage.get_auth
  @config = {:username => config[:auth][:username], :api_key => config[:auth][:api_key], :enable_ssl => false}
  @datasift = DataSift::Client.new(@config)
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



6
7
8
# File 'lib/dscli/api.rb', line 6

def hash
  @hash
end

Instance Method Details

#compile(csdl) ⇒ Object

Core API endpoints

Missing /validate /balance


19
20
21
22
23
# File 'lib/dscli/api.rb', line 19

def compile(csdl)
  response = @datasift.compile csdl
  definition = response[:data]
  definition[:hash]
end

#dpu(hash) ⇒ Object



30
31
32
33
# File 'lib/dscli/api.rb', line 30

def dpu(hash)
  response = @datasift.dpu hash
  dpu = response[:data]
end

#stream(hash) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dscli/api.rb', line 35

def stream(hash)
  on_delete  = lambda { |stream, m| puts m }
  on_error   = lambda { |stream, e| puts 'A serious error has occurred: ' + e.message.to_s }
  on_message = lambda { |message, stream, hash| puts Yajl::Encoder.encode(message) }
  on_connect = lambda { |stream| stream.subscribe(hash, on_message) }
  on_close   = lambda { |stream| puts 'closed' }

  on_datasift_message = lambda do |stream, message, hash|
    puts "DataSift Message #{hash} ==> #{message}"
  end

  conn = DataSift::new_stream(@config, on_delete, on_error, on_connect, on_close)
  conn.on_datasift_message = on_datasift_message
  conn.stream.read_thread.join

  rescue DataSiftError => dse
    puts "Error #{dse.message}"
    case dse
      when ConnectionError
      when AuthError
      when BadRequestError
        puts "Error #{dse.message}"
    end
end

#usage(period) ⇒ Object



25
26
27
28
# File 'lib/dscli/api.rb', line 25

def usage(period)
  response = @datasift.usage period
  usage = response[:data]
end