Class: DockerCloud::API

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/docker_cloud/api/api.rb

Constant Summary collapse

BASE_API_PATH =
'https://cloud.docker.com/api'.freeze
API_VERSION =
'v1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, type, client) ⇒ API

Returns a new instance of API.



11
12
13
14
15
# File 'lib/docker_cloud/api/api.rb', line 11

def initialize(headers, type, client)
  @headers = headers
  @type = type
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#get_from_uri(uri) ⇒ Object



50
51
52
53
54
55
# File 'lib/docker_cloud/api/api.rb', line 50

def get_from_uri(uri)
  uri = BASE_API_PATH + uri.split('/api')[1]
  response = RestClient.get(uri, headers)
  response = parse(response)
  format_object(response, self.class::TYPE)
end

#http_delete(path) ⇒ Object



39
40
41
42
# File 'lib/docker_cloud/api/api.rb', line 39

def http_delete(path)
  response = RestClient.delete(url(path), headers)
  parse(response)
end

#http_get(path, params = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/docker_cloud/api/api.rb', line 21

def http_get(path, params = {})
  query = '?' + params.map { |k, v| "#{k}=#{v}" }.join('&')
  full_path = path
  full_path += query unless params.empty?
  response = RestClient.get(url(full_path), headers)
  parse(response)
end

#http_patch(path, content = {}) ⇒ Object



34
35
36
37
# File 'lib/docker_cloud/api/api.rb', line 34

def http_patch(path, content = {})
  response = RestClient.patch(url(path), content.to_json, headers)
  parse(response)
end

#http_post(path, content = {}) ⇒ Object



29
30
31
32
# File 'lib/docker_cloud/api/api.rb', line 29

def http_post(path, content = {})
  response = RestClient.post(url(path), content.to_json, headers)
  parse(response)
end

#parse(response) ⇒ Object



44
45
46
47
48
# File 'lib/docker_cloud/api/api.rb', line 44

def parse(response)
  hash = JSON.parse(response, symbolize_names: true)
  hash.delete(:meta)
  hash[:objects].nil? ? hash : hash[:objects]
end

#url(path) ⇒ Object



17
18
19
# File 'lib/docker_cloud/api/api.rb', line 17

def url(path)
  BASE_API_PATH + '/' + @type + '/' + API_VERSION + path
end