module Prismic::DefaultHTTPClient

Default HTTP client implementation, using the standard Net::HTTP library.

Public Class Methods

get(uri, data={}, headers={}) click to toggle source

Performs a GET call and returns the result

The result must respond to

  • code: returns the response's HTTP status code (as number or String)

  • body: returns the response's body (as String)

# File lib/prismic.rb, line 623
def get(uri, data={}, headers={})
  uri = URI(uri) if uri.is_a?(String)
  add_query(uri, data)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme =~ /https/i
  http.get(uri.request_uri, headers)
end
post(uri, data={}, headers={}) click to toggle source

Performs a POST call and returns the result

The result must respond to

  • code: returns the response's HTTP status code (as number or String)

  • body: returns the response's body (as String)

# File lib/prismic.rb, line 636
def post(uri, data={}, headers={})
  uri = URI(uri) if uri.is_a?(String)
  add_query(uri, data)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme =~ /https/i
  http.post(uri.path, uri.query, headers)
end
url_encode(data) click to toggle source
# File lib/prismic.rb, line 644
def url_encode(data)
  # Can't use URI.encode_www_form (doesn't support multi-values in 1.9.2)
  encode = ->(k, v){ "#{k}=#{CGI::escape(v)}" }
  data.map { |k, vs|
    if vs.is_a?(Array)
      vs.map{|v| encode.(k, v) }.join("&")
    else
      encode.(k, vs)
    end
  }.join("&")
end

Private Class Methods

add_query(uri, query) click to toggle source
# File lib/prismic.rb, line 658
def add_query(uri, query)
  query = url_encode(query)
  query = "#{uri.query}&#{query}"if uri.query && !uri.query.empty?
  uri.query = query
end