<commit_before>require 'base64'
require 'nokogiri'

module Spreedly
  class Environment

    include SslRequester
    include Urls

    attr_reader :key, :currency_code, :base_url

    def initialize(environment_key, access_secret, options={})
      @key, @access_secret = environment_key, access_secret
      @base_url = options[:base_url] || "https://core.spreedly.com"
      @currency_code = options[:currency_code] || 'USD'
    end

    def transparent_redirect_form_action
      "#{base_url}/v1/payment_methods"
    end

    def find_payment_method(token)
      xml_doc = ssl_get(find_payment_method_url(token), headers)
      PaymentMethod.new_from(xml_doc)
    end

    def find_transaction(token)
      xml_doc = ssl_get(find_transaction_url(token), headers)
      Transaction.new_from(xml_doc)
    end

    def find_transcript(transaction_token)
      ssl_raw_get(find_transcript_url(transaction_token), headers)
    end

    def find_gateway(token)
      xml_doc = ssl_get(find_gateway_url(token), headers)
      Gateway.new(xml_doc)
    end

    def purchase_on_gateway(gateway_token, payment_method_token, amount, options = {})
      body = auth_purchase_body(amount, payment_method_token, options)
      api_post(purchase_url(gateway_token), body)
    end

    def authorize_on_gateway(gateway_token, payment_method_token, amount, options = {})
      body = auth_purchase_body(amount, payment_method_token, options)
      api_post(authorize_url(gateway_token), body)
    end

    def capture_transaction(authorization_token, options = {})
      api_post(capture_url(authorization_token), capture_body(options))
    end

    def void_transaction(token, options = {})
      api_post(void_transaction_url(token), void_body(options))
    end

    def refund_transaction(token, options = {})
      api_post(refund_transaction_url(token), refund_body(options))
    end

    def retain_payment_method(payment_method_token)
      xml_doc = ssl_put(retain_payment_method_url(payment_method_token), '', headers)
      Transaction.new_from(xml_doc)
    end

    def redact_payment_method(payment_method_token, options = {})
      body = redact_payment_method_body(options)
      xml_doc = ssl_put(redact_payment_method_url(payment_method_token), body, headers)
      Transaction.new_from(xml_doc)
    end

    def redact_gateway(gateway_token, options = {})
      xml_doc = ssl_put(redact_gateway_url(gateway_token), '', headers)
      Transaction.new_from(xml_doc)
    end

    def list_transactions(since_token = nil, payment_method_token = nil)
      xml_doc = ssl_get(list_transactions_url(since_token, payment_method_token), headers)
      Transaction.new_list_from(xml_doc)
    end

    def list_payment_methods(since_token = nil)
      xml_doc = ssl_get(list_payment_methods_url(since_token), headers)
      PaymentMethod.new_list_from(xml_doc)
    end

    def list_gateways(since_token = nil)
      xml_doc = ssl_get(list_gateways_url(since_token), headers)
      Gateway.new_list_from(xml_doc)
    end

    def gateway_options
      xml_doc = ssl_options(gateway_options_url)
      GatewayClass.new_list_from(xml_doc)
    end

    def add_gateway(gateway_type, credentials = {})
      body = add_gateway_body(gateway_type, credentials)
      xml_doc = ssl_post(add_gateway_url, body, headers)
      Gateway.new(xml_doc)
    end

    def add_credit_card(options)
      api_post(add_payment_method_url, add_credit_card_body(options), false)
    end

    def update_credit_card(credit_card_token, options)
      body = update_credit_card_body(options)
      xml_doc = ssl_put(update_payment_method_url(credit_card_token), body, headers)
      PaymentMethod.new_from(xml_doc)
    end

    private
    def headers
      {
        'Authorization' => ('Basic ' + Base64.strict_encode64("#{@key}:#{@access_secret}").chomp),
        'Content-Type' => 'text/xml'
      }
    end<commit_msg>Allow access to gateway options w/o credentials<commit_after>require 'base64'
require 'nokogiri'

