Class: Pebbles::API

Inherits:
Object
  • Object
show all
Defined in:
lib/pebbles/api.rb,
lib/pebbles/api/apps.rb,
lib/pebbles/api/user.rb,
lib/pebbles/api/login.rb,
lib/pebbles/api/errors.rb,
lib/pebbles/api/releases.rb,
lib/pebbles/api/config_vars.rb

Defined Under Namespace

Modules: Errors

Constant Summary collapse

HEADERS =
{
  'Accept'                => 'application/vnd.pebblescape+json; version=1',
  'Accept-Encoding'       => 'gzip',
  #'Accept-Language'       => 'en-US, en;q=0.8',
  'User-Agent'            => Pebbles.user_agent,
  'X-Ruby-Version'        => RUBY_VERSION,
  'X-Ruby-Platform'       => RUBY_PLATFORM
}
OPTIONS =
{
  :headers  => {},
  :host     => 'api.pebblesinspace.com',
  :nonblock => false,
  :scheme   => 'https'
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pebbles/api.rb', line 35

def initialize(options={})
  options = OPTIONS.merge(options)
  options[:headers] = HEADERS.merge(options[:headers])

  @api_key = options.delete(:api_key) || ENV['PEBBLES_API_KEY']
  if !@api_key && options.has_key?(:username) && options.has_key?(:password)
    username = options.delete(:username)
    password = options.delete(:password)
    @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options)
    @api_key = self.(username, password).body["api_key"]
  end

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

Instance Method Details

#delete_app(app) ⇒ Object

DELETE /apps/:app



5
6
7
8
9
10
11
# File 'lib/pebbles/api/apps.rb', line 5

def delete_app(app)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{app}"
  )
end

#delete_config_var(app, key) ⇒ Object

DELETE /apps/:app/config_vars/:key



5
6
7
8
9
10
11
# File 'lib/pebbles/api/config_vars.rb', line 5

def delete_config_var(app, key)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{app}/config_vars/#{escape(key)}"
  )
end

#get_app(app) ⇒ Object

GET /apps/:app



23
24
25
26
27
28
29
# File 'lib/pebbles/api/apps.rb', line 23

def get_app(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}"
  )
end

#get_app_maintenance(app) ⇒ Object

GET /apps/:app/server/maintenance



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

def get_app_maintenance(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/server/maintenance"
  )
end

#get_appsObject

GET /apps



14
15
16
17
18
19
20
# File 'lib/pebbles/api/apps.rb', line 14

def get_apps
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps"
  )
end

#get_config_vars(app) ⇒ Object

GET /apps/:app/config_vars



14
15
16
17
18
19
20
# File 'lib/pebbles/api/config_vars.rb', line 14

def get_config_vars(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/config_vars"
  )
end

#get_release(app, release) ⇒ Object

GET /apps/:app/releases/:release



14
15
16
17
18
19
20
# File 'lib/pebbles/api/releases.rb', line 14

def get_release(app, release)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/releases/#{release}"
  )
end

#get_releases(app) ⇒ Object

GET /apps/:app/releases



5
6
7
8
9
10
11
# File 'lib/pebbles/api/releases.rb', line 5

def get_releases(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/releases"
  )
end

#get_userObject

GET /user



5
6
7
8
9
10
11
# File 'lib/pebbles/api/user.rb', line 5

def get_user
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/user"
  )
end

#post_app(params = {}) ⇒ Object

POST /apps



41
42
43
44
45
46
47
48
# File 'lib/pebbles/api/apps.rb', line 41

def post_app(params={})
  request(
    :expects  => 201,
    :method   => :post,
    :path     => '/apps',
    :query    => app_params(params)
  )
end

#post_app_maintenance(app, maintenance_mode) ⇒ Object

POST /apps/:app/server/maintenance



51
52
53
54
55
56
57
58
# File 'lib/pebbles/api/apps.rb', line 51

def post_app_maintenance(app, maintenance_mode)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/server/maintenance",
    :query    => {'maintenance_mode' => maintenance_mode}
  )
end

#post_login(username, password) ⇒ Object



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

def (username, password)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => '/login',
    :query    => { 'username' => username, 'password' => password }
  )
end

#post_release(app, release = nil) ⇒ Object

POST /apps/:app/releases/:release



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

def post_release(app, release=nil)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/releases",
    :query    => {'rollback' => release}
  )
end

#put_app(app, params) ⇒ Object

PUT /apps/:app



61
62
63
64
65
66
67
68
# File 'lib/pebbles/api/apps.rb', line 61

def put_app(app, params)
  request(
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{app}",
    :query    => app_params(params)
  )
end

#put_config_vars(app, vars) ⇒ Object

PUT /apps/:app/config_vars



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

def put_config_vars(app, vars)
  request(
    :body     => MultiJson.dump(vars),
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{app}/config_vars"
  )
end

#request(params, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
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
# File 'lib/pebbles/api.rb', line 50

def request(params, &block)
  if @api_key
    params[:query] = {
      'api_key' => @api_key,
    }.merge(params[:query] || {})
  end
  
  begin
    response = @connection.request(params, &block)
  rescue Excon::Errors::HTTPStatusError => error
    klass = case error.response.status
      when 401 then Pebbles::API::Errors::Unauthorized
      when 402 then Pebbles::API::Errors::VerificationRequired
      when 403 then Pebbles::API::Errors::Forbidden
      when 404
        if error.request[:path].match /\/apps\/\/.*/
          Pebbles::API::Errors::NilApp
        else
          Pebbles::API::Errors::NotFound
        end
      when 408 then Pebbles::API::Errors::Timeout
      when 422 then Pebbles::API::Errors::RequestFailed
      when 423 then Pebbles::API::Errors::Locked
      when 429 then Pebbles::API::Errors::RateLimitExceeded
      when /50./ then Pebbles::API::Errors::RequestFailed
      else Pebbles::API::Errors::ErrorWithResponse
    end

    decompress_response!(error.response)
    reerror = klass.new(error.message, error.response)
    reerror.set_backtrace(error.backtrace)
    raise(reerror)
  end

  if response.body && !response.body.empty?
    decompress_response!(response)
    begin
      response.body = MultiJson.load(response.body)
    rescue
      # leave non-JSON body as is
    end
  end

  # reset (non-persistent) connection
  @connection.reset
  
  if response.headers.has_key?('X-Pebbles-Warning')
    Pebbles::Command.warnings.concat(response.headers['X-Pebbles-Warning'].split("\n"))
  end
      
  response
end