<commit_before>require 'faraday'
require 'securerandom'
require 'nokogiri'

module Threescale
  class API
    attr_accessor :provider_key, :url, :path, :conn
    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

    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

    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

    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

    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

    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').text,
        :name => application.css('name').text,
        :application_id => application.css('application_id').text,
        :plan_type => application.css('plan name').text})
      end
      results
    end

  end
end<commit_msg>Add a few new methods to the gem. get_service_plans, get_services, create_account.<commit_after>require 'faraday'
require 'securerandom'
require 'nokogiri'

module Threescale
  class API
    attr_accessor :provider_key, :url, :path, :conn
    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

    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

    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

    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

    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

    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').text,
        :name => application.css('name').text,
        :application_id => application.css('application_id').text,
        :plan_type => application.css('plan name').text})
      end
      results
    end

    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

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

    def create_account(name, service_id)
      response = @conn.post "/admin/api/services/#{service_id}/application_plans.xml", {:provider_key => @provider_key,
        :name => name}
      response.status == 201
    end
  end
end
{'fraction_non_alphanumeric': 0.11733265461949606, 'fraction_numerical': 0.005217612624077373, 'mean_word_length': 2.218263718263718, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 34}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5602137', 'n_tokens_mistral': 2337, 'n_tokens_neox': 2260, 'n_words': 475}