dynarex-tags 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/dynarex-tags.rb +47 -31
  2. metadata +2 -2
data/lib/dynarex-tags.rb CHANGED
@@ -7,55 +7,71 @@ require 'fileutils'
7
7
 
8
8
  class DynarexTags
9
9
 
10
- def initialize(tags_path, category_url)
10
+ def initialize(tags_path)
11
11
 
12
12
  FileUtils.mkdir_p tags_path
13
13
  Dir.chdir tags_path
14
- index_filename = File.join(tags_path, 'dynarextags.xml')
14
+ @index_filename = File.join(tags_path, '_dynarextags.xml')
15
+ end
15
16
 
16
- if File.exists? index_filename then
17
- dynarex = Dynarex.new index_filename
18
- else
19
- dynarex = Dynarex.new('tags/tag(keyword,count)')
20
- dynarex.save index_filename, pretty: true
21
- end
17
+ def generate(category_url)
22
18
 
19
+ h = {}
23
20
  category_dynarex = Dynarex.new category_url
24
21
 
25
- a = category_dynarex.to_h.map {|item| item.values}
26
- category_dynarex = nil
22
+ category_dynarex.to_h.each do |item|
23
+
24
+ title, url = item.values
25
+ title.scan(/#(\w+)/).flatten.each {|tag| save_tag(h, tag, title, url)}
26
+ end
27
+
28
+ save_dynarex_index(h)
29
+ end
30
+
31
+ def save_title_tags(title, url)
27
32
 
28
- a.each do |title, url|
33
+ if File.exists? @index_filename then
34
+ dynarex = Dynarex.new @index_filename
35
+ else
36
+ dynarex = Dynarex.new('tags/tag(keyword,count)')
37
+ dynarex.save @index_filename, pretty: true
38
+ end
29
39
 
30
- a2 = title.scan(/#(\w+)/).flatten
40
+ h = dynarex.flat_records.inject({}) do |r,x|
41
+ r.merge({x[:keyword] => x[:count].to_i})
42
+ end
31
43
 
32
- a2.each do |tag|
44
+ title.scan(/#(\w+)/).flatten.each {|tag| save_tag(h,tag, title, url)}
45
+ save_dynarex_index(h)
46
+ end
33
47
 
34
- rec = dynarex.find_by_keyword tag
35
- tagfile = tag + '.xml'
48
+ private
36
49
 
37
- if rec then
50
+ def save_dynarex_index(h)
51
+
52
+ dynarex = Dynarex.new('tags/tag(keyword,count)')
53
+ h.each {|tag,count| dynarex.create keyword: tag, count: count.to_s}
54
+ dynarex.save @index_filename, pretty: true
55
+ end
38
56
 
39
- rec.count = rec.count.succ
40
- dyn = Dynarex.new(tagfile)
41
- dyn.create(url: url, title: title)
42
- dyn.save tagfile, pretty: true
43
- dyn = nil
57
+ def save_tag(h, tag, title, url)
44
58
 
45
- else
59
+ rec = h[tag]
60
+ tagfile = tag + '.xml'
46
61
 
47
- dynarex.create keyword: tag, count: '1'
62
+ if rec then
48
63
 
49
- dyn = Dynarex.new('items/item(title,url)')
50
- dyn.create(url: url, title: title)
51
- dyn.save tagfile, pretty: true
52
- dyn = nil
64
+ h[tag] += 1
65
+ dyn = Dynarex.new(tagfile)
53
66
 
54
- end
67
+ else
55
68
 
56
- end
69
+ h[tag] = 1
70
+ dyn = Dynarex.new('items/item(title,url)')
57
71
  end
58
- dynarex.save index_filename, pretty: true
59
72
 
73
+ dyn.create(url: url, title: title)
74
+ dyn.save tagfile, pretty: true
60
75
  end
61
- end
76
+
77
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: dynarex-tags
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-11-12 00:00:00 Z
13
+ date: 2012-11-22 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: dynarex