Class: Anamo::Api

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

Instance Method Summary collapse

Constructor Details

#initializeApi

Returns a new instance of Api.



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

def initialize
  @api_base_url = 'https://anamo.io/api'
  @config = YAML.load_file "/etc/anamo.conf.yml"
  @client_key = File.exists?("/etc/anamo.client.key") ? File.open("/etc/anamo.client.key", 'rb') { |f| f.read } : nil
end

Instance Method Details

#do_multipart_post(path, files) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/anamo/api.rb', line 32

def do_multipart_post path, files
  uri = make_uri path
  request = Net::HTTP::Post::Multipart.new uri.path, files
  request['X-UPTB-Client-Key'] = @client_key if @client_key
  Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
    http.request(request)
  end
end

#do_post(path, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/anamo/api.rb', line 22

def do_post path, &block
  uri = make_uri path
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri)
  request['X-UPTB-Client-Key'] = @client_key if @client_key
  instance_exec request, http, &block
  http.request(request)
end

#make_uri(path) ⇒ Object



18
19
20
# File 'lib/anamo/api.rb', line 18

def make_uri path
  URI.parse make_url path
end

#make_url(path) ⇒ Object



14
15
16
# File 'lib/anamo/api.rb', line 14

def make_url path
  "#{@api_base_url}/#{path}"
end

#post_fstree(data_files) ⇒ Object



41
42
43
44
45
# File 'lib/anamo/api.rb', line 41

def post_fstree data_files
  response = do_multipart_post 'fstree', data_files
  raise 'Send failure' if !response.is_a?(Net::HTTPOK)
  response
end

#post_groups(data) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/anamo/api.rb', line 56

def post_groups data
  response = do_post 'groups' do |request|
    request.body = data
    request["Content-Type"] = "text/plain"
  end
  raise 'Send failure' if !response.is_a?(Net::HTTPOK)
  response
end

#post_node(data) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/anamo/api.rb', line 83

def post_node data
  response = do_post 'node' do |request|
    request.body = data
    request["Content-Type"] = "application/json"
  end
  #raise 'Send failure' if !response.is_a?(Net::HTTPOK)
  response
end

#post_pkgver(data) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/anamo/api.rb', line 74

def post_pkgver data
  response = do_post 'pkgver' do |request|
    request.body = data
    request["Content-Type"] = "application/json"
  end
  raise 'Send failure' if !response.is_a?(Net::HTTPOK)
  response
end

#post_ports(data) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/anamo/api.rb', line 65

def post_ports data
  response = do_post 'ports' do |request|
    request.body = data
    request["Content-Type"] = "application/json"
  end
  raise 'Send failure' if !response.is_a?(Net::HTTPOK)
  response
end

#post_users(data) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/anamo/api.rb', line 47

def post_users data
  response = do_post 'users' do |request|
    request.body = data
    request["Content-Type"] = "text/plain"
  end
  raise 'Send failure' if !response.is_a?(Net::HTTPOK)
  response
end