Class Hash

  1. lib/stella/core_ext.rb
Parent: Object

Methods

public class

  1. from_array

public instance

  1. dump
  2. flatten
  3. to_http_params
  4. to_json

Public class methods

from_array (array = [])
[show source]
# File lib/stella/core_ext.rb, line 533
  def self.from_array(array = [])
    h = Hash.new
    array.size.times do |t|
      h[t] = array[t]
    end
    h
  end

Public instance methods

dump (format)
[show source]
# File lib/stella/core_ext.rb, line 499
  def dump(format)
    respond_to?(:"to_#{format}") ? send(:"to_#{format}") : raise("Unknown format")
  end
flatten ()

Courtesy of Julien Genestoux

[show source]
# File lib/stella/core_ext.rb, line 472
  def flatten
    params = {}
    stack = []

    each do |k, v|
      if v.is_a?(Hash)
        stack << [k,v]
      elsif v.is_a?(Array)
        stack << [k,Hash.from_array(v)]
      else
        params[k] =  v
      end
    end

    stack.each do |parent, hash|
      hash.each do |k, v|
        if v.is_a?(Hash)
          stack << ["#{parent}[#{k}]", v]
        else
          params["#{parent}[#{k}]"] = v
        end
      end
    end

    params
  end
to_http_params ()

Courtesy of Julien Genestoux See: stackoverflow.com/questions/798710/how-to-turn-a-ruby-hash-into-http-params NOTE: conflicts w/ HTTParty 0.7.3 when named “to_params“

[show source]
# File lib/stella/core_ext.rb, line 506
  def to_http_params
    params = ''
    stack = []

    each do |k, v|
      if v.is_a?(Hash)
        stack << [k,v]
      elsif v.is_a?(Array)
        stack << [k,Hash.from_array(v)]
      else
        params << "#{k}=#{v}&"
      end
    end

    stack.each do |parent, hash|
      hash.each do |k, v|
        if v.is_a?(Hash)
          stack << ["#{parent}[#{k}]", URI::Escape.escape(v)]
        else
          params << "#{parent}[#{k}]=#{URI::Escape.escape(v)}&"
        end
      end
    end

    params.chop! 
    params
  end
to_json (*args)
[show source]
# File lib/stella/core_ext.rb, line 466
    def to_json(*args)
      Yajl::Encoder.encode(self)
    end