Module: Prismic::DefaultHTTPClient

Defined in:
lib/prismic.rb

Overview

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

Class Method Summary collapse

Class Method Details

.get(uri, data = {}, headers = {}) ⇒ Object

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)


623
624
625
626
627
628
629
# 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 = {}) ⇒ Object

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)


636
637
638
639
640
641
642
# 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) ⇒ Object



644
645
646
647
648
649
650
651
652
653
654
# 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