<commit_before>require 'uri'
require 'zlib'
require 'stringio'
require 'ebay/request/connection'

module Ebay #:nodoc:
  class EbayError < StandardError #:nodoc:
  end

  class RequestError < EbayError #:nodoc:
    attr_reader :errors
    def initialize(errors)
      @errors = errors
    end
  end

  # == Overview
  # Api is the main proxy class responsible for instantiating and invoking
  # the correct Ebay::Requests object for the method called. This is currently done
  # using method_missing:  
  #   ebay = Ebay::Api.new
  #   response = ebay.get_ebay_official_time
  #   puts response.timestamp # => 2006-08-13T21:28:39.515Z
  #
  # All Ebay API calls have a corresponding request and response object.
  # In the example above the request objects is 
  # Ebay::Requests::GeteBayOfficialTime and the response object is
  # Ebay::Responses::GeteBayOfficialTime
  class Api
    include Ebay::Types
    XmlNs = 'urn:ebay:apis:eBLBaseComponents'
    
    cattr_accessor :use_sandbox, :sandbox_url, :production_url, :site_id
    cattr_accessor :dev_id, :app_id, :cert, :auth_token
    
    self.sandbox_url = 'https://api.sandbox.ebay.com/ws/api.dll'
    self.production_url = 'https://api.ebay.com/ws/api.dll'
    self.use_sandbox = false

    # Make the default site US
    self.site_id = 0
  
    # The URI that all requests are sent to. This depends on the current environment the Api
    # is configured to use and will either be the Api#sandbox_url or the Api#production_url
    def self.service_uri
      URI.parse(using_sandbox? ? sandbox_url : production_url)
    end

    # Are we currently routing requests to the eBay sandbox URL?
    def self.using_sandbox?
      use_sandbox
    end

    # Are we currently routing requests to the eBay production URL?
    def self.using_production?
      !using_sandbox?
    end

    # Simply yields the Ebay::Api class itself.  This makes configuration a bit nicer looking:
    #
    #  Ebay::Api.configure do |ebay|
    #    ebay.auth_token = 'YOUR AUTH TOKEN HERE'
    #    ebay.dev_id = 'YOUR DEVELOPER ID HERE'
    #    ebay.app_id = 'YOUR APPLICATION ID HERE'
    #    ebay.cert = 'YOUR CERTIFICATE HERE'
    #
    #  # The default environment is the production environment
    #  # Override by setting use_sandbox to true
    #    ebay.use_sandbox = true
    #  end
    def self.configure
      yield self if block_given?
    end

    # The schema version the API client is currently using
    def schema_version
      Ebay::Schema::VERSION.to_s
    end

    def service_uri
      self.class.service_uri
    end
    
    def app_id
      self.class.app_id
    end

    def cert
      self.class.cert
    end

    def auth_token
       @auth_token || self.class.auth_token
    end
    
    def site_id
      @site_id || self.class.site_id
    end

    # With no options, the default is to use the default site_id and the default
    # auth_token configured on the Api class.
    #   ebay = Ebay::Api.new
    #
    # However, another user's auth_token can be used and the site can be selected
    # at the time of creation. Ex: Canada(Site 2) with another user's auth token.
    #   ebay = Ebay::Api.new(:site_id => 2, :auth_token => 'TEST')
    def initialize(options = {})
      @format = options[:format] || :object
      @auth_token = options[:auth_token]
      @site_id = options[:site_id]
    end

    private
    def method_missing(method_id, *args, &block)
      args = args.first || {}

      method_args = { :format => @format }.update(args)

      invoke_request(method_id.to_s, method_args, &block)
    end

    def invoke_request(name, args)
      format = args.delete(:format)
      request = build_request(name.ebay_camelize, args)
     
      yield request if block_given? 
      
      raw_response = connection.post( service_uri.path, 
                                      build_body(request), 
                                      build_headers(request.call_name)
                                    )
      
      parse decompress(raw_response), format
    end

    def build_request(name, args)
      request_class = Ebay::Requests.const_get(name)
      request_class.new(args)
    end<commit_msg>Don't break the whole Api<commit_after>require 'uri'
require 'zlib'
require 'stringio'
require 'ebay/request/connection'

