fundler 0.2.5 → 0.2.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d126ab4a0ce34305604459b4607513f1d00aa1b
4
- data.tar.gz: ce7f216f98126c8965f12ffd6ec9c288bb8a8915
3
+ metadata.gz: c9e5cc9979b9b7356a8341a8bd13674dece28c55
4
+ data.tar.gz: 0251f3debe730d261e4f47dcf035e3f2f8be4129
5
5
  SHA512:
6
- metadata.gz: 0650721de13bce3bb8dca720a75a3f5d163a615e4743f9b9732cd7269b1ab1a39da17ddf5d9b594a2d3b5adb5f6ad45843c8fb1ed544b7502fb45c6c48b83908
7
- data.tar.gz: e7d6e9708b44704e3b6d2908008962d8094470efa3a4eea02c90a9e287a1ead46b03b7a39da5b4e1c48a2718884437051f5e86c007f673d089429d631c7ba2ff
6
+ metadata.gz: d9f874b856944b3f6d20a16a0d91abc0e49cfec0363f941b5e4cf135c96f9dbb16acde8e693ce403e45c9e05fb2c96544f22d0983b493207e151b1f2d69c5b79
7
+ data.tar.gz: cbbf396c02cc8b21d71ebd26914dcc63e5c6805e60fe3c021a22663428ad478508081efbcda8ebdaca549a6e41d579fb772eaee0ab143b63094bc28a18f9c2d2
@@ -1,6 +1,5 @@
1
1
  # coding: utf-8
2
-
3
- module BookmarksReader
2
+ require 'sqlite3'
4
3
 
5
4
  =begin rdoc
6
5
 
@@ -31,51 +30,134 @@ where moz_historyvisits.place_id = moz_places.id
31
30
  order by moz_historyvisits.visit_date desc;
32
31
  =end
33
32
 
33
+ module BookmarksReader
34
+
35
+ attr_reader :db
36
+
34
37
  MOZILLA_FIREFOX_CONF_DIR = File.expand_path('~') + '/.mozilla/firefox/'
35
- # BOOKMARKS_QUERY = %q{select moz_places.url, moz_bookmarks.title from moz_places, moz_bookmarks where moz_places.id = moz_bookmarks.fk and moz_bookmarks.title != '';}
38
+
39
+ # all bookmarks
36
40
  BOOKMARKS_QUERY = <<-SQL
