stackoverflow 0.1.2 → 0.1.3
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.
- data/lib/stackoverflow.rb +45 -16
- metadata +2 -2
data/lib/stackoverflow.rb
CHANGED
@@ -60,6 +60,19 @@ module StackOverflow
|
|
60
60
|
@db_idx = SQLite3::Database.new(File.join(Dir.home, ".stackoverflow/stackoverflow_idx.db"))
|
61
61
|
end
|
62
62
|
|
63
|
+
def db_error_catching
|
64
|
+
begin
|
65
|
+
yield
|
66
|
+
rescue SQLite3::SQLException => e
|
67
|
+
puts "******************"
|
68
|
+
puts "** DATABASE ERROR. Did you run 'so --update'? (If you did, remove the ~/.stackoverflow directory and run 'so --update' again)"
|
69
|
+
puts "******************\n\n"
|
70
|
+
puts 'Details of the error:'
|
71
|
+
puts e.message
|
72
|
+
puts e.backtrace
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
63
76
|
def search(search_string)
|
64
77
|
# Search on titles in the small index DB, to get the ids faster
|
65
78
|
sql = "SELECT id FROM questions WHERE "
|
@@ -70,9 +83,12 @@ module StackOverflow
|
|
70
83
|
sql += sub_sql.join(' AND ')
|
71
84
|
|
72
85
|
questions_ids = []
|
73
|
-
|
74
|
-
|
86
|
+
db_error_catching do
|
87
|
+
@db_idx.execute(sql) do |row|
|
88
|
+
questions_ids << row[0]
|
89
|
+
end
|
75
90
|
end
|
91
|
+
return [] if questions_ids.length < 1
|
76
92
|
|
77
93
|
# Then retreive details from the main (large) DB
|
78
94
|
sql = "SELECT id, score, body, title FROM posts WHERE "
|
@@ -84,23 +100,29 @@ module StackOverflow
|
|
84
100
|
sql += ' ORDER BY score DESC'
|
85
101
|
|
86
102
|
questions = []
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
103
|
+
db_error_catching do
|
104
|
+
@db.execute(sql) do |row|
|
105
|
+
questions << { :id => row[0],
|
106
|
+
:score => row[1],
|
107
|
+
:body => row[2],
|
108
|
+
:title => row[3],
|
109
|
+
:link => '',
|
110
|
+
:answers => get_answers(row[0]) }
|
111
|
+
end
|
94
112
|
end
|
113
|
+
questions
|
95
114
|
end
|
96
115
|
|
97
116
|
def get_answers(question_id)
|
98
117
|
# Search on parent ids in the small index DB, to get the ids faster
|
99
118
|
sql = "SELECT id FROM answers WHERE parent_id=#{question_id}"
|
100
119
|
answers_ids = []
|
101
|
-
|
102
|
-
|
120
|
+
db_error_catching do
|
121
|
+
@db_idx.execute(sql) do |row|
|
122
|
+
answers_ids << row[0]
|
123
|
+
end
|
103
124
|
end
|
125
|
+
return [] if answers_ids.length < 1
|
104
126
|
|
105
127
|
# Then retreive details from the main (large) DB
|
106
128
|
sql = "SELECT id, score, body FROM posts WHERE "
|
@@ -112,11 +134,14 @@ module StackOverflow
|
|
112
134
|
sql += ' ORDER BY score DESC'
|
113
135
|
|
114
136
|
answers = []
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
137
|
+
db_error_catching do
|
138
|
+
@db.execute(sql) do |row|
|
139
|
+
answers << { :id => row[0],
|
140
|
+
:score => row[1],
|
141
|
+
:body => row[2] }
|
142
|
+
end
|
119
143
|
end
|
144
|
+
answers
|
120
145
|
end
|
121
146
|
end
|
122
147
|
|
@@ -224,7 +249,11 @@ module StackOverflow
|
|
224
249
|
|
225
250
|
table = Terminal::Table.new do |t|
|
226
251
|
questions.each do |question|
|
227
|
-
|
252
|
+
if question['score']
|
253
|
+
score = question['score'] > 0 ? "+#{question['score']}" : question['score']
|
254
|
+
else
|
255
|
+
score = 0
|
256
|
+
end
|
228
257
|
t << ["[#{nb}]", "(#{score})", question['title']]
|
229
258
|
nb += 1
|
230
259
|
end
|