<commit_before>require 'pusher-signature'

module Pusher
  class Client
    attr_accessor :scheme, :host, :port, :app_id, :key, :secret
    attr_reader :http_proxy, :proxy
    attr_writer :connect_timeout, :send_timeout, :receive_timeout,
                :keep_alive_timeout

    ## CONFIGURATION ##

    # Loads the configuration from an url in the environment
    def self.from_env(key = 'PUSHER_URL')
      url = ENV[key] || raise(ConfigurationError, key)
      client = new
      client.url = url
      client
    end

    def initialize(options = {})
      default_options = {
        :scheme => 'http',
        :port => 80,
      }
      merged_options = default_options.merge(options)

      if options.has_key?(:host)
        merged_options[:host] = options[:host]
      elsif options.has_key?(:cluster)
        merged_options[:host] = "api-#{options[:cluster]}.pusher.com"
      else
        merged_options[:host] = "api.pusherapp.com"
      end

      @scheme, @host, @port, @app_id, @key, @secret = merged_options.values_at(
        :scheme, :host, :port, :app_id, :key, :secret
      )

      @http_proxy = nil
      self.http_proxy = options[:http_proxy] if options[:http_proxy]

      # Default timeouts
      @connect_timeout = 5
      @send_timeout = 5
      @receive_timeout = 5
      @keep_alive_timeout = 30
    end

    # @private Returns the authentication token for the client
    def authentication_token
      raise ConfigurationError, :key unless @key
      raise ConfigurationError, :secret unless @secret
      Pusher::Signature::Token.new(@key, @secret)
    end

    # @private Builds a url for this app, optionally appending a path
    def url(path = nil)
      raise ConfigurationError, :app_id unless @app_id
      URI::Generic.build({
        :scheme => @scheme,
        :host => @host,
        :port => @port,
        :path => "/apps/#{@app_id}#{path}"
      })
    end

    # Configure Pusher connection by providing a url rather than specifying
    # scheme, key, secret, and app_id separately.
    #
    # @example
    #   Pusher.url = http://KEY:SECRET@api.pusherapp.com/apps/APP_ID
    #
    def url=(url)
      uri = URI.parse(url)
      @scheme = uri.scheme
      @app_id = uri.path.split('/').last
      @key    = uri.user
      @secret = uri.password
      @host   = uri.host
      @port   = uri.port
    end

    def http_proxy=(http_proxy)
      @http_proxy = http_proxy
      uri = URI.parse(http_proxy)
      @proxy = {
        :scheme => uri.scheme,
        :host => uri.host,
        :port => uri.port,
        :user => uri.user,
        :password => <PASSWORD>
      }
      @http_proxy
    end

    # Configure whether Pusher API calls should be made over SSL
    # (default false)
    #
    # @example
    #   Pusher.encrypted = true
    #
    def encrypted=(boolean)
      @scheme = boolean ? 'https' : 'http'
      # Configure port if it hasn't already been configured
      @port = boolean ? 443 : 80
    end

    def encrypted?
      @scheme == 'https'
    end

    def cluster=(cluster)
      @host = "api-#{cluster}.pusher.com"
    end

    # Convenience method to set all timeouts to the same value (in seconds).
    # For more control, use the individual writers.
    def timeout=(value)
      @connect_timeout, @send_timeout, @receive_timeout = value, value, value
    end

    ## INTERACE WITH THE API ##

    def resource(path)
      Resource.new(self, path)
    end

    # GET arbitrary REST API resource using a synchronous http client.
    # All request signing is handled automatically.
    #
    # @example
    #   begin
    #     Pusher.get('/channels', filter_by_prefix: 'private-')
    #   rescue Pusher::Error => e
    #     # Handle error
    #   end
    #
    # @param path [String] Path excluding /apps/APP_ID
    # @param params [Hash] API params (see http://pusher.com/docs/rest_api)
    #
    # @return [Hash] See Pusher API docs
    #
    # @raise [Pusher::Error] Unsuccessful response - see the error message
    # @raise [Pusher::HTTPError] Error raised inside http client. The original error is wrapped in error.original_error
    #
    def get(path, params = {})
      Resource.new(self, path).get(params)
    end

    # GET arbitrary REST API resource using an asynchronous http client.
    # All request signing is handled automatically.
    #
    # When the eventmachine reactor is running, the em-http-request gem is used;
    # otherwise an async request is made using httpclient. See README for
    # details and examples.
    #
    # @param path [String] Path excluding /apps/APP_ID
    # @param params [Hash] API params (see http://pusher.com/docs/rest_api)
    #
    # @return Either an EM::DefaultDeferrable or a HTTPClient::Connection
    #
    def get_async(path, params = {})
      Resource.new(self, path).get_async(params)
    end

    # POST arbitrary REST API resource using a synchronous http client.
    # Works identially to get method, but posts params as JSON in post body.
    def post(path, params = {})
      Resource.new(self, path).post(params)
    end

    # POST arbitrary REST API resource using an asynchronous http client.
    # Works identially to get_async method, but posts params as JSON in post
    # body.
    def post_async(path, params = {})
      Resource.new(self, path).post_async(params)
    end

    ## HELPER METHODS ##

    # Convenience method for creating a new WebHook instance for validating
    # and extracting info from a received WebHook<commit_msg>Remove Resource duplication<commit_after>require 'pusher-signature'

