This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
# frozen_string_literal: true
|
||||
require 'ethon/easy/http/putable'
|
||||
require 'ethon/easy/http/postable'
|
||||
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
# This module represents a Http Action and is a factory
|
||||
# for more real actions like GET, HEAD, POST and PUT.
|
||||
module Actionable
|
||||
|
||||
QUERY_OPTIONS = [ :params, :body, :params_encoding ]
|
||||
|
||||
# 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.
|
||||
def initialize(url, options)
|
||||
@url = url
|
||||
@options, @query_options = parse_options(options)
|
||||
end
|
||||
|
||||
# Return the url.
|
||||
#
|
||||
# @example Return url.
|
||||
# action.url
|
||||
#
|
||||
# @return [ String ] The url.
|
||||
def url
|
||||
@url
|
||||
end
|
||||
|
||||
# Return the options hash.
|
||||
#
|
||||
# @example Return options.
|
||||
# action.options
|
||||
#
|
||||
# @return [ Hash ] The options.
|
||||
def options
|
||||
@options
|
||||
end
|
||||
|
||||
# Returns the query options hash.
|
||||
#
|
||||
# @example Return query options.
|
||||
# action.query_options
|
||||
#
|
||||
# @return [ Hash ] The query options.
|
||||
def query_options
|
||||
@query_options
|
||||
end
|
||||
|
||||
# Return the params.
|
||||
#
|
||||
# @example Return params.
|
||||
# action.params
|
||||
#
|
||||
# @return [ Params ] The params.
|
||||
def params
|
||||
@params ||= Params.new(@easy, query_options.fetch(:params, nil))
|
||||
end
|
||||
|
||||
# Return the form.
|
||||
#
|
||||
# @example Return form.
|
||||
# action.form
|
||||
#
|
||||
# @return [ Form ] The form.
|
||||
def form
|
||||
@form ||= Form.new(@easy, query_options.fetch(:body, nil), options.fetch(:multipart, nil))
|
||||
end
|
||||
|
||||
# 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
|
||||
#
|
||||
def params_encoding
|
||||
@params_encoding ||= query_options.fetch(:params_encoding, :typhoeus)
|
||||
end
|
||||
|
||||
# Setup everything necessary for a proper request.
|
||||
#
|
||||
# @example setup.
|
||||
# action.setup(easy)
|
||||
#
|
||||
# @param [ easy ] easy the easy to setup.
|
||||
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
|
||||
|
||||
# Setup request with params.
|
||||
#
|
||||
# @example Setup nothing.
|
||||
# action.set_params(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
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 request with form.
|
||||
#
|
||||
# @example Setup nothing.
|
||||
# action.set_form(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def set_form(easy)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
|
||||
# This class knows everything about making requests for custom HTTP verbs.
|
||||
class Custom
|
||||
include Ethon::Easy::Http::Actionable
|
||||
include Ethon::Easy::Http::Postable
|
||||
|
||||
def initialize(verb, url, options)
|
||||
@verb = verb
|
||||
super(url, options)
|
||||
end
|
||||
|
||||
# Setup easy to make a request.
|
||||
#
|
||||
# @example Setup.
|
||||
# custom.set_params(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def setup(easy)
|
||||
super
|
||||
easy.customrequest = @verb
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
|
||||
# This class knows everything about making DELETE requests.
|
||||
class Delete
|
||||
include Ethon::Easy::Http::Actionable
|
||||
include Ethon::Easy::Http::Postable
|
||||
|
||||
# Setup easy to make a DELETE request.
|
||||
#
|
||||
# @example Setup customrequest.
|
||||
# delete.setup(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def setup(easy)
|
||||
super
|
||||
easy.customrequest = "DELETE"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
|
||||
# This class knows everything about making GET requests.
|
||||
class Get
|
||||
include Ethon::Easy::Http::Actionable
|
||||
include Ethon::Easy::Http::Postable
|
||||
|
||||
# Setup easy to make a GET request.
|
||||
#
|
||||
# @example Setup.
|
||||
# get.set_params(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def setup(easy)
|
||||
super
|
||||
easy.customrequest = "GET" unless form.empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
|
||||
# This class knows everything about making HEAD requests.
|
||||
class Head
|
||||
include Ethon::Easy::Http::Actionable
|
||||
include Ethon::Easy::Http::Postable
|
||||
|
||||
# Setup easy to make a HEAD request.
|
||||
#
|
||||
# @example Setup.
|
||||
# get.set_params(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def setup(easy)
|
||||
super
|
||||
easy.nobody = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
|
||||
# This class knows everything about making OPTIONS requests.
|
||||
class Options
|
||||
include Ethon::Easy::Http::Actionable
|
||||
include Ethon::Easy::Http::Postable
|
||||
|
||||
# Setup easy to make a OPTIONS request.
|
||||
#
|
||||
# @example Setup.
|
||||
# options.setup(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def setup(easy)
|
||||
super
|
||||
easy.customrequest = "OPTIONS"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
|
||||
# This class knows everything about making PATCH requests.
|
||||
class Patch
|
||||
include Ethon::Easy::Http::Actionable
|
||||
include Ethon::Easy::Http::Postable
|
||||
|
||||
# Setup easy to make a PATCH request.
|
||||
#
|
||||
# @example Setup.
|
||||
# patch.setup(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def setup(easy)
|
||||
super
|
||||
easy.customrequest = "PATCH"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
# This class knows everything about making POST requests.
|
||||
class Post
|
||||
include Ethon::Easy::Http::Actionable
|
||||
include Ethon::Easy::Http::Postable
|
||||
|
||||
# Setup easy to make a POST request.
|
||||
#
|
||||
# @example Setup.
|
||||
# post.setup(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def setup(easy)
|
||||
super
|
||||
if form.empty?
|
||||
easy.postfieldsize = 0
|
||||
easy.copypostfields = ""
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,32 @@
|
||||
# frozen_string_literal: true
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
|
||||
# This module contains logic for setting up a [multipart] POST body.
|
||||
module Postable
|
||||
|
||||
# Set things up when form is provided.
|
||||
# Deals with multipart forms.
|
||||
#
|
||||
# @example Setup.
|
||||
# post.set_form(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def set_form(easy)
|
||||
easy.url ||= url
|
||||
form.params_encoding = params_encoding
|
||||
if form.multipart?
|
||||
form.escape = false
|
||||
form.materialize
|
||||
easy.httppost = form.first.read_pointer
|
||||
else
|
||||
form.escape = easy.escape?
|
||||
easy.postfieldsize = form.to_s.bytesize
|
||||
easy.copypostfields = form.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
|
||||
# This class knows everything about making PUT requests.
|
||||
class Put
|
||||
include Ethon::Easy::Http::Actionable
|
||||
include Ethon::Easy::Http::Putable
|
||||
|
||||
# Setup easy to make a PUT request.
|
||||
#
|
||||
# @example Setup.
|
||||
# put.setup(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def setup(easy)
|
||||
super
|
||||
if form.empty?
|
||||
easy.upload = true
|
||||
easy.infilesize = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
module Ethon
|
||||
class Easy
|
||||
module Http
|
||||
|
||||
# This module contains logic about setting up a PUT body.
|
||||
module Putable
|
||||
# Set things up when form is provided.
|
||||
# Deals with multipart forms.
|
||||
#
|
||||
# @example Setup.
|
||||
# put.set_form(easy)
|
||||
#
|
||||
# @param [ Easy ] easy The easy to setup.
|
||||
def set_form(easy)
|
||||
easy.upload = true
|
||||
form.escape = true
|
||||
form.params_encoding = params_encoding
|
||||
easy.infilesize = form.to_s.bytesize
|
||||
easy.set_read_callback(form.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user