ruboty-hibari_bento 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 41161f734090362e4247a94e6e2d160a3330a7f0
4
+ data.tar.gz: 69a8956ec8b26ef342d72f962a2b1ce482a09e32
5
+ SHA512:
6
+ metadata.gz: 5b23385105ab7b188babf1e2391007af3178445d8adad470008832faf463f8d744bb68d84c771ec58c627dc61acdd60d2655f7fb2f63b38d81296424d4d0a503
7
+ data.tar.gz: ef1723f11810612afd04e0f9a6bca669d9c923c8eb56b1773872100ee5a890d80bbd024d4e5cfd73b554e475a6fc64aeea09417d56c9743f06210f8ceb63c599
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-hibari_bento.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Masaki Enjo
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,58 @@
1
+ # Ruboty::HibariBento
2
+
3
+ Ruboty handler to get information of Hibari Bento, the bento shop in Ruby City MATSUE.
4
+
5
+ https://www.facebook.com/pages/ひばり弁当/1585734291650119
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ruboty-hibari_bento'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ruboty-hibari_bento
22
+
23
+ ## Usage
24
+
25
+ Fetch new posts from the Hibari Bento Facebook page.
26
+
27
+ ```
28
+ > ruboty bento pull
29
+ > ruboty bento fetch
30
+ > ruboty bento show
31
+ ```
32
+
33
+ You can specify the number of posts.
34
+
35
+ ```
36
+ > ruboty bento pull 1
37
+ > ruboty bento fetch 2
38
+ > ruboty bento show 3
39
+ ```
40
+
41
+ Have a nice lunch!!
42
+
43
+ ## ENV
44
+
45
+ ```
46
+ HIBARI_BENTO_FACEBOOK_ACCESS_TOKEN - Facebook API access token
47
+ REDIS_URL - Redis URL (e.g. redis://:p4ssw0rd@example.com:6379/)
48
+ REDISCLOUD_URL - Redis URL of Redis Cloud
49
+ REDISTOGO_URL - Redis URL of Redis To Go
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( https://github.com/emsk/ruboty-hibari_bento/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
1
+ require 'ruboty/hibari_bento/actions/pull'
2
+
3
+ module Ruboty
4
+ module Handlers
5
+ class HibariBento < Base
6
+ env(:HIBARI_BENTO_FACEBOOK_ACCESS_TOKEN, 'Facebook API access token')
7
+ env(:REDIS_URL, 'Redis URL (e.g. redis://:p4ssw0rd@example.com:6379/)', optional: true)
8
+ env(:REDISCLOUD_URL, 'Redis URL of Redis Cloud', optional: true)
9
+ env(:REDISTOGO_URL, 'Redis URL of Redis To Go', optional: true)
10
+
11
+ on(
12
+ /bento\s+(pull|fetch|show)(\s*|\s+(?<limit>\d+))\z/,
13
+ name: 'pull',
14
+ description: 'Fetch posts from the Hibari Bento Facebook page.'
15
+ )
16
+
17
+ def pull(message)
18
+ Ruboty::HibariBento::Actions::Pull.new(message).call
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,110 @@
1
+ module Ruboty
2
+ module HibariBento
3
+ module Actions
4
+ class Pull < Ruboty::Actions::Base
5
+ FACEBOOK_PAGE_ID = '1585734291650119'.freeze
6
+ FACEBOOK_GRAPH_API_URL = 'https://graph.facebook.com'.freeze
7
+ REDIS_NAMESPACE = 'hibari_bento'.freeze
8
+ REDIS_KEY = 'id'.freeze
9
+
10
+ def call
11
+ message.reply(build_message)
12
+ rescue => e
13
+ backtrace = e.backtrace.join("\n")
14
+ Ruboty.logger.error("Error: #{e.class} - #{e.message}\n#{backtrace}")
15
+ end
16
+
17
+ private
18
+
19
+ def build_message
20
+ messages = []
21
+ posts.each { |post| messages.push(post_message(post)) }
22
+ messages.reject!(&:empty?)
23
+ messages.slice!(limit..-1) if limit_given?
24
+ return 'There are no posts.' if messages.empty?
25
+ return messages.join("\n--------------------\n")
26
+ end
27
+
28
+ def posts
29
+ options = {
30
+ fields: %w(id object_id link message updated_time),
31
+ locale: 'ja_JP'
32
+ }
33
+ return graph.get_connections(FACEBOOK_PAGE_ID, 'posts', options)
34
+ end
35
+
36
+ def graph
37
+ return Koala::Facebook::API.new(access_token)
38
+ end
39
+
40
+ def access_token
41
+ return ENV['HIBARI_BENTO_FACEBOOK_ACCESS_TOKEN']
42
+ end
43
+
44
+ def post_message(post)
45
+ unless limit_given?
46
+ return '' if remember?(post)
47
+ memorize(post)
48
+ end
49
+ return message_content(post)
50
+ end
51
+
52
+ def limit_given?
53
+ return !message[:limit].nil?
54
+ end
55
+
56
+ def remember?(post)
57
+ return all_memorized_posts.include?(post['id'])
58
+ end
59
+
60
+ def all_memorized_posts
61
+ return @all_memorized_posts ||= client.lrange(REDIS_KEY, 0, -1)
62
+ end
63
+
64
+ def client
65
+ return @client ||= Redis::Namespace.new(REDIS_NAMESPACE, redis: redis)
66
+ end
67
+
68
+ def redis
69
+ return Redis.new(url: redis_url)
70
+ end
71
+
72
+ def redis_url
73
+ return ENV['REDIS_URL'] || ENV['REDISCLOUD_URL'] || ENV['REDISTOGO_URL']
74
+ end
75
+
76
+ def memorize(post)
77
+ client.rpush(REDIS_KEY, post['id'])
78
+ end
79
+
80
+ def message_content(post)
81
+ messages = []
82
+ messages.push(post['message'])
83
+ messages.push(picture_url(post['object_id']))
84
+ messages.push(post['link'])
85
+ messages.compact!
86
+ messages.reject!(&:empty?)
87
+
88
+ if messages.any? && updated_time = time_format(post['updated_time'])
89
+ messages.push(updated_time)
90
+ end
91
+
92
+ return messages.join("\n")
93
+ end
94
+
95
+ def picture_url(object_id)
96
+ return '' if object_id.nil?
97
+ return "#{FACEBOOK_GRAPH_API_URL}/#{object_id}/picture"
98
+ end
99
+
100
+ def time_format(time)
101
+ return Time.parse(time).strftime('%Y-%m-%d %H:%M:%S') if time
102
+ end
103
+
104
+ def limit
105
+ return [message[:limit].to_i, 0].max
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module HibariBento
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'koala'
2
+ require 'redis'
3
+ require 'redis-namespace'
4
+ require 'ruboty/hibari_bento/version'
5
+ require 'ruboty/handlers/hibari_bento'
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'ruboty/hibari_bento/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ruboty-hibari_bento'
7
+ spec.version = Ruboty::HibariBento::VERSION
8
+ spec.authors = ['Masaki Enjo']
9
+ spec.email = %w(emsk1987@gmail.com)
10
+ spec.summary = 'Ruboty handler to get information of Hibari Bento, the bento shop in Ruby City MATSUE.'
11
+ spec.homepage = 'http://github.com/emsk/ruboty-hibari_bento'
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 = %w(lib)
18
+
19
+ spec.add_runtime_dependency('ruboty')
20
+ spec.add_runtime_dependency('koala')
21
+ spec.add_runtime_dependency('redis')
22
+ spec.add_runtime_dependency('redis-namespace')
23
+ spec.add_development_dependency('bundler', '~> 1.7')
24
+ spec.add_development_dependency('rake', '~> 10.0')
25
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-hibari_bento
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Masaki Enjo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-13 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: koala
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: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis-namespace
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ description:
98
+ email:
99
+ - emsk1987@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/ruboty/handlers/hibari_bento.rb
110
+ - lib/ruboty/hibari_bento.rb
111
+ - lib/ruboty/hibari_bento/actions/pull.rb
112
+ - lib/ruboty/hibari_bento/version.rb
113
+ - ruboty-hibari_bento.gemspec
114
+ homepage: http://github.com/emsk/ruboty-hibari_bento
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.5
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Ruboty handler to get information of Hibari Bento, the bento shop in Ruby
138
+ City MATSUE.
139
+ test_files: []