class Hurley::Client

Attributes

connection[W]
header[R]
request_options[R]
ssl_options[R]
url[R]

Public Class Methods

new(endpoint = nil) { |self| ... } click to toggle source
# File lib/hurley/client.rb, line 13
def initialize(endpoint = nil)
  @before_callbacks = []
  @after_callbacks = []
  @url = Url.parse(endpoint)
  @header = Header.new :user_agent => Hurley::USER_AGENT
  @connection = nil
  @request_options = RequestOptions.new
  @ssl_options = SslOptions.new
  yield self if block_given?
end

Public Instance Methods

after_call(name_or_callback = nil) click to toggle source
# File lib/hurley/client.rb, line 98
def after_call(name_or_callback = nil)
  @after_callbacks << (block_given? ?
    NamedCallback.for(name_or_callback  , Proc.new) :
    NamedCallback.for(nil, name_or_callback))
end
after_callbacks() click to toggle source
# File lib/hurley/client.rb, line 108
def after_callbacks
  @after_callbacks.map(&:name)
end
before_call(name_or_callback = nil) click to toggle source
# File lib/hurley/client.rb, line 92
def before_call(name_or_callback = nil)
  @before_callbacks << (block_given? ?
    NamedCallback.for(name_or_callback, Proc.new) :
    NamedCallback.for(nil, name_or_callback))
end
before_callbacks() click to toggle source
# File lib/hurley/client.rb, line 104
def before_callbacks
  @before_callbacks.map(&:name)
end
call(request) click to toggle source
# File lib/hurley/client.rb, line 88
def call(request)
  call_with_redirects(request, [])
end
connection() click to toggle source
# File lib/hurley/client.rb, line 32
def connection
  @connection ||= Hurley.default_connection
end
delete(path, query = nil) { |req| ... } click to toggle source
# File lib/hurley/client.rb, line 74
def delete(path, query = nil)
  req = request(:delete, path)
  req.query.update(query) if query
  yield req if block_given?
  call(req)
end
get(path, query = nil) { |req| ... } click to toggle source
# File lib/hurley/client.rb, line 43
def get(path, query = nil)
  req = request(:get, path)
  req.query.update(query) if query
  yield req if block_given?
  call(req)
end
head(path, query = nil) { |req| ... } click to toggle source
# File lib/hurley/client.rb, line 36
def head(path, query = nil)
  req = request(:head, path)
  req.query.update(query) if query
  yield req if block_given?
  call(req)
end
options(path, query = nil) { |req| ... } click to toggle source
# File lib/hurley/client.rb, line 81
def options(path, query = nil)
  req = request(:options, path)
  req.query.update(query) if query
  yield req if block_given?
  call(req)
end
patch(path, body = nil, ctype = nil) { |req| ... } click to toggle source
# File lib/hurley/client.rb, line 50
def patch(path, body = nil, ctype = nil)
  req = request(:patch, path)
  req.body = body if body
  req.header[:content_type] = ctype if ctype
  yield req if block_given?
  call(req)
end
post(path, body = nil, ctype = nil) { |req| ... } click to toggle source
# File lib/hurley/client.rb, line 66
def post(path, body = nil, ctype = nil)
  req = request(:post, path)
  req.body = body if body
  req.header[:content_type] = ctype if ctype
  yield req if block_given?
  call(req)
end
put(path, body = nil, ctype = nil) { |req| ... } click to toggle source
# File lib/hurley/client.rb, line 58
def put(path, body = nil, ctype = nil)
  req = request(:put, path)
  req.body = body if body
  req.header[:content_type] = ctype if ctype
  yield req if block_given?
  call(req)
end
request(method, path) click to toggle source
# File lib/hurley/client.rb, line 112
def request(method, path)
  Request.new(method, Url.join(@url, path), @header.dup, nil, @request_options.dup, @ssl_options.dup)
end

Private Instance Methods

call_with_redirects(request, via) click to toggle source
# File lib/hurley/client.rb, line 118
def call_with_redirects(request, via)
  @before_callbacks.each { |cb| cb.call(request) }

  request.prepare!
  response = connection.call(request)

  @after_callbacks.each { |cb| cb.call(response) }

  if response.automatically_redirect?(via)
    return call_with_redirects(response.location, via << request)
  end

  response.via = via
  response
end