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.
Files changed (2) hide show
  1. data/lib/stackoverflow.rb +45 -16
  2. 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
- @db_idx.execute(sql) do |row|
74
- questions_ids << row[0]
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
- @db.execute(sql) do |row|
88
- questions << { :id => row[0],
89
- :score => row[1],
90
- :body => row[2],
91
- :title => row[3],
92
- :link => '',
93
- :answers => get_answers(row[0]) }
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
- @db_idx.execute(sql) do |row|
102
- answers_ids << row[0]
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
- @db.execute(sql) do |row|
116
- answers << { :id => row[0],
117
- :score => row[1],
118
- :body => row[2] }
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
- score = question['score'] > 0 ? "+#{question['score']}" : question['score']
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
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Xavier Antoviaque