Class: Megam::API

Inherits:
Object
  • Object
show all
Defined in:
lib/megam/api.rb,
lib/megam/api/logs.rb,
lib/megam/api/login.rb,
lib/megam/api/nodes.rb,
lib/megam/api/errors.rb,
lib/megam/api/appreqs.rb,
lib/megam/api/predefs.rb,
lib/megam/api/version.rb,
lib/megam/api/accounts.rb,
lib/megam/api/appdefns.rb,
lib/megam/api/boltreqs.rb,
lib/megam/api/requests.rb,
lib/megam/api/boltdefns.rb,
lib/megam/api/cloud_tools.rb,
lib/megam/api/predef_clouds.rb

Defined Under Namespace

Modules: Errors

Constant Summary collapse

HEADERS =
{
  'Accept' => 'application/json',
  'Accept-Encoding' => 'gzip',
  'User-Agent' => "megam-api/#{Megam::API::VERSION}",
  'X-Ruby-Version' => RUBY_VERSION,
  'X-Ruby-Platform' => RUBY_PLATFORM
}
OPTIONS =
{
  :headers => {},
  :host => 'api.megam.co',
  :nonblock => false,
  :scheme => 'https'
}
API_VERSION1 =
"/v1"
VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

It is assumed that every API call will use an API_KEY/email. This ensures validity of the person really the same guy on who he claims. 3 levels of options exits

  1. The global OPTIONS as available inside the API (OPTIONS)
  2. The options as passed via the instantiation of API will override global options. The ones that are passed are :email and :api_key and will be merged into a class variable @options
  3. Upon merge of the options, the api_key, email as available in the @options is deleted.

Raises:

  • (ArgumentError)


104
105
106
107
108
109
# File 'lib/megam/api.rb', line 104

def initialize(options={})
  @options = OPTIONS.merge(options)
  @api_key = @options.delete(:api_key) || ENV['MEGAM_API_KEY']
  @email = @options.delete(:email)
  raise ArgumentError, "You must specify [:email, :api_key]" if @email.nil? || @api_key.nil?
end

Instance Attribute Details

#textObject

text is used to print stuff in the terminal (message, log, info, warn etc.)



70
71
72
# File 'lib/megam/api.rb', line 70

def text
  @text
end

Instance Method Details

#delete_node(node_id) ⇒ Object

Yet to be tested DELETE /nodes/:node_id



38
39
40
41
42
43
44
45
46
47
# File 'lib/megam/api/nodes.rb', line 38

def delete_node(node_id)
  @options = {:path => '/nodes/#{node_id}',
    :body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :delete,
    :body     => @options[:body]
  )
end

#get_accounts(email) ⇒ Object

GET /accounts Yet to be tested



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/megam/api/accounts.rb', line 5

def get_accounts(email)

  @options = {:path => "/accounts/#{email}",
    :body => ''}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_appdefn(node_name) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/megam/api/appdefns.rb', line 4

