<commit_before>require "voucherify/version"
require "net/http"
require "uri"
require "rest-client"
require "json"

class Voucherify
  def initialize(options)
    @backend_url = "https://api.voucherify.io/v1"
    @options = options
    @headers = {
      "X-App-Id" => @options["applicationId"],
      "X-App-Token" => @options["clientSecretKey"],
      "X-Voucherify-Channel" => "Ruby-SDK",
      :accept => :json
    }
  end

  def get(code)
    url = @backend_url + "/vouchers/" + URI.encode(code)
    response = RestClient.get(url, @headers)
    JSON.parse(response.body)
  end

  # List vouchers. Query parameters:
  # - code_query
  # - limit (default 10)
  # - skip (default 0)
  # - category
  # - campaign
  # - customer
  # Sample query: { limit: 100, skip: 200, category: "Loyalty" }
  def list(query)
    url = @backend_url + "/vouchers/"
    response = RestClient.get(url, @headers.merge({ :params => query }))
    JSON.parse(response.body)
  end

  def redemption(code)
    url = @backend_url + "/vouchers/" + URI.encode(code) + "/redemption"
    response = RestClient.get(url, @headers)
    JSON.parse(response.body)
  end

  # List redemptions. Sample query (1000 successful redemptions from April 2016):
  # {
  #   limit: 1000,
  #   page: 0,
  #   start_date: "2016-04-01T00:00:00",
  #   end_date: "2016-04-30T23:59:59",
  #   result: "Success"
  # }
  def redemptions(query)
    url = @backend_url + "/redemptions/"
    response = RestClient.get(url, @headers.merge({ :params => query }))
    JSON.parse(response.body)
  end

  def redeem(code, tracking_id = nil)
    payload = {}

    if code.is_a? Hash
      payload = code
      code = payload["voucher"] || payload[:voucher]
      payload.delete "voucher"
      payload.delete :voucher
    end

    url = @backend_url + "/vouchers/" + URI.encode(code) + "/redemption"
    url += ("?tracking_id=" + URI.encode(tracking_id)) if tracking_id

    response = RestClient.post(url, payload.to_json, @headers.merge({ :content_type => :json }))
    JSON.parse(response.body)
  end

  def publish(campaign_name)
    url = @backend_url + "/vouchers/publish"
    payload = {}
    
    if campaign_name.is_a? String
      url += "?campaign=" + URI.encode(campaign_name)
    elsif campaign_name.is_a? Hash
      payload = campaign_name
    end
        
    response = RestClient.post(url, payload.to_json, @headers.merge({ :content_type => :json }))
    JSON.parse(response.body)
  end

  # `code` is optional - will be generated if absent.
  # Sample `options` object:
  # {
  #   category: "New Customers",
  #   discount: {
  #     percent_off: 10.0,
  #     type: "PERCENT"
  #   },
  #   start_date: "2016-01-01T00:00:00Z",
  #   expiration_date: "2016-12-31T23:59:59Z",
  #   redemption: {
  #     quantity: 100 
  #   }
  # }
  def create(code, options = {})
    url = @backend_url + "/vouchers/"
    url += URI.encode(code) if code
    response = RestClient.post(url, options.to_json, @headers.merge({ :content_type => :json }))
    JSON.parse(response.body)
  end

  def enable(code)
    url = @backend_url + "/vouchers/" + URI.encode(code) + "/enable"
    response = RestClient.post(url, nil, @headers.merge({ :content_type => :json }))
    nil
  end

  def disable(code)
    url = @backend_url + "/vouchers/" + URI.encode(code) + "/disable"
    response = RestClient.post(url, nil, @headers.merge({ :content_type => :json }))
    nil
  end
  
  def rollback(redemption_id, tracking_id=nil, reason=nil)
    url = @backend_url + "/redemptions/" + URI.encode(redemption_id) + "/rollback"
    if tracking_id || reason
        url += "?" + URI.encode_www_form(:tracking_id => tracking_id, :reason => reason)
    end
    response = RestClient.post(url, nil, @headers.merge({ :content_type => :json }))
    JSON.parse(response.body)
  end
end<commit_msg>Added customer SDK methods<commit_after>require "voucherify/version"
require "net/http"
require "uri"
require "rest-client"
require "json"

