<commit_before>require 'cgi'
require 'curb'
require 'sk_sdk'

module SK::SDK
  # Authenticate your SalesKing App using oAuth2. This class provides helpers
  # to create the token & dialog url and to get an access token
  class Oauth

    attr_reader :id, :secret, :redirect_url
    attr_accessor :sub_domain

    # Setup a new oAuth connection requires you to set some default:
    #
    # @param[Hash{String=>String}] opts containing id, secrete, scope, url of
    #   your app
    # @option [String] id oAuth app id from SalesKing app registration
    # @option [String] secret oAuth app secret from SalesKing app registration
    # @option [String] scope permission your app requests
    # @option [String] redirect_url inside your app for auth dialog
    # @option [String] sk_url SalesKing base url, * is replaced with users subdomain,
    #   no trailing slash, optional defaults to https://*.salesking.eu
    # @option [String] sub_domain optional, will probably be set later after a users
    # provided his subdomain
    def initialize(opts)
      @id           = opts['id']
      @secret       = opts['secret']
      @scope        = opts['scope']
      @redirect_url = opts['redirect_url']
      @canvas_slug  = opts['canvas_slug']
      @sk_url       = opts['sk_url'] || "https://*.salesking.eu"
      @sub_domain   = opts['sub_domain']
    end

    # @return [String] URL with parameter to show the auth dialog to the user
    def auth_dialog
      params = { :client_id   => @id,
                 :redirect_uri=> @redirect_url,
                 :scope       => @scope }
      "#{sk_url}/oauth/authorize?#{to_url_params(params)}"
    end

    # @return [String] app's canvas url inside SalesKing
    def sk_canvas_url
      "#{sk_url}/app/#{@canvas_slug}"
    end

    # URL to get the access_token, used in the second step after you have
    # requested the authorization and gotten a code
    # The token url is located at /oauth/token
    #
    # @param [String] code received after auth
    # @return [String] Url with parameter
    def token_url(code)
      params = { :client_id     => @id,
                 :client_secret => @secret,
                 :redirect_uri  => @redirect_url,
                 :code          => code }
      "#{sk_url}/oauth/token?#{to_url_params(params)}"
    end

    # Makes a GET request to the access_token endpoint in SK and receives the
    # access token
    # @param [String] code request token
    # @return [Hash{String=>String}] access token
    def get_token(code)
      c = Curl::Easy.new( token_url( code ) )
      c.ssl_verify_host = false
      c.perform
      # grab token from response body
      ActiveSupport::JSON.decode(c.body_str)
    end

    # @return [String] base api url my-sub.salesking.eu/api
    def sk_api_url
      "#{sk_url}/api"
    end

    # Each company has it's own subdomain so the url must be dynamic.
    # This is achieved by replacing the * with the subdomain in the instance<commit_msg>allow self signed cert on dev.salesking.eu<commit_after>require 'cgi'
require 'curb'
require 'sk_sdk'

module SK::SDK
  # Authenticate your SalesKing App using oAuth2. This class provides helpers
  # to create the token & dialog url and to get an access token
  class Oauth

    attr_reader :id, :secret, :redirect_url
    attr_accessor :sub_domain

    # Setup a new oAuth connection requires you to set some default:
    #
    # @param[Hash{String=>String}] opts containing id, secrete, scope, url of
    #   your app
    # @option [String] id oAuth app id from SalesKing app registration
    # @option [String] secret oAuth app secret from SalesKing app registration
    # @option [String] scope permission your app requests
    # @option [String] redirect_url inside your app for auth dialog
    # @option [String] sk_url SalesKing base url, * is replaced with users subdomain,
    #   no trailing slash, optional defaults to https://*.salesking.eu
    # @option [String] sub_domain optional, will probably be set later after a users
    # provided his subdomain
    def initialize(opts)
      @id           = opts['id']
      @secret       = opts['secret']
      @scope        = opts['scope']
      @redirect_url = opts['redirect_url']
      @canvas_slug  = opts['canvas_slug']
      @sk_url       = opts['sk_url'] || "https://*.salesking.eu"
      @sub_domain   = opts['sub_domain']
    end

    # @return [String] URL with parameter to show the auth dialog to the user
    def auth_dialog
      params = { :client_id   => @id,
                 :redirect_uri=> @redirect_url,
                 :scope       => @scope }
      "#{sk_url}/oauth/authorize?#{to_url_params(params)}"
    end

    # @return [String] app's canvas url inside SalesKing
    def sk_canvas_url
      "#{sk_url}/app/#{@canvas_slug}"
    end

    # URL to get the access_token, used in the second step after you have
    # requested the authorization and gotten a code
    # The token url is located at /oauth/token
    #
    # @param [String] code received after auth
    # @return [String] Url with parameter
    def token_url(code)
      params = { :client_id     => @id,
                 :client_secret => @secret,
                 :redirect_uri  => @redirect_url,
                 :code          => code }
      "#{sk_url}/oauth/token?#{to_url_params(params)}"
    end

    # Makes a GET request to the access_token endpoint in SK and receives the
    # access token
    # @param [String] code request token
    # @return [Hash{String=>String}] access token
    def get_token(code)
      c = Curl::Easy.new( token_url( code ) )
      if sk_url[/dev\.salesking.eu/] # as long as we are using a self signed cert
        c.ssl_verify_host = false
        c.ssl_verify_peer = false
      end
      c.http_get
      # grab token from response body
      ActiveSupport::JSON.decode(c.body_str)
    end

    # @return [String] base api url my-sub.salesking.eu/api
    def sk_api_url
      "#{sk_url}/api"
    end

    # Each company has it's own subdomain so the url must be dynamic.
    # This is achieved by replacing the * with the subdomain in the instance
{'fraction_non_alphanumeric': 0.0939751146037983, 'fraction_numerical': 0.0003274394237066143, 'mean_word_length': 2.6083874778499703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '190811', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1788, 'n_words': 631}