parsed-attributes 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/parsed/base.rb +10 -8
- data/lib/parsed/http/cookie.rb +19 -0
- data/lib/parsed/json.rb +4 -4
- data/lib/parsers/http/cookie.rb +151 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f81ae9f69a48cc5c297e88bd575697e490e9c952
|
4
|
+
data.tar.gz: 217584c4424daabf5aafcbaa070ef09be3e11f4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 411b1e5a04b11779e8cef16d3a443bbcccba89506b34449dc78d5fa48cd2920d72d02dc177aa577a9c646c1f0e352337d0fb703bcb46d9e9fab86c2453b2f413
|
7
|
+
data.tar.gz: b08606048a4e79f250a1e619ac9493b719e0f3989dc5c62095c7a9e03715557299785af27079d270e6e03880000cae92ed776cc4d64bffe7ce90ee84fde53846
|
data/lib/parsed/base.rb
CHANGED
@@ -15,9 +15,10 @@ module Parsed
|
|
15
15
|
# - @parsed_yaml
|
16
16
|
def __define_parsed_attributes_raw_methods(name)
|
17
17
|
attr_reader "raw_#{name}"
|
18
|
-
|
18
|
+
|
19
19
|
define_method "raw_#{name}=" do |raw|
|
20
20
|
self.instance_variable_set("@raw_#{name}", raw)
|
21
|
+
|
21
22
|
if self.instance_variable_defined?("@parsed_#{name}")
|
22
23
|
self.remove_instance_variable("@parsed_#{name}")
|
23
24
|
end
|
@@ -38,10 +39,11 @@ module Parsed
|
|
38
39
|
# And assumes the instance variables:
|
39
40
|
# - @raw_yaml
|
40
41
|
# - @parsed_yaml
|
42
|
+
#
|
41
43
|
# options (acts like Hash):: Modifies the exception handling behavior
|
42
44
|
# raises_once (boolean):: Setting to true will raise on the first error.
|
43
45
|
#
|
44
|
-
# This is mostly so that if you are catching and
|
46
|
+
# This is mostly so that if you are catching and
|
45
47
|
# printing error messages they won't flood the
|
46
48
|
# terminal.
|
47
49
|
#
|
@@ -53,6 +55,8 @@ module Parsed
|
|
53
55
|
# the parsed data.
|
54
56
|
def __define_parsed_attributes_parsed_methods(name, options={}, &parse_block)
|
55
57
|
parsed_varname = "@parsed_#{name}"
|
58
|
+
raise_once = options.delete :raise_once
|
59
|
+
raise_always = options.delete :raise
|
56
60
|
|
57
61
|
define_method "parsed_#{name}" do
|
58
62
|
if self.instance_variable_defined? parsed_varname
|
@@ -62,15 +66,13 @@ module Parsed
|
|
62
66
|
unless parse_block.nil?
|
63
67
|
begin
|
64
68
|
parsed = parse_block.call self.instance_variable_get("@raw_#{name}")
|
65
|
-
self.instance_variable_set(parsed_varname, parsed)
|
69
|
+
self.instance_variable_set(parsed_varname, parsed)
|
66
70
|
rescue StandardError => e
|
67
|
-
if
|
68
|
-
|
69
|
-
end
|
70
|
-
raise e if options[:raise] == true || options[:raise_once] == true
|
71
|
+
self.instance_variable_set(parsed_varname, nil) if raise_once == true
|
72
|
+
raise e if raise_always == true || raise_once == true
|
71
73
|
end
|
72
74
|
end
|
73
|
-
|
75
|
+
|
74
76
|
return self.instance_variable_get(parsed_varname)
|
75
77
|
end
|
76
78
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative '../base'
|
2
|
+
require_relative '../../parsers/http/cookie'
|
3
|
+
module Parsed
|
4
|
+
module HTTP
|
5
|
+
module Cookie
|
6
|
+
include Parsed::Base
|
7
|
+
|
8
|
+
##
|
9
|
+
# Defines an attribute pair to handle HTTP cookie data
|
10
|
+
# ---
|
11
|
+
# Accepts default options, see Parsed::Base
|
12
|
+
def http_cookie_attribute(name, options = {})
|
13
|
+
__define_parsed_attributes_all_methods name, options do |raw_value|
|
14
|
+
::Cookie.parse raw_value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/parsed/json.rb
CHANGED
@@ -2,16 +2,16 @@ require_relative 'base'
|
|
2
2
|
require 'json'
|
3
3
|
|
4
4
|
module Parsed
|
5
|
-
module
|
5
|
+
module JSON
|
6
6
|
include Parsed::Base
|
7
7
|
|
8
8
|
##
|
9
9
|
# Defines an attribute pair to handle JSON encoded date
|
10
10
|
# ---
|
11
|
-
# Accepts default options, see Parsed::Base
|
12
|
-
def json_attribute(name, options = {})
|
11
|
+
# Accepts default options, see Parsed::Base
|
12
|
+
def json_attribute(name, options = {})
|
13
13
|
__define_parsed_attributes_all_methods name, options do |raw_value|
|
14
|
-
JSON.parse raw_value
|
14
|
+
::JSON.parse raw_value
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
class Cookie
|
2
|
+
class ParserError < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
attr_accessor :name, :value, :expires, :max_age, :path, :domain, :secure, :http_only
|
6
|
+
def initialize(data = {})
|
7
|
+
@name = data[:name] if data.key? :name
|
8
|
+
@value = data[:value] if data.key? :value
|
9
|
+
@expires = data[:expires] if data.key? :expires
|
10
|
+
@max_age = data[:max_age] if data.key? :max_age
|
11
|
+
@path = data[:path] if data.key? :path
|
12
|
+
@domain = data[:domain] if data.key? :domain
|
13
|
+
@secure = data[:secure] if data.key? :secure
|
14
|
+
@http_only = data[:http_only] if data.key? :http_only
|
15
|
+
end
|
16
|
+
|
17
|
+
def age_left
|
18
|
+
return @max_age if @max_age.is_a? Fixnum
|
19
|
+
return (@max_age - Time.now).to_i
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
cookie = ["#{@name}=#{@value}"]
|
24
|
+
cookie << "Expires=#{@expires}" if defined? @expires
|
25
|
+
cookie << "Max-Age=#{@max_age == Time.at(0) ? 0 : self.age_left }" if defined? @max_age
|
26
|
+
cookie << "Domain=#{@domain}" if defined? @domain
|
27
|
+
cookie << "Path=#{@path}" if defined? @path
|
28
|
+
cookie << "Secure" if defined? @secure
|
29
|
+
cookie << "HttpOnly" if defined? @http_only
|
30
|
+
return cookie.join('; ')
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_h
|
34
|
+
h = { name: @name, value: @value }
|
35
|
+
h[:expires] = @expires if defined? @expires
|
36
|
+
h[:max_age] = @max_age if defined? @max_age
|
37
|
+
h[:domain] = @domain if defined? @domain
|
38
|
+
h[:path] = @path if defined? @path
|
39
|
+
h[:secure] = @secure if defined? @secure
|
40
|
+
h[:http_only] = @http_only if defined? @http_only
|
41
|
+
return h
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.parse(raw_value, options = {})
|
45
|
+
self.new Parser.new(options).parse!(raw_value).to_h
|
46
|
+
end
|
47
|
+
|
48
|
+
class Parser
|
49
|
+
attr_reader :name, :value, :expires, :max_age, :domain, :path, :secure, :http_only
|
50
|
+
|
51
|
+
def self.empty?(val)
|
52
|
+
return true if val.nil?
|
53
|
+
return true if val.respond_to?(:size) && val.size == 0
|
54
|
+
return false
|
55
|
+
end
|
56
|
+
|
57
|
+
def initialize(options = {})
|
58
|
+
@time = options[:time]
|
59
|
+
@default_path = options[:default_path] || ''
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_h
|
63
|
+
h = { name: @name, value: @value }
|
64
|
+
h[:expires] = @expires if defined? @expires
|
65
|
+
h[:max_age] = @max_age if defined? @max_age
|
66
|
+
h[:domain] = @domain if defined? @domain
|
67
|
+
h[:path] = @path if defined? @path
|
68
|
+
h[:secure] = @secure if defined? @secure
|
69
|
+
h[:http_only] = @http_only if defined? @http_only
|
70
|
+
return h
|
71
|
+
end
|
72
|
+
|
73
|
+
def clear!
|
74
|
+
%w(@name @value @expires @max_age @domain @path @secure @http_only).each do |var|
|
75
|
+
if self.instance_variable_defined? var
|
76
|
+
self.remove_instance_variable var
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def parse!(raw_value = nil)
|
82
|
+
raise ParserError.new "nil is unparseable" if raw_value.nil?
|
83
|
+
self.clear!
|
84
|
+
|
85
|
+
chunks = raw_value.to_s.split ';'
|
86
|
+
chunks.map! do |c|
|
87
|
+
si = c.index '='
|
88
|
+
si.nil? ? [c.strip] : [ c[0...si].strip, c[si + 1..-1].strip ]
|
89
|
+
end
|
90
|
+
self.name_and_value! chunks.shift
|
91
|
+
chunks.each {|c| self.attribute! c }
|
92
|
+
return self
|
93
|
+
end
|
94
|
+
|
95
|
+
def name_and_value!(chunk)
|
96
|
+
raise ParserError.new "Name cannot be blank" if Parser.empty? chunk
|
97
|
+
raise ParserError.new "Name/value pair must include '='" if chunk.size == 1
|
98
|
+
@name = chunk[0]
|
99
|
+
@value = chunk[1]
|
100
|
+
|
101
|
+
raise ParserError.new "Name cannot be blank" if Parser.empty? @name
|
102
|
+
end
|
103
|
+
|
104
|
+
def attribute!(chunk)
|
105
|
+
key = chunk[0].downcase
|
106
|
+
|
107
|
+
case key
|
108
|
+
when 'expires' then self.expires! chunk[1]
|
109
|
+
when 'max-age' then self.max_age! chunk[1]
|
110
|
+
when 'domain' then self.domain! chunk[1]
|
111
|
+
when 'path' then self.path! chunk[1]
|
112
|
+
when 'secure' then @secure = true
|
113
|
+
when 'httponly' then @http_only = true
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def expires!(val)
|
118
|
+
raise ParserError.new 'Expires cannot have a blank value' if Parser.empty?(val)
|
119
|
+
@expires = val
|
120
|
+
end
|
121
|
+
|
122
|
+
def max_age!(val)
|
123
|
+
raise ParserError.new 'Max-Age cannot have a blank value' if Parser.empty?(val)
|
124
|
+
|
125
|
+
unless val =~ /^-?\d+$/
|
126
|
+
raise ParserError.new "Expected integer for Max-Age instead of #{val}"
|
127
|
+
end
|
128
|
+
|
129
|
+
val = val.to_i
|
130
|
+
if val <= 0
|
131
|
+
@max_age = Time.at(0)
|
132
|
+
else
|
133
|
+
@max_age = (@time.nil? ? val : @time + val)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def domain!(val)
|
138
|
+
raise ParserError.new 'Domain cannot have a blank value' if Parser.empty?(val)
|
139
|
+
@domain = (val[0] == '.' ? val[1..-1] : val).downcase
|
140
|
+
end
|
141
|
+
|
142
|
+
def path!(val)
|
143
|
+
if Parser.empty?(val) || val[0] != '/'
|
144
|
+
@path = @default_path
|
145
|
+
else
|
146
|
+
@path = val
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parsed-attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Morgen Peschke
|
@@ -21,7 +21,9 @@ extensions: []
|
|
21
21
|
extra_rdoc_files: []
|
22
22
|
files:
|
23
23
|
- lib/parsed/base.rb
|
24
|
+
- lib/parsed/http/cookie.rb
|
24
25
|
- lib/parsed/json.rb
|
26
|
+
- lib/parsers/http/cookie.rb
|
25
27
|
homepage: https://github.com/morgen-peschke/parsed-attributes
|
26
28
|
licenses:
|
27
29
|
- MIT
|