Module: Starry
- Defined in:
- lib/starry.rb,
lib/starry/version.rb
Defined Under Namespace
Classes: InnerList, Item, ParseError, Parser, SerializeError
Constant Summary
collapse
- VERSION =
'0.2.0'
Class Method Summary
collapse
Class Method Details
.parse_dictionary(input, symbolize_names: false) ⇒ Object
129
130
131
132
|
# File 'lib/starry.rb', line 129
def parse_dictionary(input, symbolize_names: false)
ensure_ascii_only(input)
Parser.new(input, symbolize_names: symbolize_names).parse(:dictionary)
end
|
.parse_item(input, symbolize_names: false) ⇒ Object
134
135
136
137
|
# File 'lib/starry.rb', line 134
def parse_item(input, symbolize_names: false)
ensure_ascii_only(input)
Parser.new(input, symbolize_names: symbolize_names).parse(:item)
end
|
.parse_list(input, symbolize_names: false) ⇒ Object
124
125
126
127
|
# File 'lib/starry.rb', line 124
def parse_list(input, symbolize_names: false)
ensure_ascii_only(input)
Parser.new(input, symbolize_names: symbolize_names).parse(:list)
end
|
.serialize(input) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/starry.rb', line 10
def serialize(input)
case input
when {}, []
return nil
when Hash
serialize_dictionary(input)
when Enumerable
serialize_list(input)
else
serialize_item(input)
end
end
|
.serialize_bare_item(input) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/starry.rb', line 66
def serialize_bare_item(input)
case input
when Integer
if input.abs >= 10 ** 15
raise SerializeError, "Integer value in HTTP Structured Field must have an absolute value less than 10 ** 15, but #{ input } given."
end
input.to_s
when Float
x = input.round(3, half: :even)
if x.abs >= 10 ** 12
raise SerializeError, "Numeric value in HTTP Structured Field must have an absolute value less than 10 ** 15, but #{ input } given."
end
x.to_s
when String
if input.encoding == Encoding::ASCII_8BIT
":#{ Base64.strict_encode64(input) }:"
else
unless input.match?(/\A[\u0020-\u007E]*\z/)
raise SerializeError, "String value in HTTP Structured Field must consist of only ASCII printable characters, but given value #{ input.inspect } does not meet that."
end
"\"#{ input.gsub(/\\|"/) { "\\#{ _1 }" } }\""
end
when Symbol
unless input.to_s.match?(/\A[A-Za-z*][!#$%&'*+\-.^_`|~0-9A-Za-z:\/]*\z/)
raise SerializeError, "The given value #{ input.inspect } contains characters that are not allowed as Token in HTTP Structured Field."
end
input.to_s
when true
'?1'
when false
'?0'
else
raise SerializeError, "The given value #{ input.inspect } cannnot be used as a bare item of HTTP Structured Field."
end
end
|
.serialize_dictionary(input) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/starry.rb', line 46
def serialize_dictionary(input)
input.transform_keys(&:to_s).map do |key, value|
if value == true
serialize_key(key)
elsif value.kind_of?(Item) && value.value == true
"#{ serialize_key(key) }#{ serialize_parameters(value.parameters) }"
else
"#{ serialize_key(key) }=#{ serialize_item_or_inner_list(value) }"
end
end.join(', ')
end
|
.serialize_item(input) ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/starry.rb', line 58
def serialize_item(input)
if input.kind_of?(Item)
input.to_s
else
serialize_bare_item(input)
end
end
|
.serialize_key(input) ⇒ Object
39
40
41
42
43
44
|
# File 'lib/starry.rb', line 39
def serialize_key(input)
unless input.match?(/\A[a-z*][a-z0-9_\-.*]*\z/)
raise SerializeError, "The given value #{ input.inspect } contains characters that are not allowed as a key for dictionary / parameters in HTTP Structured Field."
end
input
end
|
.serialize_list(input) ⇒ Object
23
24
25
26
27
|
# File 'lib/starry.rb', line 23
def serialize_list(input)
input.map do |item|
serialize_item_or_inner_list(item)
end.join(', ')
end
|
.serialize_parameters(input) ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'lib/starry.rb', line 29
def serialize_parameters(input)
input.transform_keys(&:to_s).map do |key, value|
if value == true
";#{ serialize_key(key) }"
else
";#{ serialize_key(key) }=#{ serialize_bare_item(value) }"
end
end.join('')
end
|