Class: Zillabyte::API

Inherits:
Object
  • Object
show all
Defined in:
lib/zillabyte/api/settings.rb,
lib/zillabyte/api.rb

Defined Under Namespace

Classes: Base, Data, Flows, Logs, Metrics, Queries, SemanticTags, Sources, Zillalogs

Constant Summary collapse

CASPERJS_BIN =

PY_VIRTUAL_ENV_DIR = "/tmp/vEnv"

"/usr/local/bin/casperjs"
API_CLIENT_JS =
File.join(ENV['HOME'], "zb1/api.client.js/api.coffee")
NODEJS_BIN =
"/usr/local/bin/node"
HEADERS =
{
  'Accept'                => 'application/json',
  'Content-Type'          => 'application/json',
  # 'Accept-Encoding'       => 'gzip',
  'User-Agent'            => "zillabyte/#{Zillabyte::CLI::VERSION}",
  'X-Ruby-Version'        => RUBY_VERSION,
  'X-Ruby-Platform'       => RUBY_PLATFORM
}
OPTIONS =
{
  :headers  => {},
  :host     => ENV['ZILLABYTE_API_HOST'] || 'api.zillabyte.com',
  :port     => (ENV['ZILLABYTE_API_PORT'] || '80').to_i,
  :nonblock => false,
  :scheme   => 'http'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zillabyte/api.rb', line 40

def initialize(options={})
  options = OPTIONS.merge(options)

  @progress = options.delete(:progress)
  @api_key = options.delete(:api_key) || ENV['ZILLABYTE_API_KEY']
  
  options[:headers] = HEADERS.merge({ 
    'Authorization' => "auth_token #{@api_key}",
  }).merge(options[:headers])

  @options = options.clone
  @options[:bubble_exceptions] = ENV['BUBBLE_EXCEPTIONS'] || false

  @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options)
  
end

Class Method Details

.load ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/zillabyte/api.rb', line 155

def self.load
  Dir[File.join(File.dirname(__FILE__), "api", "*.rb")].sort.each do |file|
    require file
  end
  
  Dir[File.join(File.dirname(__FILE__), "api", "helpers", "*.rb")].sort.each do |file|
    require file
  end
end

Instance Method Details

#collectors ⇒ Object Also known as: collector



141
142
143
# File 'lib/zillabyte/api.rb', line 141

def collectors
  @_collectors ||= ::Zillabyte::API::Collectors.new(self)
end

#data ⇒ Object



106
107
108
# File 'lib/zillabyte/api.rb', line 106

def data
  @_data ||= ::Zillabyte::API::Data.new(self)
end

#flows ⇒ Object Also known as: flow



120
121
122
# File 'lib/zillabyte/api.rb', line 120

def flows
  @_flows ||= ::Zillabyte::API::Flows.new(self)
end

#host ⇒ Object



58
59
60
# File 'lib/zillabyte/api.rb', line 58

def host
  @options[:host]
end

#logs ⇒ Object



110
111
112
# File 'lib/zillabyte/api.rb', line 110

def logs
  @_logs ||= ::Zillabyte::API::Logs.new(self)
end

#metrics ⇒ Object Also known as: metric



125
126
127
# File 'lib/zillabyte/api.rb', line 125

def metrics
  @_metrics ||= ::Zillabyte::API::Metrics.new(self)
end

#queries ⇒ Object Also known as: query



115
116
117
# File 'lib/zillabyte/api.rb', line 115

def queries
  @_queries ||= ::Zillabyte::API::Queries.new(self)
end

#request(params, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zillabyte/api.rb', line 63

def request(params, &block)
  begin
# The @connection object handles connections to api.zillabyte.com via Excon, this sends a command to the Rails api code
# with parameters in params (one of which contains the path to the method to be run in the form of a url, e.g. if the
# :path parameter is set to /functions/#{id}?tar=1, then the show method in functions_controller.rb will be automatically
# called...it will have it's own set of parameters which correspond to the items in the query string).
    response = @connection.request(params, &block)
  rescue Excon::Errors::HTTPStatusError => error
    @progress.error "internal server error" if @progress
    raise error
  end
  
  if response.body && !response.body.empty?
    # decompress_response!(response)
    begin
      response.body = JSON.parse(response.body)
    rescue Exception => e
      raise e if @options[:bubble_exceptions]
      @progress.error "unknown server response: \n #{response.body}" if @progress
    end
  end
  
  # Errors?
  if response.body.is_a?(Hash)
    if response.body["status"] == "error"
      if @progress
        if response.body["error"] == "authentication_error"
          @progress.error "authentication error. run 'zillabyte login'"
        else
          @progress.error response.body["error_message"] 
        end
      end
    end
  end
  

  # reset (non-persistent) connection
  @connection.reset

  response
end

#semantic_tags ⇒ Object



146
147
148
# File 'lib/zillabyte/api.rb', line 146

def semantic_tags
  @_rules ||= ::Zillabyte::API::SemanticTags.new(self)
end

#sources ⇒ Object Also known as: source



135
136
137
# File 'lib/zillabyte/api.rb', line 135

def sources
  @_sources ||= ::Zillabyte::API::Sources.new(self)
end

#zillalogs ⇒ Object Also known as: zillalog



130
131
132
# File 'lib/zillabyte/api.rb', line 130

def zillalogs
  @_rules ||= ::Zillabyte::API::Zillalogs.new(self)
end