lita-onewheel-xkcd 0.0.0 → 1.0.1

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: c35d89afb36af4bc8f816521dc5efc28dd017308
4
- data.tar.gz: 6b4d2430623972cebad8afb4474a97d8c7908534
3
+ metadata.gz: 82ec66801a1371892e26c9b90b94b8fac24b7e3b
4
+ data.tar.gz: 8e30eb41c11a7b54fc7c6dda8f980ec7490a953f
5
5
  SHA512:
6
- metadata.gz: 5d55c341e5dc44673d53c9fefad480017890f535a6eb94216ab3c642393c2b6df14a0c068f4afa952e543100291c90d33ada38638dcc2ef0b03768b8003a1dc7
7
- data.tar.gz: d5a2e0ed7a4eed78e0b3bff00424940ccd149618f6a763226d71ba7a698dc6de6fdf6f619121ea7b54deed0bba113d5a714d40f3aefd0a23450ec994e89e5185
6
+ metadata.gz: a960af3845d62077e2b4307b138cf0ba3ff31d169385ea342569800bef75bfbb7de51f1ab721602761afba16d3c73862347bfcd7cae299edd583ec813db6218a
7
+ data.tar.gz: 7eb6d47cfe39cf52446a2fc1ec990020650620d084dde3ca818975f2305da76769a80d24548cd32c1e61948891227174afeaae9c315db267e3c46956b07abe8b
@@ -12,6 +12,11 @@ module Lita
12
12
  config :db_port, required: true, default: 5432
13
13
  config :alt_delay, default: 9
14
14
 
15
+ route /^xkcdupdate$/,
16
+ :update,
17
+ command: true
18
+ # admin_only
19
+
15
20
  route /^xkcd$/i,
16
21
  :random,
17
22
  command: true,
@@ -37,6 +42,16 @@ module Lita
37
42
  command: true,
38
43
  help: {'xkcd prev' => 'return the next XKCD comic by date.'}
39
44
 
45
+ route(/^xkcd (\d{1,2})[-\/](\d{1,2})[-\/](\d{2,4})$/i,
46
+ :find_by_mdy,
47
+ command: true,
48
+ help: { 'xkcd 1/1/2014' => 'Get an XKCD comic for a m/d/y date.'})
49
+
50
+ route(/^xkcd (\d{2,4})-(\d{1,2})-(\d{1,2})$/i,
51
+ :find_by_ymd,
52
+ command: true,
53
+ help: { 'xkcd 2014-1-1' => 'Get an XKCD comic for a y-m-d date.'})
54
+
40
55
  ##
41
56
  # Search the title for a string, and return the comic.
42
57
  #
@@ -53,6 +68,20 @@ module Lita
53
68
  end
54
69
  end
55
70
 
