Module: Ethon::Easy::Queryable

Included in:
Form, Params
Defined in:
lib/ethon/easy/queryable.rb

Overview

This module contains logic about building query parameters for url or form.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



10
11
12
13
# File 'lib/ethon/easy/queryable.rb', line 10

def self.included(base)
  base.send(:attr_accessor, :escape)
  base.send(:attr_accessor, :params_encoding)
end

Instance Method Details

#build_query_pairs(hash) ⇒ Array

Return query pairs build from a hash.

Examples:

Build query pairs.

action.build_query_pairs({a: 1, b: 2})
#=> [[:a, 1], [:b, 2]]

Parameters:

  • hash (Hash)

    The hash to go through.

Returns:

  • (Array)

    The array of query pairs.



62
63
64
65
66
67
68
# File 'lib/ethon/easy/queryable.rb', line 62

def build_query_pairs(hash)
  return [hash] if hash.is_a?(String)

  pairs = []
  recursively_generate_pairs(hash, nil, pairs)
  pairs
end

#empty?Boolean

Return wether there are elements in params or not.

Examples:

Return if params is empty.

form.empty?

Returns:

  • (Boolean)

    True if params is empty, else false.



21
22
23
# File 'lib/ethon/easy/queryable.rb', line 21

def empty?
  @params.empty?
end

#file_info(file) ⇒ Array

Return file info for a file.

Examples:

Return file info.

action.file_info(File.open('fubar', 'r'))

Parameters:

  • file (File)

    The file to handle.

Returns:

  • (Array)

    Array of informations.



78
79
80
81
82
83
84
85
# File 'lib/ethon/easy/queryable.rb', line 78

def file_info(file)
  filename = File.basename(file.path)
  [
    filename,
    mime_type(filename),
    File.expand_path(file.path)
  ]
end

#query_pairsArray

Return the query pairs.

Examples:

Return the query pairs.

params.query_pairs

Returns:

  • (Array)

    The query pairs.



49
50
51
# File 'lib/ethon/easy/queryable.rb', line 49

def query_pairs
  @query_pairs ||= build_query_pairs(@params)
end

#to_sString

Return the string representation of params.

Examples:

Return string representation.

params.to_s

Returns:

  • (String)

    The string representation.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ethon/easy/queryable.rb', line 31

def to_s
  @to_s ||= query_pairs.map{ |pair|
    return pair if pair.is_a?(String)

    if escape && @easy
      pair.map{ |e| @easy.escape(e.to_s) }.join("=")
    else
      pair.join("=")
    end
  }.join('&')
end