Sha256: 64cde91d32d7e9f230d1ea75a3f499b17f1a5d1e58eff79ce22b832554dfb2bf

Contents?: true

Size: 1.04 KB

Versions: 6

Compression:

Stored size: 1.04 KB

Contents

require 'cgi'
module Exegesis
  module Http
    extend self
    
    def format_url url, params={}
      if params && !params.empty?
        query = params.map do |key, value|
          value = value.to_json if [:key, :startkey, :endkey, :keys].include?(key)
          "#{key}=#{CGI.escape(value.to_s)}"
        end.join('&')
        url = "#{url}?#{query}"
      end
      url
    end
    
    def escape_id id
      if %r{^_design/(.*)/_view/(.*)} =~ id
        "_design/#{CGI.escape($1)}/_view/#{CGI.escape($2)}"
      elsif /^_design\/(.*)/ =~ id
        "_design/#{CGI.escape($1)}"
      else 
        CGI.escape(id)
      end
    end
    
    def get url, headers={}
      JSON.parse(RestClient.get(url, headers), :max_nesting => false)
    end
    
    def post url, body='', headers={}
      JSON.parse(RestClient.post(url, body, headers))
    end
    
    def put url, body='', headers={}
      JSON.parse(RestClient.put(url, body, headers))
    end
    
    def delete url, headers={}
      JSON.parse(RestClient.delete(url, headers))
    end
    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mattly-exegesis-0.2.1 lib/exegesis/utils/http.rb
mattly-exegesis-0.2.2 lib/exegesis/utils/http.rb
mattly-exegesis-0.2.3 lib/exegesis/utils/http.rb
mattly-exegesis-0.2.5 lib/exegesis/utils/http.rb
mattly-exegesis-0.2.6 lib/exegesis/utils/http.rb
mattly-exegesis-0.2.8 lib/exegesis/utils/http.rb