Sha256: 26b97feef79040af952df8c275bead80d8a68f0d981bfe03a5d8c9219c7c9b61

Contents?: true

Size: 1.63 KB

Versions: 11

Compression:

Stored size: 1.63 KB

Contents

module PagarMe
  class Util
    def self.pagarme_classes
      return {
      'transaction' => Transaction,
      'plan' => Plan,
      'customer' => Customer,
      'subscription' => Subscription,
      'address' => Address,
      'phone' => Phone,
      }
    end

    def self.convert_to_pagarme_object(response)
      case response
      when Array
      response.map{ |i| convert_to_pagarme_object(i)}
      when Hash
      self.pagarme_classes.fetch(response['object'], PagarMeObject).build(response)
      else
      response
      end
    end

    def self.url_encode(key)
      URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
    end


    def self.normalize_params(parameters, parent = nil)
      ret = []
      parameters.each do |k, v|
        current_key = parent ? "#{parent}[#{url_encode(k)}]" : url_encode(k)
        case v
        when Hash
          ret += normalize_params(v, current_key)
        when Array
          ret += normalize_array_params(v, current_key)
        else
          ret << [current_key, v]
        end
      end
      ret
    end

    def self.normalize_array_params(value, current_key)
      ret = []
      value.each do |element|
        case element
        when Hash
          ret += normalize_params(element, current_key)
        when Array
          ret += normalize_array_params(element, current_key)
        else
          ret << ["#{current_key}[]", element]
        end
      end
      ret
    end
  end
end

class Hash
  def to_params
    self.map{ |key, value| "#{key}=#{value}" }.join('&')
  end
end

class Array
  def to_params
    self.map{ |key, value| "#{key}=#{value}" }.join('&')
  end
end


Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pagarme-1.9.2 lib/pagarme/util.rb
pagarme-1.9.1 lib/pagarme/util.rb
pagarme-1.9 lib/pagarme/util.rb
pagarme-1.8 lib/pagarme/util.rb
pagarme-1.7 lib/pagarme/util.rb
pagarme-1.5 lib/pagarme/util.rb
pagarme-1.4 lib/pagarme/util.rb
pagarme-1.3 lib/pagarme/util.rb
pagarme-1.2 lib/pagarme/util.rb
pagarme-1.1 lib/pagarme/util.rb
pagarme-1.0 lib/pagarme/util.rb