lloydpick-toast 0.0.2
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.
- data/lib/toast.rb +185 -0
- metadata +73 -0
data/lib/toast.rb
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
#
|
2
|
+
# Toast - Automated Event Driver Twitter Ruby Library
|
3
|
+
# Written by Lloyd Pick
|
4
|
+
# http://gibhub.com/lloydpick
|
5
|
+
# May not be used for commercial applications without prior concent
|
6
|
+
#
|
7
|
+
|
8
|
+
# Requirements
|
9
|
+
require 'rubygems'
|
10
|
+
require 'sqlite3'
|
11
|
+
require 'fileutils'
|
12
|
+
require 'forwardable'
|
13
|
+
require 'httparty'
|
14
|
+
require 'mash'
|
15
|
+
require 'twitter'
|
16
|
+
require 'twitter/base'
|
17
|
+
require 'twitter/httpauth'
|
18
|
+
|
19
|
+
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
20
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
21
|
+
|
22
|
+
module Toast
|
23
|
+
class TwitterAccount
|
24
|
+
attr_accessor :username, :password
|
25
|
+
|
26
|
+
def initialize(username = nil, password = nil)
|
27
|
+
@username = username
|
28
|
+
@password = password
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Bread
|
33
|
+
|
34
|
+
attr_accessor :name, :twitter_message, :twitter_account, :butters
|
35
|
+
|
36
|
+
def initialize(name = nil, message = nil)
|
37
|
+
@name = name
|
38
|
+
@twitter_message = message
|
39
|
+
@butters = []
|
40
|
+
end
|
41
|
+
|
42
|
+
class Butter < Bread
|
43
|
+
|
44
|
+
attr_accessor :name, :post_condition, :function
|
45
|
+
|
46
|
+
def initialize(name = nil, post_condition = nil, function = nil)
|
47
|
+
@name = name
|
48
|
+
@post_condition = post_condition
|
49
|
+
@function = function
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
class Toaster
|
57
|
+
|
58
|
+
attr_accessor :bread, :timer
|
59
|
+
|
60
|
+
def initialize
|
61
|
+
@bread = []
|
62
|
+
@timer = 60
|
63
|
+
end
|
64
|
+
|
65
|
+
def toast!
|
66
|
+
File.delete("toast.db")
|
67
|
+
db = SQLite3::Database.new("toast.db")
|
68
|
+
db.execute("CREATE TABLE IF NOT EXISTS toasters (
|
69
|
+
id integer PRIMARY KEY,
|
70
|
+
frequency integer,
|
71
|
+
last_check datetime,
|
72
|
+
conditions varchar2(32),
|
73
|
+
complete boolean
|
74
|
+
);")
|
75
|
+
db.execute("CREATE TABLE IF NOT EXISTS breads (
|
76
|
+
id integer PRIMARY KEY,
|
77
|
+
name varchar2(64),
|
78
|
+
twitter_username varchar2(64),
|
79
|
+
twitter_password varchar2(64),
|
80
|
+
twitter_message varchar2(140)
|
81
|
+
);")
|
82
|
+
db.execute("CREATE TABLE IF NOT EXISTS butters (
|
83
|
+
id integer PRIMARY KEY,
|
84
|
+
bread_id integer,
|
85
|
+
name varchar2(64),
|
86
|
+
function varchar2(64),
|
87
|
+
outcome varchar2(64)
|
88
|
+
);")
|
89
|
+
|
90
|
+
self.bread.each do |bread|
|
91
|
+
db.execute("INSERT INTO breads (
|
92
|
+
name,
|
93
|
+
twitter_username,
|
94
|
+
twitter_password,
|
95
|
+
twitter_message
|
96
|
+
) VALUES (
|
97
|
+
:name,
|
98
|
+
:twitter_username,
|
99
|
+
:twitter_password,
|
100
|
+
:twitter_message
|
101
|
+
)
|
102
|
+
",
|
103
|
+
"name" => bread.name,
|
104
|
+
"twitter_username" => bread.twitter_account.username,
|
105
|
+
"twitter_password" => bread.twitter_account.password,
|
106
|
+
"twitter_message" => bread.twitter_message
|
107
|
+
);
|
108
|
+
bread_id = db.execute("select last_insert_rowid()")
|
109
|
+
|
110
|
+
butters = []
|
111
|
+
bread.butters.each do |butter|
|
112
|
+
db.execute("INSERT INTO butters (
|
113
|
+
bread_id,
|
114
|
+
name,
|
115
|
+
function,
|
116
|
+
outcome
|
117
|
+
) VALUES (
|
118
|
+
:bread_id,
|
119
|
+
:name,
|
120
|
+
:function,
|
121
|
+
:outcome
|
122
|
+
)
|
123
|
+
",
|
124
|
+
"bread_id" => bread_id,
|
125
|
+
"name" => butter.name,
|
126
|
+
"function" => butter.function,
|
127
|
+
"outcome" => butter.post_condition
|
128
|
+
);
|
129
|
+
butters << db.execute("select last_insert_rowid()")
|
130
|
+
end
|
131
|
+
|
132
|
+
db.execute("INSERT INTO toasters (
|
133
|
+
frequency,
|
134
|
+
conditions,
|
135
|
+
complete,
|
136
|
+
last_check
|
137
|
+
) VALUES (
|
138
|
+
:frequency,
|
139
|
+
:conditions,
|
140
|
+
:complete,
|
141
|
+
:last_check
|
142
|
+
)
|
143
|
+
",
|
144
|
+
"frequency" => self.timer,
|
145
|
+
"conditions" => butters.join(","),
|
146
|
+
"complete" => false,
|
147
|
+
"last_check" => Time.now
|
148
|
+
);
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
def self.run
|
155
|
+
db = SQLite3::Database.new("toast.db")
|
156
|
+
|
157
|
+
db.results_as_hash = true
|
158
|
+
db.execute("select * from toasters where complete = 'false'") do |row|
|
159
|
+
if ((Time.parse(row["last_check"]) + row["frequency"].to_i) < Time.now)
|
160
|
+
row["conditions"].split(",")
|
161
|
+
row["conditions"].each do |condition|
|
162
|
+
db.execute("select * from butters where id = :id", ":id" => condition) do |butter_row|
|
163
|
+
output = eval(butter_row["function"])
|
164
|
+
if output == butter_row["outcome"]
|
165
|
+
db.execute("select * from breads where id = :id", ":id" => butter_row["bread_id"]) do |twitter|
|
166
|
+
httpauth = Twitter::HTTPAuth.new(twitter["twitter_username"], twitter["twitter_password"])
|
167
|
+
base = Twitter::Base.new(httpauth)
|
168
|
+
base.update(twitter["twitter_message"])
|
169
|
+
db.execute("update toasters set last_check = :last_check and complete = :outcome where id = :id",
|
170
|
+
"last_check" => Time.now, "id" => row["id"], "outcome" => true)
|
171
|
+
end
|
172
|
+
else
|
173
|
+
# Did'nt pass the condition, so update the time
|
174
|
+
db.execute("update toasters set last_check = :last_check where id = :id",
|
175
|
+
"last_check" => Time.now, "id" => row["id"])
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lloydpick-toast
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lloyd Pick
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-12 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: twitter
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.6.12
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sqlite3-ruby
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.4
|
34
|
+
version:
|
35
|
+
description: Toast is a Ruby library for doing pre-defined automated tasks
|
36
|
+
email: lloydpick@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/toast.rb
|
45
|
+
has_rdoc: false
|
46
|
+
homepage: https://github.com/lloydpick/toast
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --inline-source
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.2.0
|
69
|
+
signing_key:
|
70
|
+
specification_version: 2
|
71
|
+
summary: Toast is a Rubygem for doing pre-defined automated tasks
|
72
|
+
test_files: []
|
73
|
+
|