Sha256: 580f98ac7f753008ca7291b4a5416f53d0296b4f35b88cccc26615cae94eb36d

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

class Pupil
  module Essentials
    # @param [Hash] parameter
    # @return [String] URL Serialized parameters
    def serialize_parameter parameter
      return "" unless parameter.class == Hash
      ant = Hash.new
      parameter.each do |key, value|
        case key.to_sym
        when :include
          if value.class == String || Symbol
            ant[:"include_#{value}"] = "true"
            break
          end
        when :exclude
          if value.class == String || Symbol
            ant[:"exclude_#{value}"] = "true"
            break
          end
        else
          ant[key.to_sym] = value.to_s
        end
      end
      param = ant.inject(""){|k,v|k+"&#{v[0]}=#{URI.escape(v[1])}"}.sub!(/^&/,"?")
      return param ? param : ""
    end

    def guess_parameter(parameter)
      case parameter.class.to_s
      when "Fixnum", "Bignum"
        :user_id
      when "Symbol", "String"
        :screen_name
      else
        false
      end
    end

    def get(url, param={})
      param_s = serialize_parameter(param)
      begin
        response = @access_token.get(url+param_s).body
      rescue => vars
        raise NetworkError, vars
      end
      result = JSON.parse(response)
      return (result["error"].nil? rescue true)? result : false
    end

    def post(url, param={})
      param_s = serialize_parameter(param)
      begin
        response = @access_token.post(url+param_s).body
      rescue => vars
        raise NetworkError, vars
      end
      result = JSON.parse(response)
      return (result["error"].nil? rescue true)? result : false
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pupil-0.2.2 lib/pupil/essentials.rb
pupil-0.2.1 lib/pupil/essentials.rb
pupil-0.2.0 lib/pupil/essentials.rb