37
- SELECT DISTINCT
38
- moz_places.url AS url,
39
- moz_bookmarks.title AS title,
40
- moz_items_annos.content AS description
41
-
42
- FROM
43
- moz_places,
44
- moz_bookmarks,
45
- moz_items_annos,
46
- moz_anno_attributes
47
-
48
- WHERE
49
- moz_anno_attributes.name = 'bookmarkProperties/description' AND
50
- moz_items_annos.anno_attribute_id = moz_anno_attributes.id AND
51
- moz_items_annos.item_id = moz_bookmarks.id AND
52
- moz_places.id = moz_bookmarks.fk AND
53
- moz_places.id IN (
54
- SELECT DISTINCT fk
55
- FROM moz_bookmarks
56
- WHERE parent IN (
57
- SELECT moz_bookmarks.id
58
- FROM moz_bookmarks, moz_bookmarks_roots
59
- WHERE moz_bookmarks_roots.root_name = 'tags'
60
- AND moz_bookmarks.parent = moz_bookmarks_roots.folder_id
41
+ SELECT DISTINCT
42
+ moz_places.url AS url,
43
+ moz_bookmarks.title AS title,
44
+ moz_items_annos.content AS description
45
+ FROM
46
+ moz_places,
47
+ moz_bookmarks,
48
+ moz_items_annos,
49
+ moz_anno_attributes
50
+ WHERE
51
+ moz_anno_attributes.name = 'bookmarkProperties/description' AND
52
+ moz_items_annos.anno_attribute_id = moz_anno_attributes.id AND
53
+ moz_items_annos.item_id = moz_bookmarks.id AND
54
+ moz_places.id = moz_bookmarks.fk AND
55
+ moz_places.id IN (
56
+ SELECT DISTINCT fk
57
+ FROM moz_bookmarks
58
+ WHERE parent IN (
59
+ SELECT moz_bookmarks.id
60
+ FROM moz_bookmarks, moz_bookmarks_roots
61
+ WHERE moz_bookmarks_roots.root_name = 'tags'
62
+ AND moz_bookmarks.parent = moz_bookmarks_roots.folder_id
63
+ )
61
64
  )
62
- )
63
-
64
- ORDER BY UPPER(moz_bookmarks.title) ASC
65
+ ORDER BY UPPER(moz_bookmarks.title) ASC
65
66
  SQL
66
67
 
68
+ # tag.id|tag.title
69
+ QUERY_FOR_TAGS = <<-SQL
70
+ SELECT id, title FROM moz_bookmarks WHERE parent = 4;
71
+ SQL
72
+
73
+ # all bookmarks with the "given" tag.id
74
+ QUERY_BY_TAG = <<-SQL
75
+ SELECT moz_places.id, moz_places.url, moz_places.title, moz_bookmarks.parent
76
+ FROM moz_places
77
+ LEFT OUTER JOIN moz_bookmarks
78
+ ON moz_places.id = moz_bookmarks.fk
79
+ WHERE moz_bookmarks.parent = @tag_id;
80
+ SQL
81
+
82
+ # bookmark.id|bookmark.fk|bookmark.title
83
+ # ...
84
+ #=> 1933|1387|SQLite Home Page
85
+ # ...
86
+ QUERY_FOR_SIMPLE_BOOKMARKS = <<-SQL
87
+ SELECT id,fk,title FROM moz_bookmarks WHERE parent = 2;
88
+ SQL
89
+
90
+ # select the link between bookmars and tags
91
+ # fk_id = 1387
92
+ #=> 1934|1387|1144
93
+ QUERY_FOR_LINK = <<-SQL
94
+ SELECT id,fk,parent FROM moz_bookmarks WHERE title is null AND fk = @fk_id;
95
+ SQL
96
+
97
+ # tag.id|tag.title
98
+ # parent = 1144
99
+ #=> 1144|coding
100
+ QUERY_FOR_TAG_BY_ID = <<-SQL
101
+ SELECT id, title FROM moz_bookmarks WHERE parent = 4 AND id = @parent;
102
+ SQL
103
+
104
+ def find_bookmarks_by_tag(tag_id)
105
+ query = QUERY_BY_TAG
106
+ query.gsub(/@tag_id/,tag_id)
107
+ db.execute(query) do |row|
108
+ (result ||= []) << row
109
+ end
110
+ end
111
+
112
+ def get_tags
113
+ tags = {}
114
+ db.execute(QUERY_FOR_TAGS).each do |row|
115
+ tag = row.split('|')
116
+ tags[tag[0]] = tag[1]
117
+ end
118
+ tags
119
+ end
120
+
121
+ def get_tag_by_id(id)
122
+ query = QUERY_FOR_TAG_BY_ID
123
+ query.gsub(/@tag_id/,id)
124
+ db.get_first_row(query)
125
+ end
126
+
127
+ def get_bookmarks_with_tags
128
+ bookmarks = []
129
+ result = {}
130
+ db.execute(QUERY_FOR_SIMPLE_BOOKMARKS) do |bookmark|
131
+ bookmarks << bookmark
132
+ end
133
+ bookmarks.each do |bookmark|
134
+ bookmark_id, bookmark_fk, bookmark_title = bookmark
135
+ if bookmark_fk
136
+ link_query = QUERY_FOR_LINK.sub(/@fk_id/, bookmark_fk.to_s)
137
+ links = db.execute(link_query)
138
+ links.each do |link|
139
+ link_id, link_fk, link_parent = link
140
+ tag_query = QUERY_FOR_TAG_BY_ID.gsub(/@parent/, link_parent.to_s)
141
+ db.execute(tag_query) do |tag|
142
+ tag_id, tag_title = tag
143
+ result[bookmark_id] = "[#{tag_title}] #{bookmark_title}"
144
+ end
145
+ end
146
+ end
147
+ end
148
+ result
149
+ end
150
+
67
151
  # +drop_bookmarks+ browse the home directory of the current user,
68
152
  # locate the +places.sqlite+ db_file generated by Firefox and
69
153
  # retrieve all the bookmarks stored.
70
- def drop_bookmarks(format = :plain)
71
- require 'sqlite3'
154
+ def drop_bookmarks(format = :txt)
72
155
  output = []
73
- if locate_places
74
- db = SQLite3::Database.new(locate_places)
156
+ if db
75
157
  rows = db.execute(BOOKMARKS_QUERY)
76
158
  rows.each_with_index do |row, index|
77
159
  case format
78
- when :plain
160
+ when :txt
79
161
  output << "#{index}: #{row.join(' | ')}"
80
162
  output << '---'
81
163
  when :html
@@ -84,10 +166,12 @@ ORDER BY UPPER(moz_bookmarks.title) ASC
84
166
  puts "TODO"
85
167
  when :json
86
168
  puts "TODO"
169
+ else
170
+ # exit 1
87
171
  end
88
172
  end
89
173
  end
90
- File.open('./bookmarks_dump.txt', 'w') do |file|
174
+ File.open("./bookmarks_dump.#{format.to_s}", 'w') do |file|
91
175
  file.puts output
92
176
  end
93
177
  end
@@ -104,9 +188,32 @@ ORDER BY UPPER(moz_bookmarks.title) ASC
104
188
  end
105
189
  end
106
190
 
191
+ def db
192
+ @db ||= SQLite3::Database.new(locate_places) if locate_places
193
+ end
194
+
107
195
  def dump_places
108
196
  FileUtils.cp(locate_places, pwd)
109
197
  end
110
-
198
+
111
199
  end # BookmarksReader
112
200
 
201
+ __END__
202
+
203
+ select id,fk,title,parent from moz_bookmarks;
204
+ ...
205
+ 1983|2509|The Places database | MDN|2
206
+ 1984|2509||1144
207
+ 1933|1387|SQLite Home Page|2
208
+ 1934|1387||1144
209
+ ...
210
+
211
+ select id,fk,title from moz_bookmarks where parent = 2;
212
+ => 1933|1387|SQLite Home Page
213
+
214
+ select id,fk,parent from moz_bookmarks where title is null and fk = 1387;
215
+ 1934|1387|1144
216
+
217
+ select title from moz_bookmarks where parent = 4 AND id = 1144;
218
+ => coding
219
+
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
2
  class Fundler
3
3
  # The version of the Fundler Utility.
4
- VERSION = "0.2.5"
4
+ VERSION = "0.2.6"
5
5
  end
@@ -1,34 +1,28 @@
1
- require 'fundler'
2
- require 'fundler/fundler_utils'
3
-
4
- RSpec.configure do |config|
5
- config.filter_run_excluding ruby: ->(v) { !RUBY_VERSION.start_with?(v.to_s) }
6
- config.color_enabled = true
7
- config.formatter = 'documentation'
8
- # https://github.com/rspec/rspec-expectations
9
- config.expect_with :rspec do |c|
10
- c.syntax = :expect # disables `should`
11
- end
12
-
13
- config.include(ExitCodeMatchers)
14
- end
1
+ require 'fundler_spec_helper'
15
2
 
16
3
  class TestBookmarksFundler
17
4
  describe Fundler do
18
- let(:fundler) { Fundler.new }
19
- before(:each) do
20
- FileUtils::mkdir 'test'
21
- end
5
+ context 'bookmarks functions' do
6
+ let(:fundler) { Fundler.new }
7
+ before(:each) do
8
+ FileUtils::mkdir 'test'
9
+ end
22
10
 
23
- after(:each) do
24
- FileUtils::rm_rf 'test'
25
- end
26
-
27
- it 'finds bookmarks file' do
28
- FileUtils::cd './test' do
29
- fundler.drop_bookmarks_file(:plain)
30
- expect(fundler.all_files).to include('bookmarks_dump.txt')
11
+ after(:each) do
12
+ FileUtils::rm_rf 'test'
13
+ end
14
+
15
+ it 'finds bookmarks file' do
16
+ FileUtils::cd './test' do
17
+ fundler.drop_bookmarks_file(:txt)
18
+ expect(fundler.all_files).to include('bookmarks_dump.txt')
19
+ end
31
20
  end
32
- end
33
- end #describe
21
+
22
+ it 'get_bookmarks_with_tags' do
23
+ expect { fundler.get_bookmarks_with_tags }.to_not raise_error
24
+ end
25
+
26
+ end # context
27
+ end # describe
34
28
  end # TestBookmarksFundler
data/spec/fundler_spec.rb CHANGED
@@ -1,70 +1,56 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
-
4
- require 'fundler'
5
- require 'fundler/fundler_utils'
6
-
7
- RSpec.configure do |config|
8
- config.filter_run_excluding ruby: ->(v) { !RUBY_VERSION.start_with?(v.to_s) }
9
- config.color_enabled = true
10
- config.formatter = 'documentation'
11
- # https://github.com/rspec/rspec-expectations
12
- config.expect_with :rspec do |c|
13
- c.syntax = :expect # disables `should`
14
- end
15
-
16
- config.include(ExitCodeMatchers)
17
- end
1
+ require 'fundler_spec_helper'
18
2
 
19
3
  class TestFundler
20
4
  describe Fundler do
21
- let(:finder) { Fundler.new }
22
- before(:each) do
23
- FileUtils::mkdir 'test'
24
- File.open('test/example', 'w') do |f|
25
- f.puts '#!/usr/bin/env ruby'
26
- f.puts '# encoding: utf-8'
27
- f.puts 'x = 0'
28
- f.puts 'puts x'
29
- end
30
- File.open('test/example_rb.rb', 'w') do |f|
31
- f.puts 'x = :foo'
32
- f.puts 'puts x'
33
- end
34
- File.open('test/list.txt', 'w') do |f|
35
- f.puts 'grocery list'
36
- f.puts '- chunky bacon'
37
- end
38
- File.open('test/SpAcE cAmEl.txt', 'w') do |f|
39
- f.puts 'foo!'
40
- end
41
- end
42
- after(:each) do
43
- FileUtils::rm_rf 'test'
44
- end
45
-
46
- it 'finds all files' do
47
- FileUtils::cd './test' do
48
- expect(finder.all_files()).to eq(['example', 'example_rb.rb', 'list.txt', 'SpAcE cAmEl.txt'].sort)
49
- end
50
- end
51
- it 'finds a file with no .rb extension but has a shebang line' do
52
- FileUtils::cd './test' do
53
- expect(finder.ruby_files()).to eq(['example', 'example_rb.rb'])
54
- end
55
- end
56
- it 'clean all filenames' do
57
- FileUtils::cd './test' do
58
- expect(finder.clean_all_filenames()).to eq(4)
59
- expect(finder.all_files()).to eq(['example', 'example_rb.rb', 'list.txt', 'space_camel.txt'].sort)
60
- end
61
- end
62
- it 'show the pwd (last dir in this spec)' do
63
- FileUtils::cd './test' do
64
- expect(finder.get_pwd.split('/').last).to eq('test')
5
+ context 'file utils' do
6
+ let(:finder) { Fundler.new }
7
+ before(:each) do
8
+ FileUtils::mkdir 'test'
9
+ File.open('test/example', 'w') do |f|
10
+ f.puts '#!/usr/bin/env ruby'
11
+ f.puts '# encoding: utf-8'
12
+ f.puts 'x = 0'
13
+ f.puts 'puts x'
14
+ end
15
+ File.open('test/example_rb.rb', 'w') do |f|
16
+ f.puts 'x = :foo'
17
+ f.puts 'puts x'
18
+ end
19
+ File.open('test/list.txt', 'w') do |f|
20
+ f.puts 'grocery list'
21
+ f.puts '- chunky bacon'
22
+ end
23
+ File.open('test/SpAcE cAmEl.txt', 'w') do |f|
24
+ f.puts 'foo!'
25
+ end
26
+ end
27
+ after(:each) do
28
+ FileUtils::rm_rf 'test'
29
+ end
30
+
31
+ it 'finds all files' do
32
+ FileUtils::cd './test' do
33
+ expect(finder.all_files()).to eq(['example', 'example_rb.rb', 'list.txt', 'SpAcE cAmEl.txt'].sort)
34
+ end
35
+ end
36
+ it 'finds a file with no .rb extension but has a shebang line' do
37
+ FileUtils::cd './test' do
38
+ expect(finder.ruby_files()).to eq(['example', 'example_rb.rb'])
39
+ end
40
+ end
41
+ it 'clean all filenames' do
42
+ FileUtils::cd './test' do
43
+ expect(finder.clean_all_filenames()).to eq(4)
44
+ expect(finder.all_files()).to eq(['example', 'example_rb.rb', 'list.txt', 'space_camel.txt'].sort)
45
+ end
46
+ end
47
+ it 'show the pwd (last dir in this spec)' do
48
+ FileUtils::cd './test' do
49
+ expect(finder.get_pwd.split('/').last).to eq('test')
50
+ end
65
51
  end
66
- end
67
52
 
53
+ end # context
68
54
  end # describe
69
55
 
70
56
  end #TestFundler
@@ -0,0 +1,20 @@
1
+ # fundler_spec_helper.rb
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'fundler'
7
+ require 'fundler/fundler_utils'
8
+
9
+ RSpec.configure do |config|
10
+ config.filter_run_excluding ruby: ->(v) { !RUBY_VERSION.start_with?(v.to_s) }
11
+ config.color_enabled = true
12
+ config.formatter = 'documentation'
13
+
14
+ # https://github.com/rspec/rspec-expectations
15
+ config.expect_with :rspec do |c|
16
+ c.syntax = :expect # disables `should`
17
+ end
18
+
19
+ config.include(ExitCodeMatchers)
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fundler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - zeroed
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-05 00:00:00.000000000 Z
11
+ date: 2013-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -88,6 +88,7 @@ files:
88
88
  - lib/fundler/version.rb
89
89
  - spec/fundler_bookmarks_spec.rb
90
90
  - spec/fundler_spec.rb
91
+ - spec/fundler_spec_helper.rb
91
92
  homepage: https://github.com/zeroed/fundler
92
93
  licenses:
93
94
  - GPL3
@@ -115,4 +116,5 @@ summary: find rename utility
115
116
  test_files:
116
117
  - spec/fundler_bookmarks_spec.rb
117
118
  - spec/fundler_spec.rb
119
+ - spec/fundler_spec_helper.rb
118
120
  has_rdoc: