Class: Furi::QueryToken

Inherits:
Object
  • Object
show all
Defined in:
lib/furi/query_token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value) ⇒ QueryToken

Returns a new instance of QueryToken.



26
27
28
29
# File 'lib/furi/query_token.rb', line 26

def initialize(name, value)
  @name = name
  @value = value
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/furi/query_token.rb', line 3

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/furi/query_token.rb', line 3

def value
  @value
end

Class Method Details

.parse(token) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/furi/query_token.rb', line 5

def self.parse(token)
  case token
  when QueryToken
    token
  when String
    key, value = token.split('=', 2).map do |s|
      ::URI.decode_www_form_component(s)
    end
    key ||= ""
    new(key, value)
  when Array
    QueryToken.new(*token)
  else
    raise_parse_error(token)
  end
end

.raise_parse_error(token) ⇒ Object

Raises:



22
23
24
# File 'lib/furi/query_token.rb', line 22

def self.raise_parse_error(token)
  raise QueryParseError, "Can not parse query token #{token.inspect}"
end

Instance Method Details

#==(other) ⇒ Object



35
36
37
38
39
# File 'lib/furi/query_token.rb', line 35

def ==(other)
  other = self.class.parse(other)
  return false unless other
  to_s == other.to_s
end

#as_json(options = nil) ⇒ Object



49
50
51
# File 'lib/furi/query_token.rb', line 49

def as_json(options = nil)
  to_a
end

#inspectObject



53
54
55
# File 'lib/furi/query_token.rb', line 53

def inspect
  [name, value].join('=')
end

#to_aObject



31
32
33
# File 'lib/furi/query_token.rb', line 31

def to_a
  [name, value]
end

#to_sObject



41
42
43
44
45
46
47
# File 'lib/furi/query_token.rb', line 41

def to_s
  encoded_key = ::URI.encode_www_form_component(name.to_s)

  !value.nil? ?
    "#{encoded_key}=#{::URI.encode_www_form_component(value.to_s)}" :
    encoded_key
end