module Ethon::Easy::Http::Actionable

This module represents a Http Action and is a factory for more real actions like GET, HEAD, POST and PUT.

Constants

QUERY_OPTIONS

Public Class Methods

new(url, options) click to toggle source

Create a new action.

@example Create a new action.

Action.new("www.example.com", {})

@param [ String ] url The url. @param [ Hash ] options The options.

@return [ Action ] A new action.

# File lib/ethon/easy/http/actionable.rb, line 23
def initialize(url, options)
  @url = url
  @options, @query_options = parse_options(options)
end

Public Instance Methods

form() click to toggle source

Return the form.

@example Return form.

action.form

@return [ Form ] The form.

# File lib/ethon/easy/http/actionable.rb, line 74
def form
  @form ||= Form.new(@easy, query_options.fetch(:body, nil), options.fetch(:multipart, nil))
end
options() click to toggle source

Return the options hash.

@example Return options.

action.options

@return [ Hash ] The options.

# File lib/ethon/easy/http/actionable.rb, line 44
def options
  @options
end
params() click to toggle source

Return the params.

@example Return params.

action.params

@return [ Params ] The params.

# File lib/ethon/easy/http/actionable.rb, line 64
def params
  @params ||= Params.new(@easy, query_options.fetch(:params, nil))
end
params_encoding() click to toggle source

Get the requested array encoding. By default it's :typhoeus, but it can also be set to :rack.

@example Get encoding from options

action.params_encoding
# File lib/ethon/easy/http/actionable.rb, line 84
def params_encoding
  @params_encoding ||= query_options.fetch(:params_encoding, :typhoeus)
end
query_options() click to toggle source

Returns the query options hash.

@example Return query options.

action.query_options

@return [ Hash ] The query options.

# File lib/ethon/easy/http/actionable.rb, line 54
def query_options
  @query_options
end
set_form(easy) click to toggle source

Setup request with form.

@example Setup nothing.

action.set_form(easy)

@param [ Easy ] easy The easy to setup.

# File lib/ethon/easy/http/actionable.rb, line 136
def set_form(easy)
end
set_params(easy) click to toggle source

Setup request with params.

@example Setup nothing.

action.set_params(easy)

@param [ Easy ] easy The easy to setup.

# File lib/ethon/easy/http/actionable.rb, line 117
def set_params(easy)
  params.escape = easy.escape?
  params.params_encoding = params_encoding

  base_url, base_params = url.split('?')
  base_url << '?'
  base_url << base_params.to_s
  base_url << '&' if base_params
  base_url << params.to_s

  easy.url = base_url
end
setup(easy) click to toggle source

Setup everything necessary for a proper request.

@example setup.

action.setup(easy)

@param [ easy ] easy the easy to setup.

# File lib/ethon/easy/http/actionable.rb, line 94
def setup(easy)
  @easy = easy

  # Order is important, @easy will be used to provide access to options
  # relevant to the following operations (like whether or not to escape
  # values).
  easy.set_attributes(options)

  set_form(easy) unless form.empty?

  if params.empty?
    easy.url = url
  else
    set_params(easy)
  end
end
url() click to toggle source

Return the url.

@example Return url.

action.url

@return [ String ] The url.

# File lib/ethon/easy/http/actionable.rb, line 34
def url
  @url
end

Private Instance Methods

parse_options(options) click to toggle source
# File lib/ethon/easy/http/actionable.rb, line 141
def parse_options(options)
  query_options = {}
  options = options.dup

  QUERY_OPTIONS.each do |query_option|
    if options.key?(query_option)
      query_options[query_option] = options.delete(query_option)
    end
  end

  return options, query_options
end