module Ebay #:nodoc:
  class EbayError < StandardError #:nodoc:
  end

  class RequestError < EbayError #:nodoc:
    attr_reader :errors
    def initialize(errors)
      @errors = errors
    end
  end

  # == Overview
  # Api is the main proxy class responsible for instantiating and invoking
  # the correct Ebay::Requests object for the method called. This is currently done
  # using method_missing:  
  #   ebay = Ebay::Api.new
  #   response = ebay.get_ebay_official_time
  #   puts response.timestamp # => 2006-08-13T21:28:39.515Z
  #
  # All Ebay API calls have a corresponding request and response object.
  # In the example above the request objects is 
  # Ebay::Requests::GeteBayOfficialTime and the response object is
  # Ebay::Responses::GeteBayOfficialTime
  class Api
    include Ebay::Types
    XmlNs = 'urn:ebay:apis:eBLBaseComponents'
    
    cattr_accessor :use_sandbox, :sandbox_url, :production_url, :site_id
    cattr_accessor :dev_id, :app_id, :cert, :auth_token
    
    self.sandbox_url = 'https://api.sandbox.ebay.com/ws/api.dll'
    self.production_url = 'https://api.ebay.com/ws/api.dll'
    self.use_sandbox = false

    # Make the default site US
    self.site_id = 0
  
    # The URI that all requests are sent to. This depends on the current environment the Api
    # is configured to use and will either be the Api#sandbox_url or the Api#production_url
    def self.service_uri
      URI.parse(using_sandbox? ? sandbox_url : production_url)
    end

    # Are we currently routing requests to the eBay sandbox URL?
    def self.using_sandbox?
      use_sandbox
    end

    # Are we currently routing requests to the eBay production URL?
    def self.using_production?
      !using_sandbox?
    end

    # Simply yields the Ebay::Api class itself.  This makes configuration a bit nicer looking:
    #
    #  Ebay::Api.configure do |ebay|
    #    ebay.auth_token = '<PASSWORD> AUTH TOKEN HERE'
    #    ebay.dev_id = 'YOUR DEVELOPER ID HERE'
    #    ebay.app_id = 'YOUR APPLICATION ID HERE'
    #    ebay.cert = 'YOUR CERTIFICATE HERE'
    #
    #  # The default environment is the production environment
    #  # Override by setting use_sandbox to true
    #    ebay.use_sandbox = true
    #  end
    def self.configure
      yield self if block_given?
    end

    # The schema version the API client is currently using
    def schema_version
      Ebay::Schema::VERSION.to_s
    end

    def service_uri
      self.class.service_uri
    end
    
    def app_id
      self.class.app_id
    end

    def cert
      self.class.cert
    end

    def auth_token
       @auth_token || self.class.auth_token
    end
    
    def site_id
      @site_id || self.class.site_id
    end

    # With no options, the default is to use the default site_id and the default
    # auth_token configured on the Api class.
    #   ebay = Ebay::Api.new
    #
    # However, another user's auth_token can be used and the site can be selected
    # at the time of creation. Ex: Canada(Site 2) with another user's auth token.
    #   ebay = Ebay::Api.new(:site_id => 2, :auth_token => 'TEST')
    def initialize(options = {})
      @format = options[:format] || :object
      @auth_token = options[:auth_token]
      @site_id = options[:site_id]
    end

    private
    def method_missing(method_id, *args, &block)
      args = args.first || {}

      method_args = { :format => @format }.update(args)
      method_args[:auth_token] = auth_token

      invoke_request(method_id.to_s, method_args, &block)
    end

    def invoke_request(name, args)
      format = args.delete(:format)
      request = build_request(name.ebay_camelize, args)
     
      yield request if block_given? 
      
      raw_response = connection.post( service_uri.path, 
                                      build_body(request), 
                                      build_headers(request.call_name)
                                    )
      
      parse decompress(raw_response), format
    end

    def build_request(name, args)
      request_class = Ebay::Requests.const_get(name)
      request_class.new(args)
    end
{'fraction_non_alphanumeric': 0.08115494296577946, 'fraction_numerical': 0.004752851711026616, 'mean_word_length': 2.56350550381033, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2172784', 'n_tokens_mistral': 2777, 'n_tokens_neox': 2609, 'n_words': 761}