ruboty-twitter_search 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 08bcd7c975c8a611a2cc3295b424eba738090cc9
4
+ data.tar.gz: 01ca4f6850eb64711efda4c839500f0f1c14ebeb
5
+ SHA512:
6
+ metadata.gz: 02374395b9941221bb4c1a230225b2d8fd35a77836be401dde6214d67c81849d33fc0b65427d39fbb4e88e68106afbed43d5b9afe07163a4532250b1d0763898
7
+ data.tar.gz: b5715a0db4589e078209078356c2e534e420e3f9e60bf6076933e6860b6b8077fc1eccbdeace09571473f183f4c7aaaa5745075dbe15a1d560107de58b7c57c3
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ /.env
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-twitter_search.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ryo Nakamura
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # Ruboty::TwitterSearch
2
+ [Ruboty](https://github.com/r7kamura/ruboty-twitter_search) plug-in to search twitter.
3
+
4
+ ## Usage
5
+ ```
6
+ @ruboty search twitter by <query>
7
+ ```
8
+
9
+ ## ENV
10
+ ```
11
+ TWITTER_DISABLE_SINCE_ID - Pass 1 to disable using since_id parameter (optional)
12
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,84 @@
1
+ require "ruboty"
2
+ require "twitter"
3
+ require "yaml"
4
+
5
+ module Ruboty
6
+ module Handlers
7
+ class TwitterSearch < Base
8
+ NAMESPACE = "twitter-search"
9
+
10
+ env :TWITTER_ACCESS_TOKEN, "Twitter access token"
11
+ env :TWITTER_ACCESS_TOKEN_SECRET, "Twitter access token secret"
12
+ env :TWITTER_CONSUMER_KEY, "Twitter consumer key (a.k.a. API key)"
13
+ env :TWITTER_CONSUMER_SECRET, "Twitter consumer secret (a.k.a. API secret)"
14
+ env :TWITTER_DISABLE_SINCE_ID, "Pass 1 to disable using since_id parameter", optional: true
15
+
16
+ on(
17
+ /search twitter by (?<query>.+)/,
18
+ description: "Search twitter by given query",
19
+ name: :search,
20
+ )
21
+
22
+ # @todo
23
+ def search(message)
24
+ statuses = client.search(message[:query], since_id: fetch_since_id_for(message[:query])).take(10)
25
+ if statuses.any?
26
+ message.reply(StatusesView.new(statuses).to_s)
27
+ store_since_id(query: message[:query], since_id: statuses.first.id)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def client
34
+ @client ||= ::Twitter::REST::Client.new do |config|
35
+ config.consumer_key = ENV["TWITTER_CONSUMER_KEY"]
36
+ config.consumer_secret = ENV["TWITTER_CONSUMER_SECRET"]
37
+ config.access_token = ENV["TWITTER_ACCESS_TOKEN"]
38
+ config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
39
+ end
40
+ end
41
+
42
+ def disabled_to_use_since_id?
43
+ ENV["TWITTER_DISABLE_SINCE_ID"] == "1"
44
+ end
45
+
46
+ # @param query [String] Query string to be passed to Twitter API
47
+ # @return [Integer, nil] since_id or nil
48
+ def fetch_since_id_for(query)
49
+ unless disabled_to_use_since_id?
50
+ store[query]
51
+ end
52
+ end
53
+
54
+ # @note To remember since_id for each query.
55
+ def store
56
+ robot.brain.data[NAMESPACE] ||= {}
57
+ end
58
+
59
+ def store_since_id(query: nil, since_id: nil)
60
+ unless disabled_to_use_since_id?
61
+ store[query] = since_id
62
+ end
63
+ end
64
+
65
+ class StatusesView
66
+ def initialize(statuses)
67
+ @statuses = statuses
68
+ end
69
+
70
+ def to_s
71
+ source_urls.reverse.join("\n")
72
+ end
73
+
74
+ private
75
+
76
+ def source_urls
77
+ @statuses.map do |status|
78
+ "https://twitter.com/#{status.user.screen_name}/status/#{status.id}"
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module TwitterSearch
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "ruboty/handlers/twitter_search"
2
+ require "ruboty/twitter_search/version"
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "ruboty/twitter_search/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ruboty-twitter_search"
7
+ spec.version = Ruboty::TwitterSearch::VERSION
8
+ spec.authors = ["Ryo Nakamura"]
9
+ spec.email = ["r7kamura@gmail.com"]
10
+ spec.summary = "Ruboty plug-in to search twitter."
11
+ spec.homepage = "https://github.com/r7kamura/ruboty-twitter_search"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "ruboty"
20
+ spec.add_dependency "twitter"
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-twitter_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruboty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: twitter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - r7kamura@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/ruboty/handlers/twitter_search.rb
82
+ - lib/ruboty/twitter_search.rb
83
+ - lib/ruboty/twitter_search/version.rb
84
+ - ruboty-twitter_search.gemspec
85
+ homepage: https://github.com/r7kamura/ruboty-twitter_search
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.4.5
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Ruboty plug-in to search twitter.
109
+ test_files: []
110
+ has_rdoc: