Module: Puddle

Defined in:
lib/puddle.rb,
lib/puddle/version.rb,
lib/puddle/cli/main.rb,
lib/puddle/cli/base_cli.rb

Defined Under Namespace

Modules: Cli

Constant Summary collapse

DO_API_BASE =
'https://api.digitalocean.com'
DO_API_VERSION =
'v2'
VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_tokenObject

Returns the value of attribute api_token.



10
11
12
# File 'lib/puddle.rb', line 10

def api_token
  @api_token
end

Class Method Details

.accept_encoding_headerObject



31
32
33
# File 'lib/puddle.rb', line 31

def accept_encoding_header
  'gzip, deflate'
end

.accept_headerObject



19
20
21
# File 'lib/puddle.rb', line 19

def accept_header
  '*/*; q=0.5, application/json'
end

.accountObject



47
48
49
50
# File 'lib/puddle.rb', line 47

def 
  result = digital_ocean(:get, 'account')
  JSON.parse(result)
end

.authorization_headerObject



23
24
25
# File 'lib/puddle.rb', line 23

def authorization_header
  "Bearer #{api_token}"
end

.build_headers(headers = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/puddle.rb', line 35

def build_headers(headers = {})
  defaults = {
    'Accept'          => accept_header,
    'Accept-Encoding' => accept_encoding_header,
    'Authorization'   => authorization_header,
    'Content-Type'    => content_type_header,
    'User-Agent'      => user_agent_header,
  }

  defaults.merge(headers)
end

.content_type_headerObject



15
16
17
# File 'lib/puddle.rb', line 15

def content_type_header
  'application/json'
end

.digital_ocean(method, endpoint, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/puddle.rb', line 52

def digital_ocean(method, endpoint, options = {})
  headers = build_headers(options[:headers] || {})
  timeout = options[:timeout] || 10
  url     = "#{digital_ocean_api_url}/#{endpoint}"
  RestClient::Request.execute(
    method: method,
    url: url,
    headers: headers,
    timeout: timeout
  )
end

.digital_ocean_api_urlObject



11
12
13
# File 'lib/puddle.rb', line 11

def digital_ocean_api_url
  "#{DO_API_BASE}/#{DO_API_VERSION}"
end

.user_agent_headerObject



27
28
29
# File 'lib/puddle.rb', line 27

def user_agent_header
  'Puddle'
end