llt-core 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/llt/core/api/helpers.rb +25 -11
- data/lib/llt/core/containable.rb +13 -16
- data/lib/llt/core/version.rb +1 -1
- data/llt-core.gemspec +1 -0
- data/spec/lib/llt/core/api/helpers_spec.rb +28 -12
- data/spec/lib/llt/core/containable_spec.rb +8 -1
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1f24fb2e3f840c885ba1156442dd42cc21eacd7
|
4
|
+
data.tar.gz: ea0e69af62b090a427f163cfa545c6f0bc5a84a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ac77f13805196d581a12fba96cdf3dde2f51a8e999ad21018eb2954a715bee0c2ca9b08ed13c823e32aa2bb9912c54fdfd577a67d3737892e5e0f457e789fa5
|
7
|
+
data.tar.gz: b797df93991e0b930305ed47a4fc8eb03d7c3a40be69f0a9f93aad356fc122bf7ee3a08f74084283d129cf8be9f766d1d95401cbf14f5abc5935ded756be446e
|
data/lib/llt/core/api/helpers.rb
CHANGED
@@ -1,28 +1,29 @@
|
|
1
1
|
require 'cgi'
|
2
|
+
require 'xml_escape'
|
2
3
|
|
3
4
|
module LLT
|
4
5
|
module Core
|
5
6
|
module Api
|
6
7
|
module Helpers
|
7
|
-
|
8
|
-
CGI.unescape(text)
|
9
|
-
end
|
10
|
-
|
11
|
-
def u(text)
|
12
|
-
CGI.escape(text)
|
13
|
-
end
|
8
|
+
include XmlEscape
|
14
9
|
|
15
10
|
# tries to resolve an uri or a text included in the params
|
11
|
+
#
|
12
|
+
# strips any incoming xml declaration because it gets added back in at
|
13
|
+
# the end and otherwise will be duped
|
14
|
+
# if an xml declaration is included, the xml param is set to true
|
16
15
|
def extract_text(params)
|
17
|
-
|
18
|
-
|
16
|
+
text = get_text(params)
|
17
|
+
if has_xml_declaration?(text)
|
18
|
+
params[:xml] = true
|
19
|
+
text.sub(XML_DECLARATION_REGEXP, '')
|
19
20
|
else
|
20
|
-
|
21
|
+
text
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
24
25
|
def extract_markup_params(params)
|
25
|
-
mu_params = %i{ recursive indexing inline }
|
26
|
+
mu_params = %i{ recursive indexing inline id_as }
|
26
27
|
extracted = [params[:tags]]
|
27
28
|
relevant = mu_params.each_with_object({}) do |param, h|
|
28
29
|
val = params[param]
|
@@ -57,8 +58,21 @@ module LLT
|
|
57
58
|
|
58
59
|
private
|
59
60
|
|
61
|
+
XML_DECLARATION_REGEXP = /<\?xml.*?\?>/
|
60
62
|
XML_DECLARATION = %{<?xml version="1.0" encoding="UTF-8"?>}
|
61
63
|
|
64
|
+
def has_xml_declaration?(txt)
|
65
|
+
txt.match(XML_DECLARATION_REGEXP)
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_text(params)
|
69
|
+
if uri = params[:uri]
|
70
|
+
get_from_uri(uri)
|
71
|
+
else
|
72
|
+
params[:text]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
62
76
|
def typecast(val)
|
63
77
|
if val.kind_of?(Array)
|
64
78
|
val.map { |e| typecast(e) }
|
data/lib/llt/core/containable.rb
CHANGED
@@ -28,6 +28,7 @@ module LLT
|
|
28
28
|
def to_xml(tags = nil, indexing: true,
|
29
29
|
recursive: true,
|
30
30
|
inline: false,
|
31
|
+
id_as: 'n',
|
31
32
|
attrs: {})
|
32
33
|
|
33
34
|
# for easier recursion it's solved in a way that might
|
@@ -37,17 +38,17 @@ module LLT
|
|
37
38
|
end_of_recursion = false
|
38
39
|
|
39
40
|
val = if recursive && all? { |e| e.respond_to?(:to_xml)}
|
40
|
-
attrs.merge!(inline_id(tag)) if inline && indexing
|
41
|
-
recursive_xml(tags, indexing, inline, attrs)
|
41
|
+
attrs.merge!(inline_id(tag, id_as)) if inline && indexing
|
42
|
+
recursive_xml(tags, indexing, inline, id_as, attrs)
|
42
43
|
else
|
43
44
|
end_of_recursion = true
|
44
45
|
as_xml
|
45
46
|
end
|
46
47
|
|
47
48
|
if inline
|
48
|
-
end_of_recursion ? wrap_with_xml(tag, val, indexing, attrs) : val
|
49
|
+
end_of_recursion ? wrap_with_xml(tag, val, indexing, id_as, attrs) : val
|
49
50
|
else
|
50
|
-
wrap_with_xml(tag, val, indexing, attrs)
|
51
|
+
wrap_with_xml(tag, val, indexing, id_as, attrs)
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
@@ -89,8 +90,8 @@ module LLT
|
|
89
90
|
|
90
91
|
# id is represented as @n attribute in the xml, as xml:id
|
91
92
|
# is reserved for something else
|
92
|
-
def wrap_with_xml(tag, string, indexing, attrs = {})
|
93
|
-
merge_id!(attrs) if indexing && @id
|
93
|
+
def wrap_with_xml(tag, string, indexing, id_as, attrs = {})
|
94
|
+
merge_id!(id_as, attrs) if indexing && @id
|
94
95
|
attr = attrs.any? ? " #{to_xml_attrs(attrs)}" : ''
|
95
96
|
"<#{tag}#{attr}>#{string}</#{tag}>"
|
96
97
|
end
|
@@ -99,23 +100,19 @@ module LLT
|
|
99
100
|
attrs.map { |k, v| %{#{k}="#{v}"} }.join(' ')
|
100
101
|
end
|
101
102
|
|
102
|
-
def recursive_xml(tags, indexing, inline, attrs)
|
103
|
+
def recursive_xml(tags, indexing, inline, id_as, attrs)
|
103
104
|
each_with_object('') do |element, s|
|
104
105
|
s << element.to_xml(tags.clone, indexing: indexing, recursive: true,
|
105
|
-
inline: inline, attrs: attrs)
|
106
|
+
inline: inline, id_as: id_as, attrs: attrs)
|
106
107
|
end
|
107
108
|
end
|
108
109
|
|
109
|
-
def merge_id!(attrs)
|
110
|
-
attrs.merge!(
|
110
|
+
def merge_id!(id_as, attrs)
|
111
|
+
attrs.merge!(id_as => @id,)
|
111
112
|
end
|
112
113
|
|
113
|
-
def
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
def inline_id(tag)
|
118
|
-
{ "#{tag}_#{id_as_xml}" => @id }
|
114
|
+
def inline_id(tag, id_as)
|
115
|
+
{ "#{tag}_#{id_as}" => @id }
|
119
116
|
end
|
120
117
|
|
121
118
|
module ClassMethods
|
data/lib/llt/core/version.rb
CHANGED
data/llt-core.gemspec
CHANGED
@@ -7,22 +7,33 @@ describe LLT::Core::Api::Helpers do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
describe "#extract_text" do
|
10
|
-
|
11
|
-
|
10
|
+
context "text param" do
|
11
|
+
it "tries to resolve a passed or a given text param" do
|
12
|
+
dummy.extract_text(:text => "some text").should == "some text"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "strips away the xml declaration" do
|
16
|
+
dummy.extract_text(:text => %{<?xml version="1.0" encoding="UTF-8"?>some text}).should == "some text"
|
17
|
+
end
|
12
18
|
end
|
13
|
-
end
|
14
19
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
20
|
+
context "uri param" do
|
21
|
+
it "calls to the given URI to retrieve the text" do
|
22
|
+
allow(dummy).to receive(:get_from_uri) { "text from uri" }
|
23
|
+
dummy.extract_text(:uri => "some uri").should == "text from uri"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "strips away the xml declaration" do
|
27
|
+
allow(dummy).to receive(:get_from_uri) { %{<?xml version="1.0" encoding="UTF-8"?>text from uri} }
|
28
|
+
dummy.extract_text(:uri => "some uri").should == "text from uri"
|
29
|
+
end
|
19
30
|
end
|
20
|
-
end
|
21
31
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
32
|
+
it "sets xml param to true when there is an xml declaration present" do
|
33
|
+
params = { text: %{<?xml version="1.0"?>some text} }
|
34
|
+
dummy.extract_text(params).should == 'some text'
|
35
|
+
params.should have_key(:xml)
|
36
|
+
params[:xml].should be_true
|
26
37
|
end
|
27
38
|
end
|
28
39
|
|
@@ -32,6 +43,11 @@ describe LLT::Core::Api::Helpers do
|
|
32
43
|
dummy.extract_markup_params(params).should == [nil, { recursive: true }]
|
33
44
|
end
|
34
45
|
|
46
|
+
it "extracts the id_as param" do
|
47
|
+
params = { 'id_as' => 'id' }
|
48
|
+
dummy.extract_markup_params(params).should == [nil, { id_as: 'id'} ]
|
49
|
+
end
|
50
|
+
|
35
51
|
it "returns an array that should be exploded when used with #to_xml" do
|
36
52
|
params = { recursive: true, tags: %w{ a b }}
|
37
53
|
extracted = [%w{ a b }, { recursive: true }]
|
@@ -125,7 +125,7 @@ describe LLT::Core::Containable do
|
|
125
125
|
sentence.to_xml(['a', nil, 'c'], recursive: true).should == result
|
126
126
|
end
|
127
127
|
|
128
|
-
it "can include the id of an element, as @n attribute" do
|
128
|
+
it "can include the id of an element, as @n attribute by default" do
|
129
129
|
dummy.xml_tag 's'
|
130
130
|
other_dummy.xml_tag 'w'
|
131
131
|
|
@@ -136,6 +136,13 @@ describe LLT::Core::Containable do
|
|
136
136
|
sentence.to_xml(indexing: true, recursive: true).should == result
|
137
137
|
end
|
138
138
|
|
139
|
+
it "can represent a custom id attribute identifier" do
|
140
|
+
dummy.xml_tag 's'
|
141
|
+
sentence = dummy.new('test', 1)
|
142
|
+
result = '<s id="1">test</s>'
|
143
|
+
sentence.to_xml(id_as: 'id').should == result
|
144
|
+
end
|
145
|
+
|
139
146
|
it "recursive representation can be inlined" do
|
140
147
|
dummy.xml_tag 's'
|
141
148
|
other_dummy.xml_tag 'w'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: llt-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- LFDM
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: xml_escape
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
111
125
|
description: LLT::Core classes and modules
|
112
126
|
email:
|
113
127
|
- 1986gh@gmail.com
|
@@ -154,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
168
|
version: '0'
|
155
169
|
requirements: []
|
156
170
|
rubyforge_project:
|
157
|
-
rubygems_version: 2.
|
171
|
+
rubygems_version: 2.2.0
|
158
172
|
signing_key:
|
159
173
|
specification_version: 4
|
160
174
|
summary: Contains classes and modules that are shared across several llt gems
|