lita-irasutoya 0.1.0 → 0.1.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
  SHA256:
3
- metadata.gz: 1b83e5319f31e787e611214bfa1e90609e4f5abf8b4c110458971c1374681b63
4
- data.tar.gz: 07723f59315d5dc60956f3c61fcf42c51954765d6014707118955928e336a9c1
3
+ metadata.gz: 51993bdd206d0c9d3f43477bc3ceac2e8231b56d059168906aead98563bcf3bd
4
+ data.tar.gz: 2e7a1a69c38ff1d8a2e031bde22138965dd3f044d9744c05cc24389c0de2e64c
5
5
  SHA512:
6
- metadata.gz: a83550596ad3cfa7d82d787f5468528c7588f3033afe3d78446aa10fbf45072f189172194928c1ce2587fba7b451166ece2b227497585797cd474990c0075cb8
7
- data.tar.gz: 3f413303c6d6bdf5e8c53d7ad42ab17ac20465f7bbadc61b86f2432bbeae4964cf4e987ccaf67556cb657697e5f41b2c1d8edcf46eeb744c831b5d9b638619b2
6
+ metadata.gz: 61c97519e88a47820622fa6460e77ed4b0e15beaa73ae46d2c2f52804c90ff449e3edef7c404bc621c91ff83364900ded911dffd268604143318b25a6d57b51e
7
+ data.tar.gz: 02043e68d8d29612883581828afdde4519d8f06a6521a8b66d52cbfd06ad3a565e47af01a34767eda84cae40ae430bdeebf9bdc134d3b8865f1d2326de9c6c36
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lita-irasutoya (0.1.0)
4
+ lita-irasutoya (0.1.1)
5
5
  nokogiri
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/lita-irasutoya.svg)](https://badge.fury.io/rb/lita-irasutoya)
2
2
  [![Code Climate](https://codeclimate.com/github/unhappychoice/lita-irasutoya/badges/gpa.svg)](https://codeclimate.com/github/unhappychoice/lita-irasutoya)
3
- [![Dependency Status](https://gemnasium.com/badges/github.com/unhappychoice/lita-irasutoya.svg)](https://gemnasium.com/github.com/unhappychoice/lita-irasutoya)
3
+ [![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/unhappychoice/lita-irasutoya.svg)](https://libraries.io/github/unhappychoice/lita-irasutoya)
4
4
  ![](http://ruby-gem-downloads-badge.herokuapp.com/lita-irasutoya?type=total)
5
+ ![GitHub](https://img.shields.io/github/license/unhappychoice/lita-irasutoya.svg)
5
6
 
6
7
  ## Installation
7
8
 
@@ -0,0 +1,66 @@
1
+ module Lita
2
+ module Handlers
3
+ class Irastoya < Handler
4
+ route(
5
+ /^irasutoya$/i,
6
+ :irasutoya,
7
+ help: { 'irasutoya' => 'いらすとやからランダムに画像を表示します。' }
8
+ )
9
+
10
+ def irasutoya(bot)
11
+ url = get_random_url
12
+ document = get_page_and_parse(url)
13
+ image_url = document.css('.entry').search('img').attribute('src').value
14
+
15
+ case robot.config.robot.adapter
16
+ when :slack
17
+ send_attachement(
18
+ target: bot.room,
19
+ url: url,
20
+ title: document.css('.post').css('.title').search('h2').text,
21
+ body: document.css('.entry').css('.separator')[1].text,
22
+ image_url: image_url.chars.first == '/' ? 'http:' + image_url : image_url
23
+ )
24
+ else
25
+ bot.reply document.css('.post').css('.title').search('h2').text
26
+ bot.reply document.css('.entry').css('.separator')[1].text
27
+ bot.reply image_url
28
+ end
29
+ end
30
+
31
+ private def get_random_url
32
+ luck = Random.rand(22208)
33
+ jsonp = Net::HTTP.get('www.irasutoya.com', "/feeds/posts/summary?start-index=#{luck}&max-results=1&alt=json-in-script")
34
+ JSON.parse(jsonp[/{.+}/])
35
+ .dig('feed', 'entry')
36
+ .first
37
+ .dig('link')
38
+ .filter { |link| link['rel'] == 'alternate' }
39
+ .first
40
+ .dig('href')
41
+ end
42
+
43
+ private def get_page_and_parse(url)
44
+ charset = nil
45
+ html = URI.parse(url).open do |f|
46
+ charset = f.charset
47
+ f.read
48
+ end
49
+ Nokogiri::HTML.parse(html, nil, charset)
50
+ end
51
+
52
+ private def send_attachement(target:, url:, title:, body:, image_url:)
53
+ attachment = Lita::Adapters::Slack::Attachment.new(body, {
54
+ color: 'good',
55
+ title: title,
56
+ title_link: url,
57
+ text: body,
58
+ image_url: image_url
59
+ })
60
+ robot.chat_service.send_attachment(target, attachment)
61
+ end
62
+ end
63
+
64
+ Lita.register_handler(Irastoya)
65
+ end
66
+ end
@@ -1,69 +1,7 @@
1
- require "lita/irasutoya/version"
1
+ require_relative "./handlers/irasutoya"
2
+ require_relative "./irasutoya/version"
2
3
  require 'open-uri'
3
4
 
4
5
  module Lita
5
- module Handlers
6
- class Irastoya < Handler
7
- route(
8
- /^irasutoya$/i,
9
- :irasutoya,
10
- help: { 'irasutoya' => 'いらすとやからランダムに画像を表示します。' }
11
- )
12
-
13
- def irasutoya(bot)
14
- url = get_random_url
15
- document = get_page_and_parse(url)
16
- image_url = document.css('.entry').search('img').attribute('src').value
17
-
18
- case robot.config.robot.adapter
19
- when :slack
20
- send_attachement(
21
- target: bot.room,
22
- url: url,
23
- title: document.css('.post').css('.title').search('h2').text,
24
- body: document.css('.entry').css('.separator')[1].text,
25
- image_url: image_url.chars.first == '/' ? 'http:' + image_url : image_url
26
- )
27
- else
28
- bot.reply document.css('.post').css('.title').search('h2').text
29
- bot.reply document.css('.entry').css('.separator')[1].text
30
- bot.reply image_url
31
- end
32
- end
33
-
34
- private def get_random_url
35
- luck = Random.rand(22208)
36
- jsonp = Net::HTTP.get('www.irasutoya.com', "/feeds/posts/summary?start-index=#{luck}&max-results=1&alt=json-in-script")
37
- JSON.parse(jsonp[/{.+}/])
38
- .dig('feed', 'entry')
39
- .first
40
- .dig('link')
41
- .filter { |link| link['rel'] == 'alternate' }
42
- .first
43
- .dig('href')
44
- end
45
-
46
- private def get_page_and_parse(url)
47
- charset = nil
48
- html = URI.parse(url).open do |f|
49
- charset = f.charset
50
- f.read
51
- end
52
- Nokogiri::HTML.parse(html, nil, charset)
53
- end
54
-
55
- private def send_attachement(target:, url:, title:, body:, image_url:)
56
- attachment = Lita::Adapters::Slack::Attachment.new(body, {
57
- color: 'good',
58
- title: title,
59
- title_link: url,
60
- text: body,
61
- image_url: image_url
62
- })
63
- robot.chat_service.send_attachment(target, attachment)
64
- end
65
- end
66
-
67
- Lita.register_handler(Lita::Handlers::Irastoya)
68
- end
6
+ module Irasutoya; end
69
7
  end
@@ -1,5 +1,5 @@
1
1
  module Lita
2
2
  module Irasutoya
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-irasutoya
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuji Ueki
@@ -82,6 +82,7 @@ files:
82
82
  - README.md
83
83
  - Rakefile
84
84
  - images/usage.png
85
+ - lib/lita/handlers/irasutoya.rb
85
86
  - lib/lita/irasutoya.rb
86
87
  - lib/lita/irasutoya/version.rb
87
88
  - lita-irasutoya.gemspec