71
+ ##
72
+ # Search for the date, and return the comic.
73
+ #
74
+ def find_by_date(month, day, year)
75
+ db = init_db
76
+ result = db["
77
+ select id, data->'img' as img, data->'title' as title, data->'alt' as alt
78
+ from comics
79
+ where data->>'month' = ? and data->>'day' = ? and data->>'year' = ? limit 1", month.to_s, day.to_s, year.to_s]
80
+ if row = result[:data]
81
+ Comic.new(row[:id], row[:img], row[:title], row[:alt])
82
+ end
83
+ end
84
+
56
85
  ##
57
86
  # Find by xkcd id.
58
87
  #
@@ -108,6 +137,18 @@ module Lita
108
137
  end
109
138
  end
110
139
 
140
+ def find_by_ymd(response)
141
+ date = Date.civil(response.match_data[1].to_i, response.match_data[2].to_i, response.match_data[3].to_i)
142
+ comic = find_by_date(date.month, date.day, date.year)
143
+ reply_with_comic response, comic
144
+ end
145
+
146
+ def find_by_mdy(response)
147
+ date = Date.civil(response.match_data[3].to_i, response.match_data[1].to_i, response.match_data[2].to_i)
148
+ comic = find_by_date(date.month, date.day, date.year)
149
+ reply_with_comic response, comic
150
+ end
151
+
111
152
  ##
112
153
  # Grab the comic object by xkcd id (which is also db id)
113
154
  #
@@ -124,10 +165,12 @@ module Lita
124
165
  # Helper function to display comic and set timer for alt tag.
125
166
  #
126
167
  def reply_with_comic(response, comic)
127
- set_state comic, response.user
128
- response.reply "XKCD #{comic.id} \"#{comic.title}\" #{comic.image}"
129
- after(config.alt_delay) do |timer|
130
- response.reply comic.alt
168
+ if comic
169
+ set_state comic, response.user
170
+ response.reply "XKCD #{comic.id} \"#{comic.title}\" #{comic.image}"
171
+ after(config.alt_delay) do |timer|
172
+ response.reply comic.alt
173
+ end
131
174
  end
132
175
  end
133
176
 
@@ -138,7 +181,6 @@ module Lita
138
181
  db = init_db
139
182
  state = db[:state]
140
183
  user_state = state.where(:user => user.name)
141
- puts user_state.count
142
184
  if user_state.count > 0
143
185
  log.debug 'Updating state!'
144
186
  user_state.update(:last_comic => comic.id)
@@ -159,7 +201,33 @@ module Lita
159
201
  else
160
202
  log.debug("get_last_comic called with no user state for #{user.name}")
161
203
  end
204
+ end
205
+
206
+ def update(response)
207
+ db = init_db
208
+ # Check tables? run schema?
209
+ max_id = db[:comics].max(:id) || 1 # Get the last id or start with one.
210
+ top_num = get_top_num
211
+ response.reply "Updating from #{max_id} to #{top_num}!"
212
+ perform_update(max_id + 1, top_num)
213
+ end
162
214
 
215
+ def get_top_num
216
+ top_response = RestClient.get 'http://xkcd.com/info.0.json'
217
+ top_json = JSON.parse top_response
218
+ top_num = top_json['num']
219
+ top_json
220
+ end
221
+
222
+ def perform_update(max_id, top_num)
223
+ for num in max_id..top_num do
224
+ if num == 404
225
+ db_comics.insert(data: '{"status":"not found"}')
226
+ next
227
+ end
228
+ response = RestClient.get "http://xkcd.com/#{num}/info.0.json"
229
+ db_comics.insert(data: response)
230
+ end
163
231
  end
164
232
 
165
233
  def init_db
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'lita-onewheel-xkcd'
3
- spec.version = '0.0.0'
3
+ spec.version = '1.0.1'
4
4
  spec.authors = ['Andrew Kreps']
5
5
  spec.email = ['andrew.kreps@gmail.com']
6
6
  spec.description = 'XKCD searchable archive for comics by keyword, id and date*. * date coming soon to a minor update near you'
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
15
  spec.require_paths = ['lib']
16
16
 
17
- spec.add_runtime_dependency 'lita', '~> 4.6'
17
+ spec.add_runtime_dependency 'lita', '~> 4'
18
18
  spec.add_runtime_dependency 'lita-irc', '~> 2.0'
19
19
  spec.add_runtime_dependency 'sequel', '~> 4.27'
20
20
  spec.add_runtime_dependency 'sequel_pg', '~> 1.6'
data/readme.rst CHANGED
@@ -9,6 +9,7 @@ A Lita_ handler to display XKCD comics in your chat handler of choice.
9
9
 
10
10
  Installation
11
11
  ------------
12
+ Get a postgres instance running, give this guy access to write to the two tables in scripts/schema.rb.
12
13
  Add lita-onewheel-xkcd to your Lita instance's Gemfile:
13
14
  ::
14
15
  gem "lita-onewheel-xkcd"
@@ -36,7 +37,7 @@ All commands return the comic, and then display the alt text alt_delay seconds l
36
37
  :xkcd ballmer: returns my favorite Steve Ballmer comic.
37
38
  :xkcd next: returns the next comic by index.
38
39
  :xkcd prev: returns the previous comic by index.
39
-
40
+ :xkcdupdate: Updates the database with the latest comics. Manually, for now.
40
41
 
41
42
  Engineering Notes
42
43
  -----------------
@@ -47,8 +48,6 @@ Comic by number: http://xkcd.com/1/info.0.json
47
48
 
48
49
  How to map # to date? - it's in the meta
49
50
 
50
- Parser script, import into postgres? Best way to handle json
51
-
52
51
  Keyword tokenizer, it's substring searching at the moment.
53
52
 
54
53
  Add postgres setup and details on how to update the database with new comics.
data/scripts/pop.rb CHANGED
@@ -22,4 +22,3 @@ for num in max_id..top_json['num'] do
22
22
  db_comics.insert(data: response)
23
23
  puts "#{num}/#{top_json['num']}"
24
24
  end
25
-
@@ -11,8 +11,13 @@ describe Lita::Handlers::OnewheelXkcd, lita_handler: true do
11
11
  it { is_expected.to route_command('xkcd 1998-5-5') }
12
12
  it { is_expected.to route_command('xkcd prev') }
13
13
  it { is_expected.to route_command('xkcd next') }
14
+ it { is_expected.to route_command('xkcdupdate') }
14
15
 
15
- @img_url = 'http://imgs.xkcd.com/comics/'
16
+ attr_accessor :img_url
17
+
18
+ before do
19
+ @img_url = 'http://imgs.xkcd.com/comics/'
20
+ end
16
21
 
17
22
  it 'will return a random xkcd comic' do
18
23
  send_command 'xkcd random'
@@ -26,10 +31,10 @@ describe Lita::Handlers::OnewheelXkcd, lita_handler: true do
26
31
  expect(replies.last).to include(@img_url)
27
32
  end
28
33
 
29
- it 'will return today\'s xkcd comic' do
30
- send_command 'xkcd today'
31
- expect(replies.last).to include(@img_url)
32
- end
34
+ # it 'will return today\'s xkcd comic' do
35
+ # send_command 'xkcd today'
36
+ # expect(replies.last).to include(@img_url)
37
+ # end
33
38
 
34
39
  it 'will return today\'s xkcd comic' do
35
40
  send_command 'xkcd last'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-onewheel-xkcd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kreps
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-20 00:00:00.000000000 Z
11
+ date: 2016-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.6'
19
+ version: '4'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '4.6'
26
+ version: '4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: lita-irc
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -207,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
207
  version: '0'
208
208
  requirements: []
209
209
  rubyforge_project:
210
- rubygems_version: 2.4.5.1
210
+ rubygems_version: 2.5.1
211
211
  signing_key:
212
212
  specification_version: 4
213
213
  summary: Ever wanted a way to display XKCD comics in your chat client of choice? Look