<commit_before>require 'httparty'
require 'hashie/mash'

require 'doyoubuzz/showcase/error'

module Doyoubuzz
  class Showcase

    include HTTParty
    base_uri 'http://showcase.doyoubuzz.com/api/v1'

    # Construction with mandatory api key and api secret
    def initialize(api_key, api_secret)
      @api_key = api_key
      @api_secret = api_secret
    end

    # HTTP calls => forwarded to #call_api with the verb as the first argument
    [:get, :post, :put, :delete].each do |verb|
      define_method(verb) do |method, params = {}|
        call_api(verb, method, params)
      end
    end

    # SSO redirection
    def sso_redirect_url(application_name, timestamp, sso_key, user_attributes)
      # Check mandatory attributes are given
      missing_attributes = [:email, :external_id, :firstname, :lastname].reject{|key| user_attributes[key]}

      if missing_attributes.length > 0
        raise ArgumentError, "Missing mandatory attributes for SSO : #{missing_attributes.join(', ')}"
      end

      params = sign_sso_params(user_attributes.merge(timestamp: timestamp), sso_key)

      "http://showcase.doyoubuzz.com/p/fr/#{application_name}/sso?#{URI.encode_www_form(params)}"
    end


    private

    # The actual api call
    def call_api(verb, method, params)
      res = self.class.send(verb, method, build_request_parameters(params, verb))
      return process_response(res)
    end

    # Process the HTTParty response, checking for errors
    def process_response(res)
      if !res.success?
        raise Error.new(res.code, res.body)
      end

      if res.is_a? Hash
        return Hashie::Mash.new(res)
      elsif res.is_a? Array
        return res.map{|item| Hashie::Mash.new(item)}
      else
        return res
      end
    end

    # Build the request parameters
    def build_request_parameters(params, verb)
      additional_parameters = {:apikey => @api_key, :timestamp => Time.now.to_i}

      # GET, DELETE requests : the parameters are in the request query
      if [:get, :delete].include? verb<commit_msg>Fix code to make spec pass again<commit_after>require 'logger'
require 'httparty'
require 'hashie/mash'

require 'doyoubuzz/showcase/error'

module Doyoubuzz
  class Showcase

    Hashie.logger = ::Logger.new(STDOUT)

    include HTTParty
    base_uri 'http://showcase.doyoubuzz.com/api/v1'

    # Construction with mandatory api key and api secret
    def initialize(api_key, api_secret)
      @api_key = api_key
      @api_secret = api_secret
    end

    # HTTP calls => forwarded to #call_api with the verb as the first argument
    [:get, :post, :put, :delete].each do |verb|
      define_method(verb) do |method, params = {}|
        call_api(verb, method, params)
      end
    end

    # SSO redirection
    def sso_redirect_url(application_name, timestamp, sso_key, user_attributes)
      # Check mandatory attributes are given
      missing_attributes = [:email, :external_id, :firstname, :lastname].reject{|key| user_attributes[key]}

      if missing_attributes.length > 0
        raise ArgumentError, "Missing mandatory attributes for SSO : #{missing_attributes.join(', ')}"
      end

      params = sign_sso_params(user_attributes.merge(timestamp: timestamp), sso_key)

      "http://showcase.doyoubuzz.com/p/fr/#{application_name}/sso?#{URI.encode_www_form(params)}"
    end


    private

    # The actual api call
    def call_api(verb, method, params)
      res = self.class.send(verb, method, build_request_parameters(params, verb))
      return process_response(res)
    end

    # Process the HTTParty response, checking for errors
    def process_response(res)
      if !res.success?
        raise Error.new(res.code, res.body)
      end

      object = res.parsed_response

      case object
      when Hash then mash(object)
      when Array then object.map(&method(:mash))
      else object
      end
    end

    def mash(object)
      Hashie::Mash.new(object)
    end

    # Build the request parameters
    def build_request_parameters(params, verb)
      additional_parameters = {:apikey => @api_key, :timestamp => Time.now.to_i}

      # GET, DELETE requests : the parameters are in the request query
      if [:get, :delete].include? verb
{'fraction_non_alphanumeric': 0.09717942640436122, 'fraction_numerical': 0.0009480919649205973, 'mean_word_length': 3.1291585127201564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '740777', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1285, 'n_words': 347}