lazybird 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+
4
+ module Lazybird
5
+ module Tasks
6
+ class Quote
7
+ def initialize(url:, retry_attempts: 3)
8
+ @url = url
9
+ @retry_attempts = retry_attempts
10
+ @retry_count = 0
11
+ end
12
+
13
+ # TODO create a generic way to call diff apis (this should be on its own subclass)
14
+ def random_storm_quote
15
+ raise Lazybird::QuoteMaxRetriesException if @retry_count > @retry_attempts
16
+ quote = fetch_json(@url)['quote']
17
+ quote_check(quote) { random_storm_quote }
18
+ end
19
+
20
+ def quote_check(quote)
21
+ if quote.length > 140
22
+ @retry_count += 1
23
+ yield
24
+ else
25
+ @retry_count = 0
26
+ quote
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def fetch_json(json_url)
33
+ JSON.load(open(json_url))
34
+ end
35
+ end
36
+
37
+ class QuoteMaxRetriesException < StandardError;
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ module Lazybird
2
+ module Tasks
3
+ class RetweetRandomTask < Lazybird::Tasks::Task
4
+ def run
5
+ @twitter_client.retweet_random_friend
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ require 'lazybird/tasks/quote'
2
+
3
+ module Lazybird
4
+ module Tasks
5
+ class StormQuoteTask < Lazybird::Tasks::Task
6
+ def run
7
+ quote = Lazybird::Tasks::Quote.new(url: 'http://quotes.stormconsultancy.co.uk/random.json')
8
+ @twitter_client.tweet(message: quote.random_storm_quote)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ require 'lazybird/twitter_client'
2
+
3
+ module Lazybird
4
+ module Tasks
5
+ class Task
6
+ attr_reader :command, :opts
7
+
8
+ def initialize(command:, twitter_client: Lazybird::TwitterClient, **opts)
9
+ # TODO define command on subclass
10
+ @command = command
11
+ @opts = opts
12
+ @twitter_client = twitter_client
13
+ end
14
+
15
+ def run
16
+ raise NotImplementedError
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ require 'rufus-scheduler'
2
+
3
+ module Lazybird
4
+ module Tasks
5
+ module TaskScheduler
6
+ extend self
7
+
8
+ @job = nil
9
+ @scheduler = Rufus::Scheduler.new
10
+
11
+ def self.schedule(frequency: '1h', &block)
12
+ @frequency = frequency
13
+ restart_scheduler unless @job.nil?
14
+ @job = job(&block)
15
+ end
16
+
17
+ def self.job_info
18
+ @job ? "Runs every: #{@job.original} Ran: #{@job.count} times." : 'No run!'
19
+ end
20
+
21
+ private
22
+
23
+ def job
24
+ job_id = @scheduler.every "#{@frequency}" do
25
+ # Process random task
26
+ yield
27
+ end
28
+ @scheduler.job(job_id)
29
+ end
30
+
31
+ def restart_scheduler
32
+ @job.unschedule
33
+ @job.kill
34
+ #@scheduler.shutdown(:kill)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,48 @@
1
+ require 'lazybird/db'
2
+
3
+ module Lazybird
4
+ module TwitterClient
5
+ extend self
6
+
7
+ def retweet_random_friend
8
+ tweet = random_friend_tweet
9
+ message = tweet.text
10
+ duplicate_tweet_check(message)
11
+ client.retweet tweet
12
+ message
13
+ rescue Twitter::Error::Forbidden, Sequel::UniqueConstraintViolation
14
+ duplicated_tweet_warning
15
+ end
16
+
17
+ def tweet(message:)
18
+ duplicate_tweet_check(message)
19
+ client.update message
20
+ message
21
+ rescue Twitter::Error::Forbidden, Sequel::UniqueConstraintViolation
22
+ duplicated_tweet_warning
23
+ end
24
+
25
+ private
26
+
27
+ def random_friend_id
28
+ client.friends.to_a.sample.id
29
+ end
30
+
31
+ def random_friend_tweet
32
+ client.user_timeline(random_friend_id, {count: 1}).first
33
+ end
34
+
35
+ def client
36
+ @_client ||= Lazybird::Config.client
37
+ end
38
+
39
+ def duplicated_tweet_warning
40
+ puts 'Same tweet found! Ignoring this run...'
41
+ nil
42
+ end
43
+
44
+ def duplicate_tweet_check(message)
45
+ Lazybird::Db.save_tweet!(text: message)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,17 @@
1
+ module Lazybird
2
+ module Utils
3
+ module Hash
4
+ def self.find_by_key(hash, key)
5
+ hash.select { |k, h| k == key }
6
+ end
7
+
8
+ def self.compact(hash)
9
+ hash.select { |_, v| v }
10
+ end
11
+
12
+ def self.array_to_hash(keys, values)
13
+ ::Hash[[keys, values].transpose]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ module Lazybird
2
+ VERSION = "0.1.0"
3
+
4
+ def self.version
5
+ VERSION
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazybird
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Lopez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: twitter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.15'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.15'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rufus-scheduler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: amalgalite
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.4'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sequel
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.27'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.27'
111
+ - !ruby/object:Gem::Dependency
112
+ name: colorize
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.7'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.7'
125
+ description: 'Provides a command line interface to interact with twitter and automatically
126
+ tweets/retweets some predefined (or API based tweets) at a specified frequency. '
127
+ email:
128
+ - james@jameslopez.es
129
+ executables:
130
+ - lazybird
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - ".gitignore"
135
+ - ".rspec"
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/console
141
+ - bin/lazybird
142
+ - bin/setup
143
+ - lazybird.gemspec
144
+ - lib/config/tasks.yml
145
+ - lib/lazybird.rb
146
+ - lib/lazybird/cli/command_line.rb
147
+ - lib/lazybird/cli/commands.rb
148
+ - lib/lazybird/config.rb
149
+ - lib/lazybird/db.rb
150
+ - lib/lazybird/lazybird_facade.rb
151
+ - lib/lazybird/tasks/quote.rb
152
+ - lib/lazybird/tasks/retweet_random_task.rb
153
+ - lib/lazybird/tasks/storm_quote_task.rb
154
+ - lib/lazybird/tasks/task.rb
155
+ - lib/lazybird/tasks/task_scheduler.rb
156
+ - lib/lazybird/twitter_client.rb
157
+ - lib/lazybird/utils.rb
158
+ - lib/lazybird/version.rb
159
+ homepage: https://github.com/bluegod/lazybird
160
+ licenses:
161
+ - GPL-2.0
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubyforge_project:
179
+ rubygems_version: 2.4.8
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: Twitter for lazy people. Automatically tweets/retweets random stuff.
183
+ test_files: []