module Pusher
  class Client
    attr_accessor :scheme, :host, :port, :app_id, :key, :secret
    attr_reader :http_proxy, :proxy
    attr_writer :connect_timeout, :send_timeout, :receive_timeout,
                :keep_alive_timeout

    ## CONFIGURATION ##

    # Loads the configuration from an url in the environment
    def self.from_env(key = 'PUSHER_URL')
      url = ENV[key] || raise(ConfigurationError, key)
      client = new
      client.url = url
      client
    end

    def initialize(options = {})
      default_options = {
        :scheme => 'http',
        :port => 80,
      }
      merged_options = default_options.merge(options)

      if options.has_key?(:host)
        merged_options[:host] = options[:host]
      elsif options.has_key?(:cluster)
        merged_options[:host] = "api-#{options[:cluster]}.pusher.com"
      else
        merged_options[:host] = "api.pusherapp.com"
      end

      @scheme, @host, @port, @app_id, @key, @secret = merged_options.values_at(
        :scheme, :host, :port, :app_id, :key, :secret
      )

      @http_proxy = nil
      self.http_proxy = options[:http_proxy] if options[:http_proxy]

      # Default timeouts
      @connect_timeout = 5
      @send_timeout = 5
      @receive_timeout = 5
      @keep_alive_timeout = 30
    end

    # @private Returns the authentication token for the client
    def authentication_token
      raise ConfigurationError, :key unless @key
      raise ConfigurationError, :secret unless @secret
      Pusher::Signature::Token.new(@key, @secret)
    end

    # @private Builds a url for this app, optionally appending a path
    def url(path = nil)
      raise ConfigurationError, :app_id unless @app_id
      URI::Generic.build({
        :scheme => @scheme,
        :host => @host,
        :port => @port,
        :path => "/apps/#{@app_id}#{path}"
      })
    end

    # Configure Pusher connection by providing a url rather than specifying
    # scheme, key, secret, and app_id separately.
    #
    # @example
    #   Pusher.url = http://KEY:SECRET@api.pusherapp.com/apps/APP_ID
    #
    def url=(url)
      uri = URI.parse(url)
      @scheme = uri.scheme
      @app_id = uri.path.split('/').last
      @key    = uri.user
      @secret = <PASSWORD>.password
      @host   = uri.host
      @port   = uri.port
    end

    def http_proxy=(http_proxy)
      @http_proxy = http_proxy
      uri = URI.parse(http_proxy)
      @proxy = {
        :scheme => uri.scheme,
        :host => uri.host,
        :port => uri.port,
        :user => uri.user,
        :password => <PASSWORD>
      }
      @http_proxy
    end

    # Configure whether Pusher API calls should be made over SSL
    # (default false)
    #
    # @example
    #   Pusher.encrypted = true
    #
    def encrypted=(boolean)
      @scheme = boolean ? 'https' : 'http'
      # Configure port if it hasn't already been configured
      @port = boolean ? 443 : 80
    end

    def encrypted?
      @scheme == 'https'
    end

    def cluster=(cluster)
      @host = "api-#{cluster}.pusher.com"
    end

    # Convenience method to set all timeouts to the same value (in seconds).
    # For more control, use the individual writers.
    def timeout=(value)
      @connect_timeout, @send_timeout, @receive_timeout = value, value, value
    end

    ## INTERACE WITH THE API ##

    def resource(path)
      Resource.new(self, path)
    end

    # GET arbitrary REST API resource using a synchronous http client.
    # All request signing is handled automatically.
    #
    # @example
    #   begin
    #     Pusher.get('/channels', filter_by_prefix: 'private-')
    #   rescue Pusher::Error => e
    #     # Handle error
    #   end
    #
    # @param path [String] Path excluding /apps/APP_ID
    # @param params [Hash] API params (see http://pusher.com/docs/rest_api)
    #
    # @return [Hash] See Pusher API docs
    #
    # @raise [Pusher::Error] Unsuccessful response - see the error message
    # @raise [Pusher::HTTPError] Error raised inside http client. The original error is wrapped in error.original_error
    #
    def get(path, params = {})
      resource(path).get(params)
    end

    # GET arbitrary REST API resource using an asynchronous http client.
    # All request signing is handled automatically.
    #
    # When the eventmachine reactor is running, the em-http-request gem is used;
    # otherwise an async request is made using httpclient. See README for
    # details and examples.
    #
    # @param path [String] Path excluding /apps/APP_ID
    # @param params [Hash] API params (see http://pusher.com/docs/rest_api)
    #
    # @return Either an EM::DefaultDeferrable or a HTTPClient::Connection
    #
    def get_async(path, params = {})
      resource(path).get_async(params)
    end

    # POST arbitrary REST API resource using a synchronous http client.
    # Works identially to get method, but posts params as JSON in post body.
    def post(path, params = {})
      resource(path).post(params)
    end

    # POST arbitrary REST API resource using an asynchronous http client.
    # Works identially to get_async method, but posts params as JSON in post
    # body.
    def post_async(path, params = {})
      resource(path).post_async(params)
    end

    ## HELPER METHODS ##

    # Convenience method for creating a new WebHook instance for validating
    # and extracting info from a received WebHook
{'fraction_non_alphanumeric': 0.10280288505432301, 'fraction_numerical': 0.0021911804984935633, 'mean_word_length': 2.5187921619017026, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '727651', 'n_tokens_mistral': 3346, 'n_tokens_neox': 3274, 'n_words': 999}