Sha256: 55e9d1b2876a5f931b9a85bcbc97cda99f6ae0429a4a31287b0f0469eaa6607f

Contents?: true

Size: 1.95 KB

Versions: 15

Compression:

Stored size: 1.95 KB

Contents

require "cgi"
require "json"

module Tamber
  module Util

    def self.encode_parameters(params)
      Util.flatten_params(params).
        map { |k,v| "#{url_encode(k)}=#{url_encode(v)}" }.join('&')
    end

    def self.url_encode(key)
      CGI.escape(key.to_s).
        gsub('%5B', '[').gsub('%5D', ']')
    end

    def self.flatten_params(params, parent_key=nil)
      result = []

      params.sort_by { |(k, v)| k.to_s }.each do |key, value|
        calculated_key = parent_key ? "#{parent_key}[#{key}]" : "#{key}"
        if value.is_a?(Hash) || value.is_a?(Array) || value.is_a?(APIResource)
          result << [calculated_key, value.to_json]
        else
          result << [calculated_key, value]
        end
      end

      result
    end

    def self.object_classes
      @object_classes ||= {
        # Core
        'event' => Event,
        'discovery' => Discovery,

        # Expanded
        'user' => User,
        'item' => Item,
        'behavior' => Behavior,
      }
    end

    def self.convert_full(resp)
      case resp
      when Hash
        object_classes.fetch(resp[:object],TamberObject).construct_from(resp)
      when Array
        resp.map { |i| convert_full(i) }
      else
        resp
      end
    end

    def self.convert_to_tamber_object(resp)
      case resp
      when Hash
        object_classes.fetch(resp[:object],TamberObject).construct_from(resp)
      when Array
        if resp.size == 1
          resp = resp[0]
          self.convert_full(resp)
        else
          resp.map { |i| convert_full(i) }
        end
      else
        resp
      end
    end

    def self.symbolize_names(object)
      case object
      when Hash
        new_hash = {}
        object.each do |key, value|
          key = (key.to_sym rescue key) || key
          new_hash[key] = symbolize_names(value)
        end
        new_hash
      when Array
        object.map { |value| symbolize_names(value) }
      else
        object
      end
    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
tamber-0.2.2 lib/tamber/util.rb
tamber-0.2.1 lib/tamber/util.rb
tamber-0.2.0 lib/tamber/util.rb
tamber-0.1.14 lib/tamber/util.rb
tamber-0.1.13 lib/tamber/util.rb
tamber-0.1.12 lib/tamber/util.rb
tamber-0.1.11 lib/tamber/util.rb
tamber-0.1.10 lib/tamber/util.rb
tamber-0.1.9 lib/tamber/util.rb
tamber-0.1.8 lib/tamber/util.rb
tamber-0.1.7 lib/tamber/util.rb
tamber-0.1.4 lib/tamber/util.rb
tamber-0.1.3 lib/tamber/util.rb
tamber-0.1.2 lib/tamber/util.rb
tamber-0.1.1 lib/tamber/util.rb