tzispa_helpers 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3598394c1f006b21b86ff4f7be1753c6bae77c8
4
- data.tar.gz: e4571c3f6391e50e7a339829767ce80248982746
3
+ metadata.gz: 59e3ae3ed4d6a0bfcb61eb4889a28d9e5860fa12
4
+ data.tar.gz: 8084288b0d207595fb6f29a5a8eaddf0faef0c9e
5
5
  SHA512:
6
- metadata.gz: 4850753f0916e933e257987974012fa890c0ae7364f1da1ed4e6e2988b553dd3681e2abfaf463cce57260c0d2dfbeb45ad539f21bdb79748fb7c952eaa7562fb
7
- data.tar.gz: 0d1cfa416eff89b8ddb95cda88a3eb46ff91e5e14527fe4cce1d9744acea0c74b4112161d5eae9dae09f9cb6d121f40aa1581f4e330a06570b962b3374f9704f
6
+ metadata.gz: 2e289fe238d46591a6510818d1391735fc5b4d4e77bedd6e09ccd414a560d90691ef300444be26366fa8f716bd1b5f5252488ad0220750db2b2737ce96bf233e
7
+ data.tar.gz: 0d0ff069448e3fded14e70cc835ec6cf7016550473474fbda50236a58b7ab1de3e8b39cf90e9f585fa34df1e64e2c7baefa90bbf812c0da7781b77cac467f1f9
data/CHANGELOG.md CHANGED
@@ -1,8 +1,10 @@
1
1
  Tzispa Helpers
2
2
 
3
- ## v0.1.5
3
+ ## v0.1.6
4
+ - Add raise exception for debug in send_smtp_mail
5
+
4
6
  ## v0.1.4
5
- -- Fix secret generator in security
7
+ - Fix secret generator in security
6
8
 
7
9
  ## v0.1.3
8
10
  - Added mime module, code moved partially from response
@@ -6,15 +6,24 @@ require 'htmlentities'
6
6
  require 'reverse_markdown'
7
7
  require 'unicode_utils'
8
8
  require 'redcarpet'
9
+ require 'tzispa/helpers/hash_trans'
9
10
 
10
11
  module Tzispa
11
12
  module Helpers
12
13
  module Crawler
13
14
 
15
+ include Tzispa::Helpers::HashTrans
16
+
17
+ class CrawlerError < StandardError; end
18
+
14
19
 
15
20
  def crawler_save_file(url, dest_file)
16
- File.open("#{dest_file}", 'wb') do |fo|
17
- fo.write open(url).read
21
+ begin
22
+ File.open("#{dest_file}", 'wb') do |fo|
23
+ fo.write open(url).read
24
+ end
25
+ rescue => ex
26
+ raise CrawlerError.new "Error in crawler_save_file '#{url}': #{ex.message}"
18
27
  end
19
28
  end
20
29
 
@@ -27,33 +36,88 @@ module Tzispa
27
36
  end
28
37
  end
29
38
 
30
- def crawler_table_to_dl(source, table_path, columns=2, excluded_terms=[])
39
+ def crawler_table_to_dl(noko, table_path, columns, excluded_terms: [], fussion_terms:{})
31
40
  String.new.tap { |content|
32
- dt, dd = Array.new, Array.new
33
- htmee = HTMLEntities.new
34
41
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new)
35
- Nokogiri::HTML(source)&.xpath(table_path).collect { |row|
36
- dterm = htmee.decode(row.at_xpath('td[1]')&.content).strip
37
- if dterm.length > 0 && !excluded_terms.include?(UnicodeUtils.downcase dterm)
38
- dt << dterm
39
- dd << (2..columns).map { |i|
40
- ReverseMarkdown.convert(htmee.decode(row.at_xpath("td[#{i}]")&.children&.to_s || row.at_xpath("td[#{i}]")&.to_s).strip, unknown_tags: :bypass)
41
- }.join('\n')
42
- end
43
- }
44
- if dt.length > 0
42
+ sections = crawler_table(noko, table_path, columns)
43
+ hash_fussion! sections, fussion_terms
44
+ unless sections.empty?
45
45
  content << '<dl>'