class Voucherify
  def initialize(options)
    @backend_url = "https://api.voucherify.io/v1"
    @options = options
    @headers = {
      "X-App-Id" => @options["applicationId"],
      "X-App-Token" => @options["clientSecretKey"],
      "X-Voucherify-Channel" => "Ruby-SDK",
      :accept => :json
    }
  end

  def get(code)
    url = @backend_url + "/vouchers/" + URI.encode(code)
    response = RestClient.get(url, @headers)
    JSON.parse(response.body)
  end

  # List vouchers. Query parameters:
  # - code_query
  # - limit (default 10)
  # - skip (default 0)
  # - category
  # - campaign
  # - customer
  # Sample query: { limit: 100, skip: 200, category: "Loyalty" }
  def list(query)
    url = @backend_url + "/vouchers/"
    response = RestClient.get(url, @headers.merge({ :params => query }))
    JSON.parse(response.body)
  end

  def redemption(code)
    url = @backend_url + "/vouchers/" + URI.encode(code) + "/redemption"
    response = RestClient.get(url, @headers)
    JSON.parse(response.body)
  end

  # List redemptions. Sample query (1000 successful redemptions from April 2016):
  # {
  #   limit: 1000,
  #   page: 0,
  #   start_date: "2016-04-01T00:00:00",
  #   end_date: "2016-04-30T23:59:59",
  #   result: "Success"
  # }
  def redemptions(query)
    url = @backend_url + "/redemptions/"
    response = RestClient.get(url, @headers.merge({ :params => query }))
    JSON.parse(response.body)
  end

  def redeem(code, tracking_id = nil)
    payload = {}

    if code.is_a? Hash
      payload = code
      code = payload["voucher"] || payload[:voucher]
      payload.delete "voucher"
      payload.delete :voucher
    end

    url = @backend_url + "/vouchers/" + URI.encode(code) + "/redemption"
    url += ("?tracking_id=" + URI.encode(tracking_id)) if tracking_id

    response = RestClient.post(url, payload.to_json, @headers.merge({ :content_type => :json }))
    JSON.parse(response.body)
  end

  def publish(campaign_name)
    url = @backend_url + "/vouchers/publish"
    payload = {}
    
    if campaign_name.is_a? String
      url += "?campaign=" + URI.encode(campaign_name)
    elsif campaign_name.is_a? Hash
      payload = campaign_name
    end
        
    response = RestClient.post(url, payload.to_json, @headers.merge({ :content_type => :json }))
    JSON.parse(response.body)
  end

  # `code` is optional - will be generated if absent.
  # Sample `options` object:
  # {
  #   category: "New Customers",
  #   discount: {
  #     percent_off: 10.0,
  #     type: "PERCENT"
  #   },
  #   start_date: "2016-01-01T00:00:00Z",
  #   expiration_date: "2016-12-31T23:59:59Z",
  #   redemption: {
  #     quantity: 100 
  #   }
  # }
  def create(code, options = {})
    url = @backend_url + "/vouchers/"
    url += URI.encode(code) if code
    response = RestClient.post(url, options.to_json, @headers.merge({ :content_type => :json }))
    JSON.parse(response.body)
  end

  def enable(code)
    url = @backend_url + "/vouchers/" + URI.encode(code) + "/enable"
    response = RestClient.post(url, nil, @headers.merge({ :content_type => :json }))
    nil
  end

  def disable(code)
    url = @backend_url + "/vouchers/" + URI.encode(code) + "/disable"
    response = RestClient.post(url, nil, @headers.merge({ :content_type => :json }))
    nil
  end
  
  def rollback(redemption_id, tracking_id=nil, reason=nil)
    url = @backend_url + "/redemptions/" + URI.encode(redemption_id) + "/rollback"
    if tracking_id || reason
        url += "?" + URI.encode_www_form(:tracking_id => tracking_id, :reason => reason)
    end
    response = RestClient.post(url, nil, @headers.merge({ :content_type => :json }))
    JSON.parse(response.body)
  end

  def create_customer(customer)
    url = @backend_url + "/customers/"
    response = RestClient.post(url, customer.to_json, @headers.merge({ :content_type => :json }))
    JSON.parse(response.body)
  end

  def fetch_customer(customer_id)
    url = @backend_url + "/customers/" + customer_id
    response = RestClient.get(url, @headers)
    JSON.parse(response.body)
  end

  def update_customer(customer)
    url = @backend_url + "/customers/" + customer.id
    response = RestClient.put(url, customer.to_json, @headers.merge({ :content_type => :json }))
    JSON.parse(response.body)
  end

  def delete_customer(customer_id)
    url = @backend_url + "/customers/" + customer_id
    response = RestClient.delete(url, @headers)
    nil
  end
end
{'fraction_non_alphanumeric': 0.15106028115320466, 'fraction_numerical': 0.020252561353347628, 'mean_word_length': 3.0931253047294005, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '64057', 'n_tokens_mistral': 3088, 'n_tokens_neox': 2906, 'n_words': 611}