module Spreedly
  class Environment

    include SslRequester
    include Urls

    attr_reader :key, :currency_code, :base_url

    def initialize(environment_key, access_secret, options={})
      @key, @access_secret = environment_key, access_secret
      @base_url = options[:base_url] || "https://core.spreedly.com"
      @currency_code = options[:currency_code] || 'USD'
    end

    def transparent_redirect_form_action
      "#{base_url}/v1/payment_methods"
    end

    def find_payment_method(token)
      xml_doc = ssl_get(find_payment_method_url(token), headers)
      PaymentMethod.new_from(xml_doc)
    end

    def find_transaction(token)
      xml_doc = ssl_get(find_transaction_url(token), headers)
      Transaction.new_from(xml_doc)
    end

    def find_transcript(transaction_token)
      ssl_raw_get(find_transcript_url(transaction_token), headers)
    end

    def find_gateway(token)
      xml_doc = ssl_get(find_gateway_url(token), headers)
      Gateway.new(xml_doc)
    end

    def purchase_on_gateway(gateway_token, payment_method_token, amount, options = {})
      body = auth_purchase_body(amount, payment_method_token, options)
      api_post(purchase_url(gateway_token), body)
    end

    def authorize_on_gateway(gateway_token, payment_method_token, amount, options = {})
      body = auth_purchase_body(amount, payment_method_token, options)
      api_post(authorize_url(gateway_token), body)
    end

    def capture_transaction(authorization_token, options = {})
      api_post(capture_url(authorization_token), capture_body(options))
    end

    def void_transaction(token, options = {})
      api_post(void_transaction_url(token), void_body(options))
    end

    def refund_transaction(token, options = {})
      api_post(refund_transaction_url(token), refund_body(options))
    end

    def retain_payment_method(payment_method_token)
      xml_doc = ssl_put(retain_payment_method_url(payment_method_token), '', headers)
      Transaction.new_from(xml_doc)
    end

    def redact_payment_method(payment_method_token, options = {})
      body = redact_payment_method_body(options)
      xml_doc = ssl_put(redact_payment_method_url(payment_method_token), body, headers)
      Transaction.new_from(xml_doc)
    end

    def redact_gateway(gateway_token, options = {})
      xml_doc = ssl_put(redact_gateway_url(gateway_token), '', headers)
      Transaction.new_from(xml_doc)
    end

    def list_transactions(since_token = nil, payment_method_token = nil)
      xml_doc = ssl_get(list_transactions_url(since_token, payment_method_token), headers)
      Transaction.new_list_from(xml_doc)
    end

    def list_payment_methods(since_token = nil)
      xml_doc = ssl_get(list_payment_methods_url(since_token), headers)
      PaymentMethod.new_list_from(xml_doc)
    end

    def list_gateways(since_token = nil)
      xml_doc = ssl_get(list_gateways_url(since_token), headers)
      Gateway.new_list_from(xml_doc)
    end

    def gateway_options
      xml_doc = ssl_options(gateway_options_url)
      GatewayClass.new_list_from(xml_doc)
    end

    def self.gateway_options
      self.new("", "").gateway_options
    end

    def add_gateway(gateway_type, credentials = {})
      body = add_gateway_body(gateway_type, credentials)
      xml_doc = ssl_post(add_gateway_url, body, headers)
      Gateway.new(xml_doc)
    end

    def add_credit_card(options)
      api_post(add_payment_method_url, add_credit_card_body(options), false)
    end

    def update_credit_card(credit_card_token, options)
      body = update_credit_card_body(options)
      xml_doc = ssl_put(update_payment_method_url(credit_card_token), body, headers)
      PaymentMethod.new_from(xml_doc)
    end

    private
    def headers
      {
        'Authorization' => ('Basic ' + Base64.strict_encode64("#{@key}:#{@access_secret}").chomp),
        'Content-Type' => 'text/xml'
      }
    end
{'fraction_non_alphanumeric': 0.08803039898670044, 'fraction_numerical': 0.001773274224192527, 'mean_word_length': 4.104072398190045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 50}, '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': '7457878', 'n_tokens_mistral': 2630, 'n_tokens_neox': 2571, 'n_words': 410}