<commit_before>require 'json'
require 'faraday'
require 'faraday_middleware'

module Profitrolly
  class Client

    @@host = 'http://profitrolly.com/merchant_api/v1'.freeze
    @@methods = {
        activate: 'activate',
        available: 'coupons'
    }.freeze

    def initialize(token:)
      @token = token
    end

    def activate(code:)
      @data = { token: @token, code: code }
      resp = post :activate
      resp.body
    rescue Faraday::ResourceNotFound => error
      { error: error.message, code: 404 }
    rescue Faraday::ClientError => error
      { error: error.message, code: 401 }
    end

    def available?(code)
      resp = get :available, code
      (
        resp.body.has_key?('used_at') && resp.body['used_at'].empty?
      ) && (
        resp.body.has_key?('code') && resp.body['code'] == code
      ) && (
        resp.body.has_key?('owned_at') && Time.parse(resp.body['owned_at']) < Time.now
      )
    end

    private

    def connection
      Faraday.new(url: @@host) do |faraday|
        faraday.request  :json
        faraday.response :json, :content_type => /\bjson$/
        # faraday.response :logger
        # faraday.response :raise_error
        faraday.adapter  Faraday.default_adapter
      end
    end

    def post(method)
      connection.post do |req|
        req.url "#{@@methods[method]}"
        req.body = @data.to_json
      end
    end

    def get(method, parameter)
      connection.get do |req|
        req.url "#{@@methods[method]}/#{parameter}", token: @token
      end
    end<commit_msg>Add check for promo code expiry date<commit_after>require 'json'
require 'faraday'
require 'faraday_middleware'

module Profitrolly
  class Client

    @@host = 'http://profitrolly.com/merchant_api/v1'.freeze
    @@methods = {
        activate: 'activate',
        available: 'coupons'
    }.freeze

    def initialize(token:)
      @token = token
    end

    def activate(code:)
      @data = { token: @token, code: code }
      resp = post :activate
      resp.body
    rescue Faraday::ResourceNotFound => error
      { error: error.message, code: 404 }
    rescue Faraday::ClientError => error
      { error: error.message, code: 401 }
    end

    def available?(code)
      resp = get :available, code
      (
        resp.body.has_key?('used_at') && resp.body['used_at'].empty?
      ) && (
        resp.body.has_key?('code') && resp.body['code'] == code
      ) && (
        resp.body.has_key?('owned_at') && Time.parse(resp.body['owned_at']) < Time.now
      ) && (
      resp.body.has_key?('expired_at') && Time.parse(resp.body['expired_at']) > Time.now
      )
    end

    private

    def connection
      Faraday.new(url: @@host) do |faraday|
        faraday.request  :json
        faraday.response :json, :content_type => /\bjson$/
        # faraday.response :logger
        # faraday.response :raise_error
        faraday.adapter  Faraday.default_adapter
      end
    end

    def post(method)
      connection.post do |req|
        req.url "#{@@methods[method]}"
        req.body = @data.to_json
      end
    end

    def get(method, parameter)
      connection.get do |req|
        req.url "#{@@methods[method]}/#{parameter}", token: @token
      end
    end
{'fraction_non_alphanumeric': 0.13710925410089755, 'fraction_numerical': 0.004333023831631074, 'mean_word_length': 2.331958762886598, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, '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': '4006645', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1023, 'n_words': 224}