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

73
74
75
# File 'lib/ethon/easy/http/actionable.rb', line 73

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", {})

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

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

#optionsHash

Return the options hash.

Examples:

Return options.

action.options

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

def options
  @options
end

#paramsParams

Return the params.

Examples:

Return params.

action.params

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

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

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

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

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

def query_options
  @query_options
end

#set_form(easy) ⇒ Object

Setup request with form.

Examples:

Setup nothing.

action.set_form(easy)

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

def set_form(easy)
end

#set_params(easy) ⇒ Object

Setup request with params.

Examples:

Setup nothing.

action.set_params(easy)

116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ethon/easy/http/actionable.rb', line 116

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)

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ethon/easy/http/actionable.rb', line 93

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

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

def url
  @url
end