def get_appdefn(node_name)
  @options = {:path => "/appdefns/#{node_name}",:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_appreq(node_name) ⇒ Object

=begin



5
6
7
8
9
10
11
12
13
# File 'lib/megam/api/appreqs.rb', line 5

def get_appreq(node_name)
  @options = {:path => "/appreqs/#{node_name}",:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_boltdefn(boltdefns_id) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/megam/api/boltdefns.rb', line 5

def get_boltdefn(boltdefns_id)
  @options = {:path => "/boltdefns/#{boltdefns_id}",:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_boltreq(node_name) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/megam/api/boltreqs.rb', line 5

def get_boltreq(node_name)
  @options = {:path => "/boltreqs/#{node_name}",:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_cloudtool(cloudtool_name) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/megam/api/cloud_tools.rb', line 13

def get_cloudtool(cloudtool_name)
  @options = {:path => "/cloudtools/#{cloudtool_name}",:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_cloudtoolsObject



3
4
5
6
7
8
9
10
11
# File 'lib/megam/api/cloud_tools.rb', line 3

def get_cloudtools
  @options = {:path => '/cloudtools',:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_logs(node_id) ⇒ Object

PUT /logs/:node_id empty body posted to get the logs path having the node_id which is the key_name to fetch that particular log



6
7
8
9
10
11
12
13
14
15
# File 'lib/megam/api/logs.rb', line 6

def get_logs(node_id)
  @options = {:path => "/logs/#{node_id}",
   :body => OkJson.encode({:nothing =>"nothing in body"})}.merge(@options)

  request(
    :expects  => 200,
    :method   => :post,
    :body     => @options[:body]
  )
end

#get_node(node_id) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/megam/api/nodes.rb', line 15

def get_node(node_id)
  @options = {:path => "/nodes/#{node_id}",:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_nodesObject

GET /nodes



5
6
7
8
9
10
11
12
13
# File 'lib/megam/api/nodes.rb', line 5

def get_nodes
  @options = {:path => '/nodes',:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_predef(predef_name) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/megam/api/predefs.rb', line 13

def get_predef(predef_name)
  @options = {:path => "/predefs/#{predef_name}",:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_predefcloud(predefcloud_name) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/megam/api/predef_clouds.rb', line 13

def get_predefcloud(predefcloud_name)
  @options = {:path => "/predefclouds/#{predefcloud_name}",:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_predefcloudsObject



3
4
5
6
7
8
9
10
11
# File 'lib/megam/api/predef_clouds.rb', line 3

def get_predefclouds
  @options = {:path => '/predefclouds',:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_predefsObject



3
4
5
6
7
8
9
10
11
# File 'lib/megam/api/predefs.rb', line 3

def get_predefs
  @options = {:path => '/predefs',:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_request(node_name) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/megam/api/requests.rb', line 15

def get_request(node_name)
  @options = {:path => "/requests/#{node_name}",:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#get_requestsObject

GET /requests



5
6
7
8
9
10
11
12
13
# File 'lib/megam/api/requests.rb', line 5

def get_requests
  @options = {:path => '/requests',:body => ""}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end

#last_responseObject



93
94
95
# File 'lib/megam/api.rb', line 93

def last_response
  @last_response
end

#post_accounts(new_account) ⇒ Object

The body content needs to be a json.



18
19
20
21
22
23
24
25
26
27
# File 'lib/megam/api/accounts.rb', line 18

def post_accounts()
  @options = {:path => '/accounts/content',
  :body => Megam::JSONCompat.to_json()}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
    )
end

#post_appdefn(new_appdefn) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/megam/api/appdefns.rb', line 14

def post_appdefn(new_appdefn)
  @options = {:path => '/appdefns/content',
    :body => Megam::JSONCompat.to_json(new_appdefn)}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
  )
end

#post_appreq(new_appreq) ⇒ Object

=end



15
16
17
18
19
20
21
22
23
24
# File 'lib/megam/api/appreqs.rb', line 15

def post_appreq(new_appreq)
  @options = {:path => '/appreqs/content',
    :body => Megam::JSONCompat.to_json(new_appreq)}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
  )
end

#post_authObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/megam/api/login.rb', line 3

def post_auth
  @options = {:path => '/auth', :body => ""}.merge(@options)

  request(
    :expects  => 200,
   :method   => :post,
    :body     => @options[:body]

    )
end

#post_boltdefn(new_boltdefn) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/megam/api/boltdefns.rb', line 15

def post_boltdefn(new_boltdefn)
  @options = {:path => '/boltdefns/content',
    :body => Megam::JSONCompat.to_json(new_boltdefn)}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
  )
end

#post_boltreq(new_boltreq) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/megam/api/boltreqs.rb', line 15

def post_boltreq(new_boltreq)
  @options = {:path => '/boltreqs/content',
    :body => Megam::JSONCompat.to_json(new_boltreq)}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
  )
end

#post_cloudtool(new_cloudtool) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/megam/api/cloud_tools.rb', line 23

def post_cloudtool(new_cloudtool)
  @options = {:path => '/cloudtools/content',
    :body => Megam::JSONCompat.to_json(new_cloudtool)}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
  )
end

#post_node(new_node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/megam/api/nodes.rb', line 25

def post_node(new_node)
  @options = {:path => '/nodes/content',
    :body => Megam::JSONCompat.to_json(new_node)}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
  )
end

#post_predef(new_predef) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/megam/api/predefs.rb', line 23

def post_predef(new_predef)
  @options = {:path => '/predefs/content',
    :body => Megam::JSONCompat.to_json(new_predef)}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
  )
end

#post_predefcloud(new_predefcloud) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/megam/api/predef_clouds.rb', line 23

def post_predefcloud(new_predefcloud)
  @options = {:path => '/predefclouds/content',
    :body => Megam::JSONCompat.to_json(new_predefcloud)}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
  )
end

#post_request(new_req) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/megam/api/requests.rb', line 25

def post_request(new_req)
  @options = {:path => '/requests/content',
    :body => Megam::JSONCompat.to_json(new_req)}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
  )
end

#request(params, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/megam/api.rb', line 111

def request(params,&block)
  start = Time.now
  text.msg "#{text.color("START", :cyan, :bold)}"
  params.each do |pkey, pvalue|
    text.msg("> #{pkey}: #{pvalue}")
  end

  begin
    response = connection.request(params, &block)
  rescue Excon::Errors::HTTPStatusError => error
    klass = case error.response.status

    when 401 then Megam::API::Errors::Unauthorized
    when 403 then Megam::API::Errors::Forbidden
    when 404 then Megam::API::Errors::NotFound
    when 408 then Megam::API::Errors::Timeout
    when 422 then Megam::API::Errors::RequestFailed
    when 423 then Megam::API::Errors::Locked
    when /50./ then Megam::API::Errors::RequestFailed
    else Megam::API::Errors::ErrorWithResponse
    end
    reerror = klass.new(error.message, error.response)
    reerror.set_backtrace(error.backtrace)
    text.msg "#{text.color("#{reerror.response.body}", :white)}"
    reerror.response.body = Megam::JSONCompat.from_json(reerror.response.body.chomp)
    text.msg("#{text.color("RESPONSE ERR: Ruby Object", :magenta, :bold)}")
    text.msg "#{text.color("#{reerror.response.body}", :white, :bold)}"
    raise(reerror)
  end

  @last_response = response
  text.msg("#{text.color("RESPONSE: HTTP Status and Header Data", :magenta, :bold)}")
  text.msg("> HTTP #{response.remote_ip} #{response.status}")

  response.headers.each do |header, value|
    text.msg("> #{header}: #{value}")
  end
  text.info("End HTTP Status/Header Data.")

  if response.body && !response.body.empty?
    if response.headers['Content-Encoding'] == 'gzip'
      response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read
    end
    text.msg("#{text.color("RESPONSE: HTTP Body(JSON)", :magenta, :bold)}")

    text.msg "#{text.color("#{response.body}", :white)}"

    begin
      response.body = Megam::JSONCompat.from_json(response.body.chomp)
      text.msg("#{text.color("RESPONSE: Ruby Object", :magenta, :bold)}")

      text.msg "#{text.color("#{response.body}", :white, :bold)}"
    rescue Exception => jsonerr
      text.error(jsonerr)
      raise(jsonerr)
    # exception = Megam::JSONCompat.from_json(response_body)
    # msg = "HTTP Request Returned #{response.code} #{response.message}: "
    # msg << (exception["error"].respond_to?(:join) ? exception["error"].join(", ") : exception["error"].to_s)
    # text.error(msg)
    end
  end
  text.msg "#{text.color("END(#{(Time.now - start).to_s}s)", :blue, :bold)}"
  #  text.msg "#{text.color("END(#{(Megam::Stuff.time_ago(start))})", :blue, :bold)}"

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