46
- dt.zip(dd).sort.each do |idt,idd|
47
- if idt.length > 0 && !excluded_terms.include?(UnicodeUtils.downcase idt)
48
- content << "<dt>#{idt}</dt>"
49
- content << "<dd>#{markdown.render idd}</dd>"
50
- end
51
- end
46
+ sections.sort.each { |key, value|
47
+ unless key.empty? || value.empty? || excluded_terms.include?(UnicodeUtils.downcase key)
48
+ content << "<dt>#{key}</dt>"
49
+ if value.is_a?(Array) && value.count > 1
50
+ content << '<ul>' << value.map { |item| "<li>#{markdown.render item}</li>"}.join("\n") << '</ul>'
51
+ elsif value.is_a?(Array) && value.count == 1
52
+ content << "<dd>#{markdown.render value.first}</dd>"
53
+ else
54
+ content << "<dd>#{markdown.render value}</dd>"
55
+ end
56
+ end
57
+ }
52
58
  content << "</dl>"
53
59
  end
54
60
  }
55
61
  end
56
62
 
63
+ def crawler_table(noko, table_path, columns)
64
+ Hash.new.tap { |sections|
65
+ htmee = HTMLEntities.new(:expanded)
66
+ noko = noko.xpath(table_path)
67
+ colspans = "td[@colspan=\"#{columns}\"]"
68
+ if noko.xpath(colspans).count == 0
69
+ noko.collect { |row|
70
+ dterm = htmee.decode(row.at_xpath('td[1]')&.content).gsub(/\n|\r|\t/,' ').strip
71
+ unless dterm.empty?
72
+ sections[dterm] ||= Array.new
73
+ sections[dterm] << (2..columns).map { |i|
74
+ ReverseMarkdown.convert(
75
+ htmee.decode(row.at_xpath("td[#{i}]")&.children&.to_s || row.at_xpath("td[#{i}]")&.to_s).strip, unknown_tags: :bypass
76
+ ).gsub(/\r|\t/,' ').strip
77
+ }.join('\n')
78
+ end
79
+ }
80
+ else
81
+ current_section = nil
82
+ noko.collect { |row|
83
+ unless row.xpath(colspans)&.text.strip.empty?
84
+ current_section = htmee.decode(row.xpath("td[@colspan=\"#{columns}\"]").text).gsub(/\n|\r|\t/,' ').strip
85
+ sections[current_section] ||= Array.new
86
+ else
87
+ if current_section
88
+ sections[current_section] << (1..columns).map { |i|
89
+ ReverseMarkdown.convert(
90
+ htmee.decode(row.at_xpath("td[#{i}]")&.children&.to_s.strip || row.at_xpath("td[#{i}]")&.to_s.strip), unknown_tags: :bypass
91
+ ).strip
92
+ }.join(': ')
93
+ end
94
+ end
95
+ }
96
+ end
97
+ }
98
+ end
99
+
100
+ def crawler_table_to_list(noko, table_path, excluded_terms: [])
101
+ htmee = HTMLEntities.new(:expanded)
102
+ markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new)
103
+ String.new.tap { |list|
104
+ list << '<ul>'
105
+ list << Array.new.tap { |lines|
106
+ noko.xpath(table_path).collect { |td|
107
+ line = if td.xpath('table/tr/td').count > 0
108
+ crawler_table(td, 'table/tr', 2)
109
+ else
110
+ ReverseMarkdown.convert(
111
+ td&.children&.to_s.strip, unknown_tags: :bypass
112
+ ).strip
113
+ end
114
+ lines << "<li>#{htmee.decode(markdown.render line)}</li>" unless line.empty? || excluded_terms.include?(line)
115
+ }
116
+ }.join
117
+ list << '</ul>'
118
+ }
119
+ end
120
+
57
121
 
58
122
  end
59
123
  end
