Sha256: 0a315d9362897d223a4b488a456f16360c9b623811d5e3d6a24301cab9a001a4

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

module Cloudhdr
  class Base
    
    def self.api_key
      Cloudhdr.api_key
    end
    
    def self.base_url
      Cloudhdr.base_url
    end
    
    def self.encode(content, format=nil)
      if content.is_a?(String)
        content
      elsif format.to_s == 'xml'
        if content.is_a?(Hash) && content.keys.size == 1
          content[content.keys.first].to_xml(:root => content.keys.first)
        else
          content.to_xml
        end
      else
        content.to_json
      end
    end
    
    def encode(content, format=nil)
      self.class.encode(content, format)
    end
    
    def self.decode(content, format=nil)
      begin
        if content.is_a?(String)
          if format.to_s == 'xml'
            Hash.from_xml(content)
          else
            ActiveSupport::JSON.decode(content)
          end
        else
          content
        end
      rescue Exception => e
        content
      end   
    end

    def decode(content, format=nil)
      self.class.decode(content, format)
    end

    protected

    def self.apply_api_key(params, format=nil)
      if api_key
        decoded_params = decode(params, format).with_indifferent_access

        if decoded_params[:api_request]
          decoded_params[:api_request] = decoded_params[:api_request].with_indifferent_access
        end

        if format.to_s == 'xml'
          if !(decoded_params[:api_request] && decoded_params[:api_request][:api_key])
            decoded_params[:api_request] ||= {}.with_indifferent_access
            decoded_params[:api_request][:api_key] = api_key
          end
        else
          decoded_params['api_key'] = api_key unless decoded_params['api_key']
        end

        decoded_params
      else
        params
      end
    end

    def self.merge_params(options, params)
      if options[:params]
        options[:params] = options[:params].merge(params)
        options
      else
        options.merge(:params => params)
      end
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cloudhdr-0.0.3 lib/cloudhdr/base.rb
cloudhdr-0.0.2 lib/cloudhdr/base.rb
cloudhdr-0.0.1 lib/cloudhdr/base.rb