Class: Threescale::API

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale_api/3scale/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider_key = nil) ⇒ API

Returns a new instance of API.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/3scale_api/3scale/api.rb', line 8

def initialize(provider_key = nil)
  if ENV['THREESCALE_URL']
    @url = ENV['THREESCALE_URL']
  else
    raise ("Please set your 3 Scale URL as an environmental variable THREESCALE_URL")
  end
  if not provider_key
    if ENV['THREESCALE_PROVIDER_KEY']
      provider_key = ENV['THREESCALE_PROVIDER_KEY']
    end
    raise Error, "You must provide a 3 Scale provider key" if not provider_key
  end
  @provider_key = provider_key
  @conn = Faraday.new(url = @url) do | faraday|
    faraday.request :url_encoded
    faraday.response :logger
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



7
8
9
# File 'lib/3scale_api/3scale/api.rb', line 7

def conn
  @conn
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/3scale_api/3scale/api.rb', line 7

def path
  @path
end

#provider_keyObject

Returns the value of attribute provider_key.



7
8
9
# File 'lib/3scale_api/3scale/api.rb', line 7

def provider_key
  @provider_key
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/3scale_api/3scale/api.rb', line 7

def url
  @url
end

Instance Method Details

#activate_user(account_id, user_id) ⇒ Object



178
179
180
181
182
# File 'lib/3scale_api/3scale/api.rb', line 178

def activate_user(, user_id)
  response = @conn.put "/admin/api/accounts/#{}/users/#{user_id}/activate.xml", {
    :provider_key => @provider_key}
  response.status == 201
end

#approve_account(account_id) ⇒ Object

Account API methods



116
117
118
119
120
# File 'lib/3scale_api/3scale/api.rb', line 116

def ()
  response = @conn.put "/admin/api/accounts/#{}/approve.xml", {
    :provider_key => @provider_key}
  response.status == 201
end

#change_role_to_admin(account_id, user_id) ⇒ Object



184
185
186
187
188
# File 'lib/3scale_api/3scale/api.rb', line 184

def change_role_to_admin(, user_id)
  response = @conn.put "/admin/api/accounts/#{}/users/#{user_id}/admin.xml", {
    :provider_key => @provider_key}
  response.status == 201
end

#change_role_to_member(account_id, user_id) ⇒ Object



190
191
192
193
194
# File 'lib/3scale_api/3scale/api.rb', line 190

def change_role_to_member(, user_id)
  response = @conn.put "/admin/api/accounts/#{}/users/#{user_id}/member.xml", {
    :provider_key => @provider_key}
  response.status == 201
end

#create_application(account_id, plan_id, name, description = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/3scale_api/3scale/api.rb', line 28

def create_application(, plan_id, name, description = nil)
  response = conn.post "/admin/api/accounts/#{}/applications.xml", {
    :provider_key => @provider_key ,
    :name => name,
    :description => description,
    :plan_id => plan_id}
  return false if response.status != 201
  xml = Nokogiri::XML(response.body)
  result = {
      :app_id => xml.css("application application_id").text ,
      :application_id => xml.css("application id").text,
      :keys => [xml.css("application keys key").text]
  }
end

#create_user(account_id, email, password, username) ⇒ Object



172
173
174
175
176
# File 'lib/3scale_api/3scale/api.rb', line 172

def create_user(, email, password, username)
  response = @conn.post "/admin/api/accounts/#{}/users.xml", {:provider_key => @provider_key,
    :username => username, :password => password, :email => email}
  response.status == 201
end

#delete_application_key(account_id, application_id, key) ⇒ Object



43
44
45
46
47
# File 'lib/3scale_api/3scale/api.rb', line 43

def delete_application_key(, application_id, key)
  response = @conn.delete "/admin/api/accounts/#{}/applications/#{application_id}/keys/#{key}.xml", {
  :provider_key => @provider_key }
  response.status == 200
end

#generate_application_key(account_id, application_id) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/3scale_api/3scale/api.rb', line 49

