Class: Addons::Client

Inherits:
Object
  • Object
show all
Extended by:
Mock::Methods, Mock::Responses
Defined in:
lib/addons-client/mock.rb,
lib/addons-client/client.rb,
lib/addons-client/version.rb,
lib/addons-client/response.rb

Defined Under Namespace

Modules: Mock Classes: Response

Constant Summary collapse

DEFAULT_CONSUMER_ID =
"api-client@localhost"
VERSION =
"0.0.10"

Class Method Summary collapse

Methods included from Mock::Methods

mock!, mocked?, unmock!

Class Method Details

.api_urlObject



7
8
9
# File 'lib/addons-client/client.rb', line 7

def self.api_url
  @api_url = validate_api_url!
end

.deprovision!(resource_id) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/addons-client/client.rb', line 61

def self.deprovision!(resource_id)
  validate_authenticated_api_url!
  wrap_request do
    if mocked?
      mocked_deprovision(resource_id)
    else
      resource["/resources/#{resource_id}"].delete :accept => :json
    end
  end
end

.list(search = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/addons-client/client.rb', line 11

def self.list(search = nil)
  wrap_request do
    if mocked?
      mocked_list(search)
    else
      payload = { :accept => :json }
      if search.is_a? String
        payload[:params] = { :search => search } unless search == ""
      elsif !search.nil?
        payload[:params] = search
      end
      resource["/addons"].get payload
    end
  end
end

.plan_change!(resource_id, plan) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/addons-client/client.rb', line 72

def self.plan_change!(resource_id, plan)
  validate_authenticated_api_url!
  wrap_request do
    if mocked?
      mocked_plan_change(resource_id, plan)
    else
      payload = {
        :plan => plan,
      }
      resource["/resources/#{resource_id}"].put payload, :accept => :json
    end
  end
end

.provision!(slug, opts = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/addons-client/client.rb', line 27

def self.provision!(slug, opts = {})
  validate_authenticated_api_url!
  wrap_request do
    addon_name, plan  = slug.split(':')
    raise UserError, "No add-on name given" unless addon_name

    if mocked?
      mocked_provision(addon_name)
    else
      payload = {
        :addon => addon_name,
        :consumer_id => opts[:consumer_id] || DEFAULT_CONSUMER_ID
      }
      payload.merge! :plan => plan if plan
      payload.merge! :options => opts[:options] if opts[:options]
      payload.merge! :rate => opts[:rate] if opts[:rate]
      payload.merge! :addons_id => opts[:addons_id] if opts[:addons_id]
      if start_at = opts[:start_at]
        start_at.utc
        payload.merge! :start_at => start_at.to_s
      end
      if end_at = opts[:end_at]
        end_at.utc
        payload.merge! :end_at => end_at.to_s
      end
      if config = opts[:config]
        config = config.to_json unless config.is_a? String
        payload.merge! :config => config
      end
      resource["/resources"].post payload, :accept => :json
    end
  end
end

.resourceObject



86
87
88
# File 'lib/addons-client/client.rb', line 86

def self.resource
  RestClient::Resource.new(api_url.to_s)
end