@@ -0,0 +1,18 @@
1
+ module Tzispa
2
+ module Helpers
3
+ module HashTrans
4
+
5
+ def hash_fussion!(hash, fussion_keys={})
6
+ hash.keys.each { |key|
7
+ if fussion_keys.has_key?(key)
8
+ hash[fussion_keys[key]] ?
9
+ hash[fussion_keys[key]] += hash[key] :
10
+ hash[fussion_keys[key]] = hash[key]
11
+ hash.delete(key)
12
+ end
13
+ } unless fussion_keys.empty?
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -6,7 +6,7 @@ module Tzispa
6
6
  module Helpers
7
7
  module Mail
8
8
 
9
- def send_smtp_mail(from:, to:, subject:, body:, config:, cc: nil, html: false)
9
+ def send_smtp_mail(from:, to:, subject:, body:, config:, cc: nil, html: false, debug: false)
10
10
  begin
11
11
  smtp_configuration config
12
12
  mail = ::Mail.new
@@ -38,7 +38,7 @@ module Tzispa
38
38
  nil
39
39
  end
40
40
  rescue
41
- nil
41
+ raise if debug
42
42
  end
43
43
  end
44
44
 
@@ -5,6 +5,7 @@ require 'bigdecimal'
5
5
  require 'i18n'
6
6
  require "unicode_utils"
7
7
  require 'redcarpet'
8
+ require 'cgi/util'
8
9
 
9
10
  module Tzispa
10
11
  module Helpers
@@ -45,6 +46,10 @@ module Tzispa
45
46
  end
46
47
  end
47
48
 
49
+ def html_unscape(str)
50
+ CGI::unescapeHTML(str.strip) if str && !str.strip.empty?
51
+ end
52
+
48
53
  def split_to_array(str, separator=';')
49
54
  str.split(separator) if str
50
55
  end
@@ -74,6 +79,17 @@ module Tzispa
74
79
  result
75
80
  end
76
81
 
82
+ def str_to_amount(str, options = {})
83
+ if str && !str.strip.empty?
84
+ separator = options.fetch(:separator, I18n.t('number.currency.format.separator'))
85
+ precision = options.fetch(:precision, I18n.t('number.currency.format.precision'))
86
+ re = Regexp.new "[^\\d\\#{separator}\\-]"
87
+ str = str.gsub(re, '')
88
+ str = str.gsub(separator, '.') if separator != '.'
89
+ BigDecimal.new(str).round(precision).to_s('F')
90
+ end
91
+ end
92
+
77
93
  def amount(number, options = {})
78
94
  if number.nil? && options[:nil_as_dash] != false
79
95
  '–'
@@ -144,6 +160,13 @@ module Tzispa
144
160
  end
145
161
  end
146
162
 
163
+ def starinizer(rating, star_value, max_stars)
164
+ Hash.new.tap { |stars|
165
+ stars[:full] = rating / star_value
166
+ stars[:half] = rating % star_value
167
+ stars[:o] = max_stars - stars[:full] - stars[:half]
168
+ }
169
+ end
147
170
 
148
171
 
149
172
  end
@@ -3,7 +3,7 @@
3
3
  module Tzispa
4
4
  module Helpers
5
5
 
6
- VERSION = '0.1.5'
6
+ VERSION = '0.1.6'
7
7
  NAME = 'Tzispa Helpers'
8
8
  GEM_NAME = 'tzispa_helpers'
9
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tzispa_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Antonio Piñero
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-23 00:00:00.000000000 Z
11
+ date: 2016-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
@@ -105,6 +105,7 @@ files:
105
105
  - README.md
106
106
  - lib/tzispa/helpers.rb
107
107
  - lib/tzispa/helpers/crawler.rb
108
+ - lib/tzispa/helpers/hash_trans.rb
108
109
  - lib/tzispa/helpers/html.rb
109
110
  - lib/tzispa/helpers/mail.rb
110
111
  - lib/tzispa/helpers/mime.rb
@@ -136,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
137
  version: '0'
137
138
  requirements: []
138
139
  rubyforge_project:
139
- rubygems_version: 2.4.8
140
+ rubygems_version: 2.5.1
140
141
  signing_key:
141
142
  specification_version: 4
142
143
  summary: Helpers for Tzispa