class Threescale::API

Attributes

conn[RW]
path[RW]
provider_key[RW]
url[RW]

Public Class Methods

new(provider_key = nil) click to toggle source
# 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

Public Instance Methods

activate_user(account_id, user_id) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 191
def activate_user(account_id, user_id)
  response = @conn.put "/admin/api/accounts/#{account_id}/users/#{user_id}/activate.xml", {
    :provider_key => @provider_key}
  response.status == 201
end
approve_account(account_id) click to toggle source

Account API methods

# File lib/3scale_api/3scale/api.rb, line 131
def approve_account(account_id)
  response = @conn.put "/admin/api/accounts/#{account_id}/approve.xml", {
    :provider_key => @provider_key}
  response.status == 201
end
change_role_to_admin(account_id, user_id) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 197
def change_role_to_admin(account_id, user_id)
  response = @conn.put "/admin/api/accounts/#{account_id}/users/#{user_id}/admin.xml", {
    :provider_key => @provider_key}
  response.status == 201
end
change_role_to_member(account_id, user_id) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 203
def change_role_to_member(account_id, user_id)
  response = @conn.put "/admin/api/accounts/#{account_id}/users/#{user_id}/member.xml", {
    :provider_key => @provider_key}
  response.status == 201
end
create_application(account_id, plan_id, name, description = nil) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 28
def create_application(account_id, plan_id, name, description = nil)
  response = conn.post "/admin/api/accounts/#{account_id}/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) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 185
def create_user(account_id, email, password, username)
  response = @conn.post "/admin/api/accounts/#{account_id}/users.xml", {:provider_key => @provider_key,
    :username => username, :password => password, :email => email}
  response.status == 201
end
delete_application_key(account_id, application_id, key) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 43
def delete_application_key(account_id, application_id, key)
  response = @conn.delete "/admin/api/accounts/#{account_id}/applications/#{application_id}/keys/#{key}.xml", {
  :provider_key => @provider_key }
  response.status == 200
end
generate_application_key(account_id, application_id) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 49
def generate_application_key(account_id, application_id)
  new_key = SecureRandom.hex(16)
  response = conn.post "/admin/api/accounts/#{account_id}/applications/#{application_id}/keys.xml", {
                                                                                                      :provider_key => @provider_key ,
                                                                                                      :key => new_key }
  response.status == 201
end
get_account_plans() click to toggle source
# File lib/3scale_api/3scale/api.rb, line 169
def get_account_plans
  response = @conn.get "/admin/api/account_plans.xml", {:provider_key => @provider_key}
  return false if response.status != 200
  xml = Nokogiri::XML(response.body)
  account_plans = Array.new
  xml.xpath("//plans/plan").map do |account_plan|
    if account_plan.css("state").text == "published"
      account_plans.push({
        :name => account_plan.css("name").text,
        :account_plan_id => account_plan.css("id").text
      })
    end
  end
  account_plans
end
get_application_keys(account_id, application_id) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 57
def get_application_keys(account_id, application_id)
  response = @conn.get "/admin/api/accounts/#{account_id}/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) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 69
def get_application_list(account_id)
  results = Array.new
  response = @conn.get "/admin/api/accounts/#{account_id}/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').first.text,
    :name => application.css('name').text,
    :application_id => application.css('application_id').text,
    :plan_type => application.css('plan name').text,
    :service_id => application.css('service_id').first.text
    })
  end
  results
end
get_service_plans() click to toggle source
# File lib/3scale_api/3scale/api.rb, line 91
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_service_plans_by_() click to toggle source
# File lib/3scale_api/3scale/api.rb, line 104
def get_service_plans_by_
  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_services() click to toggle source
# File lib/3scale_api/3scale/api.rb, line 117
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
load_application_data(account_id, application_id) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 209
def load_application_data(account_id, application_id)
  response = @conn.get "/admin/api/accounts/#{account_id}/applications/#{application_id}.xml",{
    :provider_key => @provider_key}
  return false if response.status != 200
  xml = Nokogiri::XML(response.body)
  application_data = {:keys => Array.new}
  xml.xpath("//keys/key").map do |key|
    application_data[:keys].push key.text
  end
  application_data[:app_id] = xml.css("application application_id").text
  application_data[:name] = xml.css("application name").text
  application_data[:description] = xml.css("application description").text
  application_data
end
signup_express( email, org_name, password, username, additional_fields = nil) click to toggle source
# File lib/3scale_api/3scale/api.rb, line 137
def signup_express( email, org_name, password, username, additional_fields = nil)
  params = {:provider_key => @provider_key, :username => username, :password => password, :email => email,
    :org_name => org_name}
  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

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