Module: Ethon::Easy::Http::Actionable

Included in:
Custom, Delete, Get, Head, Options, Patch, Post, Put
Defined in:
lib/ethon/easy/http/actionable.rb

Overview

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

Constant Summary collapse

QUERY_OPTIONS =
[ :params, :body, :params_encoding ]

Instance Method Summary collapse

Instance Method Details

#formForm

Return the form.

Examples:

Return form.

action.form

Returns:

  • (Form)

    The form.


74
75
76
# 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

#initialize(url, options) ⇒ Action

Create a new action.

Examples:

Create a new action.

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

Parameters:

  • url (String)

    The url.

  • options (Hash)

    The options.

Returns:

  • (Action)

    A new action.


23
24
25
26
# File 'lib/ethon/easy/http/actionable.rb', line 23

def initialize(url, options)
  @url = url
  @options, @query_options = parse_options(options)
end

#optionsHash

Return the options hash.

Examples:

Return options.

action.options

Returns:

  • (Hash)

    The options.


44
45
46
# File 'lib/ethon/easy/http/actionable.rb', line 44

def options
  @options
end

#paramsParams

Return the params.

Examples:

Return params.

action.params

Returns:


64
65
66
# File 'lib/ethon/easy/http/actionable.rb', line 64

def params
  @params ||= Params.new(@easy, query_options.fetch(:params, nil))
end

#params_encodingObject

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

Examples:

Get encoding from options

action.params_encoding

84
85
86
# File 'lib/ethon/easy/http/actionable.rb', line 84

def params_encoding
  @params_encoding ||= query_options.fetch(:params_encoding, :typhoeus)
end

#query_optionsHash

Returns the query options hash.

Examples:

Return query options.

action.query_options

Returns:

  • (Hash)

    The query options.


54
55
56
# File 'lib/ethon/easy/http/actionable.rb', line 54

def query_options
  @query_options
end

#set_form(easy) ⇒ Object

Setup request with form.

Examples:

Setup nothing.

action.set_form(easy)

Parameters:

  • easy (Easy)

    The easy to setup.


136
137
# File 'lib/ethon/easy/http/actionable.rb', line 136

def set_form(easy)
end

#set_params(easy) ⇒ Object

Setup request with params.

Examples:

Setup nothing.

action.set_params(easy)

Parameters:

  • easy (Easy)

    The easy to setup.


117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Object

Setup everything necessary for a proper request.

Examples:

setup.

action.setup(easy)

Parameters:

  • easy (easy)

    the easy to setup.


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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

#urlString

Return the url.

Examples:

Return url.

action.url

Returns:

  • (String)

    The url.


34
35
36
# File 'lib/ethon/easy/http/actionable.rb', line 34

def url
  @url
end