moobooks 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/bin/plushies +79 -44
- data/lib/moobooks.rb +1 -3
- data/lib/moobooks/plush.rb +106 -0
- data/lib/moobooks/version.rb +4 -1
- data/spec/{database_spec.rb → plush_spec.rb} +25 -7
- metadata +20 -10
- data/lib/moobooks/database.rb +0 -43
- data/lib/moobooks/twitter.rb +0 -22
- data/lib/moobooks/twitter/app.rb +0 -102
- data/spec/twitter_app_spec.rb +0 -60
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a015cf1b4143a7fca8c88b5fe96603fb35965282
|
4
|
+
data.tar.gz: 3a908d3527ede6484d570ad790a8f352d768fd1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56e081fee01e291ba1f13b8d0ff74db005f80bc2a07811d2d6e6d41a74bf3a22911edf5bb3b265d74cbd12904edd501a130bba09f80b9beaee57a22cc3d39579
|
7
|
+
data.tar.gz: 057f27e94db5afdc545ccf9a21799eb823560e3fa96fe1be3fd1f47dbc9c6caa90091346fc7f2f799961037deee9d039ea95b52b837cb8ecdbdd21d64b24721e
|
data/bin/plushies
CHANGED
@@ -20,56 +20,91 @@
|
|
20
20
|
|
21
21
|
require_relative '../lib/moobooks'
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
require 'logger'
|
24
|
+
|
25
|
+
def print_help(help_param = false)
|
26
|
+
puts 'Usage:',
|
27
|
+
' plushies post',
|
28
|
+
' plushies update'
|
29
|
+
exit(help_param ? 0 : 1)
|
30
|
+
end
|
31
|
+
|
32
|
+
app_config = File.expand_path("#{__dir__}/../config/apps.json")
|
33
|
+
unless File.exist?(app_config)
|
34
|
+
warn 'No app configuration present!'
|
35
|
+
exit 1
|
27
36
|
end
|
28
37
|
|
29
|
-
|
38
|
+
log = Logger.new(File.expand_path("#{__dir__}/../log/plushies.log"), 'monthly')
|
39
|
+
log.level = Logger::INFO
|
40
|
+
log.formatter = proc do |sev, date, _prog, msg|
|
41
|
+
"#{date.strftime('%b %e, %Y %e %r')} [#{sev[0, 1].upcase}]: #{msg}\n"
|
42
|
+
end
|
43
|
+
|
44
|
+
apps = JSON.parse(File.read(app_config), symbolize_names: true)
|
45
|
+
|
46
|
+
plush_config = File.expand_path("#{__dir__}/../config/plushies.json")
|
47
|
+
unless File.exist?(plush_config)
|
48
|
+
warn 'No plush configuration present!'
|
49
|
+
exit 1
|
50
|
+
end
|
51
|
+
plush_configs = JSON.parse(File.read(plush_config), symbolize_names: true)
|
52
|
+
|
53
|
+
cmd = ARGV.shift
|
54
|
+
|
55
|
+
plushies = ARGV.map do |name|
|
56
|
+
conf = plush_configs[name.to_sym]
|
57
|
+
if conf.nil?
|
58
|
+
warn "Plush \"#{name}\" not known"
|
59
|
+
log.warn "Plush \"#{name}\" not known"
|
60
|
+
next
|
61
|
+
end
|
62
|
+
Moobooks::Plush.new(name, conf, apps)
|
63
|
+
end.compact
|
64
|
+
|
65
|
+
case cmd
|
30
66
|
when ['-h']
|
31
|
-
print_help
|
32
|
-
when
|
33
|
-
|
34
|
-
|
35
|
-
warn 'Name can\'t be empty.' if name.nil?
|
36
|
-
case platform
|
37
|
-
when 'twitter'
|
38
|
-
puts 'Please check your app\'s developer page to get it\'s data.'
|
39
|
-
print 'Consumer key: '
|
40
|
-
consumer_key = $stdin.gets.chomp
|
41
|
-
print 'Consumer secret: '
|
42
|
-
consumer_secret = $stdin.gets.chomp
|
43
|
-
Moobooks::Twitter::App.create(name, consumer_key, consumer_secret)
|
44
|
-
when 'mastodon'
|
45
|
-
Moobooks::Mastodon::App.create(name)
|
46
|
-
else
|
47
|
-
warn "Unrecognized platform: #{platform}"
|
48
|
-
print_add_help
|
49
|
-
exit 1
|
67
|
+
print_help(true)
|
68
|
+
when 'post'
|
69
|
+
plushies.each do |plush|
|
70
|
+
log.info "Plush \"#{plush.name}\" said: \"#{plush.post_status!}\""
|
50
71
|
end
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
72
|
+
when 'update'
|
73
|
+
plushies.each do |plush|
|
74
|
+
corpus = JSON.parse(File.read("#{__dir__}/../corpora/#{plush.name}.json"),
|
75
|
+
symbolize_names: true).map do |t|
|
76
|
+
Twitter::Tweet.new(t)
|
77
|
+
end
|
78
|
+
max_id = 2**61
|
79
|
+
new_tweets = []
|
80
|
+
16.times do
|
81
|
+
new_tweets += plush.clients[:twitter].user_timeline(
|
82
|
+
plush.originals[:twitter], max_id: max_id, count: 200,
|
83
|
+
tweet_mode: 'extended'
|
84
|
+
)
|
85
|
+
log.info("Added #{new_tweets.count} tweets from "\
|
86
|
+
"\"#{plush.originals[:twitter]}\" to \"#{plush.name}\"")
|
87
|
+
max_id = new_tweets.min_by(&:id).id
|
88
|
+
break if max_id <= corpus.max_by(&:id).id
|
89
|
+
end
|
90
|
+
corpus = (corpus + new_tweets).reject(&:retweeted?).sort_by(&:id)
|
91
|
+
.map(&:attrs).uniq.reject do |t|
|
92
|
+
t[:full_text].match(/^(RT|MT)/)
|
93
|
+
end.to_json
|
94
|
+
File.write("#{__dir__}/../corpora/#{plush.name}.json", corpus)
|
95
|
+
corpus = JSON.parse(corpus, symbolize_names: true).map do |t|
|
96
|
+
t[:full_text]
|
97
|
+
end
|
98
|
+
corpus = {
|
99
|
+
statuses: corpus.reject { |t| t.match(/@\w+/) },
|
100
|
+
mentions: corpus.select { |t| t.match(/@\w+/) }
|
101
|
+
}
|
102
|
+
model = Ebooks::Model.new.consume(corpus).to_json
|
103
|
+
File.write("#{__dir__}/../models/#{plush.name}.json", model)
|
104
|
+
log.info("Updated model for \"#{plush.name}\" ")
|
64
105
|
end
|
65
|
-
# when 'update'
|
66
|
-
# when 'post'
|
67
|
-
# when 'profile'
|
68
|
-
# when 'follow'
|
69
|
-
# when 'favorite'
|
70
|
-
# when 'read'
|
71
106
|
else
|
72
107
|
warn "Unknown command: #{cmd.join(' ')}"
|
108
|
+
log.warn "Unknown command: #{cmd.join(' ')}"
|
73
109
|
print_help
|
74
|
-
exit 1
|
75
110
|
end
|
data/lib/moobooks.rb
CHANGED
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2018 Maxine Michalski <maxine@furfind.net>
|
4
|
+
#
|
5
|
+
# This file is part of MooBooks.
|
6
|
+
#
|
7
|
+
# MooBooks is free software: you can redistribute it and/or modify
|
8
|
+
# it under the terms of the GNU General Public License as published by
|
9
|
+
# the Free Software Foundation, either version 3 of the License, or
|
10
|
+
# (at your option) any later version.
|
11
|
+
#
|
12
|
+
# MooBooks is distributed in the hope that it will be useful,
|
13
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
# GNU General Public License for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU General Public License
|
18
|
+
# along with MooBooks. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
require 'moo_ebooks'
|
21
|
+
require 'twitter'
|
22
|
+
|
23
|
+
module Moobooks
|
24
|
+
class UnsupportedEngineError < StandardError; end
|
25
|
+
# @author Maxine Michalski
|
26
|
+
# @since 0.1.0
|
27
|
+
#
|
28
|
+
# Plushies!
|
29
|
+
class Plush
|
30
|
+
# @return [String] Name of current toy
|
31
|
+
attr_reader :name
|
32
|
+
|
33
|
+
# @return [String] Engine this plush is running on
|
34
|
+
attr_reader :engine
|
35
|
+
|
36
|
+
# @return [Ebooks::Model] Model used by this plush
|
37
|
+
attr_reader :model
|
38
|
+
|
39
|
+
# @return [Hash] Names of original accounts
|
40
|
+
attr_reader :originals
|
41
|
+
|
42
|
+
# @author Maxine Michalski
|
43
|
+
#
|
44
|
+
# Initializer for Plushies
|
45
|
+
#
|
46
|
+
# @param name [String] Name of new plush
|
47
|
+
# @param conf [Hash] Configuration hash
|
48
|
+
# @param apps [Hash] Hash that contains available app configurations
|
49
|
+
#
|
50
|
+
# @notice Engine can be `rng` or `markov` only.
|
51
|
+
def initialize(name, conf, apps)
|
52
|
+
raise ArgumentError, 'Name can\'t be empty~' if name.nil? || name.empty?
|
53
|
+
@name = name
|
54
|
+
@engine = conf[:engine]
|
55
|
+
raise UnsupportedEngineError unless %w[rng markov].include?(@engine)
|
56
|
+
@originals = {}
|
57
|
+
twitter_login(apps, conf[:twitter]) unless conf[:twitter].nil?
|
58
|
+
@model = Ebooks::Model.from_json(
|
59
|
+
File.read("#{__dir__}/../../models/#{@name}.json")
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
# @author Maxine Michalski
|
64
|
+
#
|
65
|
+
# Method to post statuses to all available social platforms
|
66
|
+
def post_status!
|
67
|
+
@status = @model.update(280)
|
68
|
+
# @twitter_client.update(@status) unless @twitter_client.nil?
|
69
|
+
@status
|
70
|
+
end
|
71
|
+
|
72
|
+
# @author Maxine Michalski
|
73
|
+
#
|
74
|
+
# Let's this plush reply to messages.
|
75
|
+
#
|
76
|
+
# @notice This can cause endless reply loops, use with caution!
|
77
|
+
def reply!(status)
|
78
|
+
@status = @model.reply(status.text, 280)
|
79
|
+
@status
|
80
|
+
end
|
81
|
+
|
82
|
+
# @author Maxine Michalski
|
83
|
+
#
|
84
|
+
# Returns a hash of all connected clients
|
85
|
+
#
|
86
|
+
# @return [Hash] Hash of different, connected, clients
|
87
|
+
def clients
|
88
|
+
clients = {}
|
89
|
+
clients.store(:twitter, @twitter_client) unless @twitter_client.nil?
|
90
|
+
clients
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def twitter_login(apps, conf)
|
96
|
+
app = apps[conf[:api].to_sym]
|
97
|
+
@twitter_client = Twitter::REST::Client.new do |config|
|
98
|
+
config.consumer_key = app[:consumer_key]
|
99
|
+
config.consumer_secret = app[:consumer_secret]
|
100
|
+
config.access_token = conf[:access_token]
|
101
|
+
config.access_token_secret = conf[:access_token_secret]
|
102
|
+
end
|
103
|
+
@originals.store(:twitter, conf[:original])
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
data/lib/moobooks/version.rb
CHANGED
@@ -22,8 +22,11 @@
|
|
22
22
|
# @since 0.1.0
|
23
23
|
module Moobooks
|
24
24
|
MAJOR = 0
|
25
|
-
MINOR =
|
25
|
+
MINOR = 2
|
26
26
|
PATCH = 0
|
27
27
|
NAME = 'moobooks'
|
28
28
|
VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}"
|
29
|
+
DESCRIPTION = 'A framework and CLI to create and manage ebook '\
|
30
|
+
"accounts.\nKeep in mind that actual accounts need to "\
|
31
|
+
'be created manually, but can be linked with this tool.'
|
29
32
|
end
|
@@ -17,12 +17,30 @@
|
|
17
17
|
# You should have received a copy of the GNU General Public License
|
18
18
|
# along with MooBooks. If not, see <http://www.gnu.org/licenses/>.
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
20
|
+
conf = {
|
21
|
+
maxine: {
|
22
|
+
engine: 'markov',
|
23
|
+
twitter: nil,
|
24
|
+
mastodon: nil
|
25
|
+
}
|
26
|
+
}
|
27
|
+
apps = {
|
28
|
+
general: {
|
29
|
+
consumer_key: '',
|
30
|
+
consumer_secret: ''
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
app = Moobooks::Plush.new('maxine', conf[:maxine], apps)
|
35
|
+
|
36
|
+
describe Moobooks::Plush, '#name' do
|
37
|
+
it 'returns name of plush' do
|
38
|
+
expect(app.name).to be_a String
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe Moobooks::Plush, '#engine' do
|
43
|
+
it 'returns engine of plush' do
|
44
|
+
expect(app.engine).to be_a String
|
27
45
|
end
|
28
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moobooks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maxine Michalski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '1.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: oauth
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.5'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.5'
|
111
125
|
- !ruby/object:Gem::Dependency
|
112
126
|
name: pg
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -147,13 +161,10 @@ extra_rdoc_files: []
|
|
147
161
|
files:
|
148
162
|
- bin/plushies
|
149
163
|
- lib/moobooks.rb
|
150
|
-
- lib/moobooks/
|
151
|
-
- lib/moobooks/twitter.rb
|
152
|
-
- lib/moobooks/twitter/app.rb
|
164
|
+
- lib/moobooks/plush.rb
|
153
165
|
- lib/moobooks/version.rb
|
154
|
-
- spec/
|
166
|
+
- spec/plush_spec.rb
|
155
167
|
- spec/spec_helper.rb
|
156
|
-
- spec/twitter_app_spec.rb
|
157
168
|
homepage: https://github.com/maxine-red/moobooks
|
158
169
|
licenses:
|
159
170
|
- GPL-3.0
|
@@ -174,11 +185,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
185
|
version: '0'
|
175
186
|
requirements: []
|
176
187
|
rubyforge_project:
|
177
|
-
rubygems_version: 2.
|
188
|
+
rubygems_version: 2.5.2.1
|
178
189
|
signing_key:
|
179
190
|
specification_version: 4
|
180
191
|
summary: Mooing bots for your friends.
|
181
192
|
test_files:
|
182
|
-
- spec/database_spec.rb
|
183
193
|
- spec/spec_helper.rb
|
184
|
-
- spec/
|
194
|
+
- spec/plush_spec.rb
|
data/lib/moobooks/database.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright 2018 Maxine Michalski <maxine@furfind.net>
|
4
|
-
#
|
5
|
-
# This file is part of MooBooks.
|
6
|
-
#
|
7
|
-
# MooBooks is free software: you can redistribute it and/or modify
|
8
|
-
# it under the terms of the GNU General Public License as published by
|
9
|
-
# the Free Software Foundation, either version 3 of the License, or
|
10
|
-
# (at your option) any later version.
|
11
|
-
#
|
12
|
-
# MooBooks is distributed in the hope that it will be useful,
|
13
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
-
# GNU General Public License for more details.
|
16
|
-
#
|
17
|
-
# You should have received a copy of the GNU General Public License
|
18
|
-
# along with MooBooks. If not, see <http://www.gnu.org/licenses/>.
|
19
|
-
|
20
|
-
require 'pg'
|
21
|
-
|
22
|
-
module Moobooks
|
23
|
-
# @author Maxine Michalski
|
24
|
-
# @since 0.1.0
|
25
|
-
#
|
26
|
-
# A general purpose database connection module.
|
27
|
-
#
|
28
|
-
# @return Result of the given block
|
29
|
-
module Database
|
30
|
-
# @author Maxine Michalski
|
31
|
-
#
|
32
|
-
# Spawns a database connection and yields it
|
33
|
-
#
|
34
|
-
# @return object Result of the database block
|
35
|
-
def self.connect
|
36
|
-
pg = PG::Connection.new(dbname: 'moobooks')
|
37
|
-
pg.type_map_for_results = PG::BasicTypeMapForResults.new(pg)
|
38
|
-
result = yield pg
|
39
|
-
pg.close
|
40
|
-
result
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
data/lib/moobooks/twitter.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright 2018 Maxine Michalski <maxine@furfind.net>
|
4
|
-
#
|
5
|
-
# This file is part of MooBooks.
|
6
|
-
#
|
7
|
-
# MooBooks is free software: you can redistribute it and/or modify
|
8
|
-
# it under the terms of the GNU General Public License as published by
|
9
|
-
# the Free Software Foundation, either version 3 of the License, or
|
10
|
-
# (at your option) any later version.
|
11
|
-
#
|
12
|
-
# MooBooks is distributed in the hope that it will be useful,
|
13
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
-
# GNU General Public License for more details.
|
16
|
-
#
|
17
|
-
# You should have received a copy of the GNU General Public License
|
18
|
-
# along with MooBooks. If not, see <http://www.gnu.org/licenses/>.
|
19
|
-
|
20
|
-
require 'twitter'
|
21
|
-
|
22
|
-
require_relative 'twitter/app'
|
data/lib/moobooks/twitter/app.rb
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright 2018 Maxine Michalski <maxine@furfind.net>
|
4
|
-
#
|
5
|
-
# This file is part of MooBooks.
|
6
|
-
#
|
7
|
-
# MooBooks is free software: you can redistribute it and/or modify
|
8
|
-
# it under the terms of the GNU General Public License as published by
|
9
|
-
# the Free Software Foundation, either version 3 of the License, or
|
10
|
-
# (at your option) any later version.
|
11
|
-
#
|
12
|
-
# MooBooks is distributed in the hope that it will be useful,
|
13
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
-
# GNU General Public License for more details.
|
16
|
-
#
|
17
|
-
# You should have received a copy of the GNU General Public License
|
18
|
-
# along with MooBooks. If not, see <http://www.gnu.org/licenses/>.
|
19
|
-
|
20
|
-
module Moobooks
|
21
|
-
module Twitter
|
22
|
-
# @author Maxine Michalski
|
23
|
-
# @since 0.1.0
|
24
|
-
class AppNotFound < StandardError; end
|
25
|
-
# @author Maxine Michalski
|
26
|
-
# @since 0.1.0
|
27
|
-
#
|
28
|
-
# A class to handle Twitter apps.
|
29
|
-
class App
|
30
|
-
# @return [Integer] ID of this app
|
31
|
-
attr_reader :id
|
32
|
-
|
33
|
-
# @return [String] Name of this app
|
34
|
-
attr_reader :name
|
35
|
-
|
36
|
-
# @return [String] Consumer key of this app
|
37
|
-
attr_reader :consumer_key
|
38
|
-
|
39
|
-
# @return [String] Consumer secret of this app
|
40
|
-
attr_reader :consumer_secret
|
41
|
-
# @author Maxine Michalski
|
42
|
-
#
|
43
|
-
# Create a new app, by storing it's credentials into databse.
|
44
|
-
#
|
45
|
-
# @param name [String] Name of the app
|
46
|
-
# @param consumer_key [String] Twitter app consumer key
|
47
|
-
# @param consumer_secret [String] Twitter app consumer secret
|
48
|
-
def self.create(name, consumer_key, consumer_secret)
|
49
|
-
if consumer_key.empty? || consumer_secret.empty?
|
50
|
-
raise ArgumentError, 'Consumer data can\'t be empty.'
|
51
|
-
end
|
52
|
-
Moobooks::Database.connect do |pg|
|
53
|
-
pg.exec('INSERT INTO twitter.apps (name, consumer_key, '\
|
54
|
-
'consumer_secret) VALUES ($1, $2, $3);',
|
55
|
-
[name, consumer_key, consumer_secret])
|
56
|
-
end
|
57
|
-
nil
|
58
|
-
end
|
59
|
-
|
60
|
-
# @author Maxine Michalski
|
61
|
-
#
|
62
|
-
# Creates a list of all Twitter apps in database
|
63
|
-
#
|
64
|
-
# @return [Array<Moobooks::Twitter::App>] A list Twitter apps
|
65
|
-
def self.list
|
66
|
-
apps = Moobooks::Database.connect do |pg|
|
67
|
-
pg.exec('SELECT id FROM twitter.apps ORDER BY id;').to_a
|
68
|
-
end
|
69
|
-
apps.map do |a|
|
70
|
-
Moobooks::Twitter::App.new(a['id'])
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
# @author Maxine Michalski
|
75
|
-
#
|
76
|
-
# Initializer for Twitter appps
|
77
|
-
#
|
78
|
-
# @param id [Integer] The ID of an app we want to fetch
|
79
|
-
def initialize(id)
|
80
|
-
app = Moobooks::Database.connect do |pg|
|
81
|
-
pg.exec('SELECT id, name, consumer_key, consumer_secret, '\
|
82
|
-
'created_at FROM twitter.apps WHERE id = $1;', [id]).first
|
83
|
-
end
|
84
|
-
raise AppNotFoundError if app.nil?
|
85
|
-
app.each do |k, v|
|
86
|
-
instance_variable_set("@#{k}", v)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
# @author Maxine Michalski
|
91
|
-
#
|
92
|
-
# Turn this App into a string representation
|
93
|
-
#
|
94
|
-
# @notice The returned string will be in the form of '<id> <name>'
|
95
|
-
#
|
96
|
-
# @return [String] A formatted string representation of this App
|
97
|
-
def to_s
|
98
|
-
"#{@id} #{@name}"
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
data/spec/twitter_app_spec.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright 2018 Maxine Michalski <maxine@furfind.net>
|
4
|
-
#
|
5
|
-
# This file is part of MooBooks.
|
6
|
-
#
|
7
|
-
# MooBooks is free software: you can redistribute it and/or modify
|
8
|
-
# it under the terms of the GNU General Public License as published by
|
9
|
-
# the Free Software Foundation, either version 3 of the License, or
|
10
|
-
# (at your option) any later version.
|
11
|
-
#
|
12
|
-
# MooBooks is distributed in the hope that it will be useful,
|
13
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
-
# GNU General Public License for more details.
|
16
|
-
#
|
17
|
-
# You should have received a copy of the GNU General Public License
|
18
|
-
# along with MooBooks. If not, see <http://www.gnu.org/licenses/>.
|
19
|
-
|
20
|
-
describe Moobooks::Twitter::App, '.create' do
|
21
|
-
context 'a consumer key and secret are given' do
|
22
|
-
it 'saves them in a database' do
|
23
|
-
pg = double(PG::Connection)
|
24
|
-
expect(pg).to receive(:exec).and_return(true)
|
25
|
-
expect(Moobooks::Database).to receive(:connect).and_yield(pg)
|
26
|
-
expect(Moobooks::Twitter::App.create('', '1', '1')).to be nil
|
27
|
-
end
|
28
|
-
end
|
29
|
-
context 'an empty consumer key and secret are given' do
|
30
|
-
it 'raises an argument error' do
|
31
|
-
expect { Moobooks::Twitter::App.create('', '', '') }.to(
|
32
|
-
raise_error(ArgumentError)
|
33
|
-
)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe Moobooks::Twitter::App, '.list' do
|
39
|
-
it 'returns an array of Twitte::App objects' do
|
40
|
-
pg = double(PG::Connection)
|
41
|
-
data = [{ 'id' => 1, 'name' => 'test' }]
|
42
|
-
allow(pg).to receive(:exec).and_return(data, data)
|
43
|
-
allow(Moobooks::Database).to receive(:connect).and_yield(pg)
|
44
|
-
list = Moobooks::Twitter::App.list
|
45
|
-
expect(list).to be_an Array
|
46
|
-
expect(list.first).to be_a Moobooks::Twitter::App
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
describe Moobooks::Twitter::App, '#to_s' do
|
51
|
-
it 'returns a string representation of an App' do
|
52
|
-
pg = double(PG::Connection)
|
53
|
-
data = [{ 'id' => 1, 'name' => 'test' }]
|
54
|
-
expect(pg).to receive(:exec).and_return(data)
|
55
|
-
expect(Moobooks::Database).to receive(:connect).and_yield(pg)
|
56
|
-
app = Moobooks::Twitter::App.new(1)
|
57
|
-
expect(app.to_s).to be_a String
|
58
|
-
expect(app.to_s).to eq '1 test'
|
59
|
-
end
|
60
|
-
end
|