def generate_application_key(, application_id)
  new_key = SecureRandom.hex(16)
  response = conn.post "/admin/api/accounts/#{}/applications/#{application_id}/keys.xml", {
                                                                                                      :provider_key => @provider_key ,
                                                                                                      :key => new_key }
  response.status == 201
end

#get_account_plansObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/3scale_api/3scale/api.rb', line 156

def 
  response = @conn.get "/admin/api/account_plans.xml", {:provider_key => @provider_key}
  return false if response.status != 200
  xml = Nokogiri::XML(response.body)
   = Array.new
  xml.xpath("//plans/plan").map do ||
    if .css("state").text == "published"
      .push({
        :name => .css("name").text,
        :account_plan_id => .css("id").text
      })
    end
  end
  
end

#get_application_keys(account_id, application_id) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/3scale_api/3scale/api.rb', line 57

def get_application_keys(, application_id)
  response = @conn.get "/admin/api/accounts/#{}/applications/#{application_id}/keys.xml", {
  :provider_key => @provider_key, }
  p response.status
  return [] if response.status != 200
  xml = Nokogiri::XML(response.body)
  nodes = xml.xpath('keys/key')
  nodes.css('value').map do |key|
    key.text
  end
end

#get_application_list(account_id) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/3scale_api/3scale/api.rb', line 69

def get_application_list()
  results = Array.new
  response = @conn.get "/admin/api/accounts/#{}/applications.xml", {:provider_key => @provider_key, }
  return [] if response.status != 200
  xml = Nokogiri::XML(response.body)
  applications = xml.xpath('applications/application')
  applications.each do |application|
    keys = application.xpath("//keys/key").map do |key|
      key.text
    end
    results.push(
    {:keys => keys,
    :id => application.css('id').text,
    :name => application.css('name').text,
    :application_id => application.css('application_id').text,
    :plan_type => application.css('plan name').text})
  end
  results
end

#get_service_plansObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/3scale_api/3scale/api.rb', line 89

def get_service_plans
  results = Array.new
  response = @conn.get "/admin/api/application_plans.xml", {:provider_key => @provider_key }
  xml = Nokogiri::XML(response.body)
  plans = xml.xpath("//plans/plan")
  plans = plans.map do |plan|
    {
      :name => plan.css("name").text,
      :service_plan_id => plan.css("service_id").text
    }
  end
end

#get_servicesObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/3scale_api/3scale/api.rb', line 102

def get_services
  results = Array.new
  response = @conn.get "/admin/api/services.xml", {:provider_key => @provider_key }
  xml = Nokogiri::XML(response.body)
  services = xml.xpath("//services/service")
  services.map do |service|
    {
        :name => service.css("name").first.text,
        :service_id => service.css("id").first.text
    }
  end
end

#signup_express(account_plan_id, application_plan_id, email, org_name, password, service_plan_id, username, additional_fields = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/3scale_api/3scale/api.rb', line 122

def (, application_plan_id, email, org_name, password, service_plan_id, username,
  additional_fields = nil)
  params = {:provider_key => @provider_key, :username => username, :password => password, :email => email,
    :org_name => org_name, :account_plan_id => , :service_plan_id => service_plan_id,
    :application_plan_id => application_plan_id}
  if (additional_fields)
    additional_fields.each do |key, value|
      params[key] = value
    end
  end
  response = @conn.post "/admin/api/signup.xml", params
  xml = Nokogiri::XML(response.body)
  result = {
      :success => false
  }
  if response.status == 422
    errors =  xml.xpath("//errors/error").map do |error|
      error.text
    end
    result[:errors] = errors
  end
  return result if response.status != 201
  result[:success] = true

   = xml.xpath('//account/id').first.text
  user_id = xml.xpath('//account/users/user/id').text
  self. 
  results = self.get_application_list 
  results[0][:user_id] = user_id.to_s
  result[:account_info] = results
  result[:account_id] = 
  result
end