twix 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +58 -1
  2. data/lib/twix.rb +60 -7
  3. metadata +24 -8
data/README.md CHANGED
@@ -10,4 +10,61 @@ The first implementation will allow you to run something like this:
10
10
  $ twix 12345
11
11
  ```
12
12
 
13
- Where 12345 is the id of the tweet.
13
+ Where 12345 is the id of the tweet. It will execute the body of the
14
+ tweet as Ruby code. So, if Tweet #12345 has body "puts 'Hello World'"
15
+ it'll print "Hello World" to the screen.
16
+
17
+ You can also run:
18
+
19
+ ```bash
20
+ $ twix @cmaxw
21
+ ```
22
+
23
+ If you know that someone has recently tweeted some Ruby code you want to run, this will show the last 20 tweets that person has shared and you can choose the tweet you want to run. Something like this.
24
+
25
+ ```
26
+ Tweets for @cmaxw:
27
+ 0 - Hey, don’t forget to vote for @rubyrogues today in the podcast awards. http://t.co/6BHkRXuS
28
+ 1 - p "Hi, welcome to twix, the Twitter Executor for Ruby"
29
+ 2 - RT @JSJabber: RT @JSJabber 034 JSJ Ember.js http://t.co/rZuOHT0R cc/ @trek
30
+ 3 - @chrishunt @jstorimer @saturnflyer @pat_shaughnessy If it’s within walking distance. Shall we meet up by the registration table?
31
+ 4 - OH: “Mathematicians don’t like intention revealing names” via @jimweirich at #rubyconf
32
+ 5 - @kfarris glad it helped. What’s your background? coding? sales? marketing? management?
33
+ 6 - @jstorimer @saturnflyer @pat_shaughnessy I was looking forward to a boxed lunch, but if you insist… Where should we meet?
34
+ 7 - @jwo I looked at rubycop. How it works is not immediately obvious (due partly to the lack of README.) Don’t think I want to require jruby .
35
+ 8 - @natehop Thanks, appreciate it. I love recording the shows. I also love that people enjoy listening.
36
+ 9 - So, if I wanted to write a script that downloaded a random string of code and executed it, what is the best way to do so safely?
37
+ 10 - Or, in @r00k’s case. Find an audience to heckle you.
38
+ 11 - Sitting in @r00k’s talk, it occurs to me that when they say ‘grow a pair’, they mean ‘find someone to sit with you and check your code.’
39
+ 12 - @codeodor LOL, mine’s an android phone. I’m going to upgrade it to an iPhone5. Wish me luck!
40
+ 13 - My phone won’t turn off the screen. So, it died after half a day.
41
+ 14 - Anyone up for dinner after the minitest #bof at #Rubyconf?
42
+ 15 - @martco Nice meeting you too. I hope I keep them going for a long time as well.
43
+ 16 - Had a terrific discussion with @jstorimer about ebooks, podcasts, marketing, and videos.
44
+ 17 - So, it turns out my phone is not rubyfriends friendly. Rear facing camera only with an onscreen button to take pics.
45
+ 18 - RT @blowmage: RubyConf attendees: join us tonight for a BoF session on minitest and rails4. Please RT. #rubyconf
46
+ 19 - RT @dbrady: If you are weapons-grade at ExtJS, have a passing knowledge of ruby, and have a couple weeks free (or more) please contact m ...
47
+
48
+
49
+ Which Tweet do you want to execute?
50
+ ```
51
+
52
+ If you choose '1', it'll execute the tweet and print "Hi, welcome to
53
+ twix, the Twitter Executor for Ruby"
54
+
55
+ #Safety
56
+
57
+ We want you to be able to execute a tweet without concerns about whether
58
+ it's going to do something crazy like `rm -Rf /` (delete your entire
59
+ hard drive. We're using [RubyCop](https://github.com/envylabs/RubyCop)
60
+ to make sure that the tweet is restricted from taking control of your
61
+ system through the Ruby interpreter or doing anything harmful.
62
+
63
+ To turn off the safety feature, run twix with the --naked command.
64
+
65
+ ---
66
+
67
+ #ToDo
68
+ * Set up a service that archives twixable tweets
69
+ * Add ability to require other tweets
70
+
@@ -1,7 +1,12 @@
1
+ #encoding: UTF-8
2
+
3
+ require 'oauth'
1
4
  require 'twitter'
2
5
  require 'yaml'
6
+ require 'ruby_cop'
7
+ require 'pry'
3
8
 
4
- module Twix
9
+ class Twix
5
10
  def self.init
6
11
  config_file = File.expand_path("~") + "/.twixrc"
7
12
  twix_config = YAML.load_file(config_file)
@@ -14,14 +19,37 @@ module Twix
14
19
  end
15
20
 
16
21
  def self.run(args)
22
+ @args = args
17
23
  handle_switches
18
- usernames = args.select {|arg| arg.match(/\A@/)}
19
- user = usernames.first
20
- show_user_tweets(user) if user
24
+ if args.last.match(/\A@/)
25
+ show_user_tweets(args.last)
26
+ elsif args.last =~ /\A[0-9]+\Z/
27
+ tweet = Twitter.status(args.last)
28
+ execute_tweet(tweet)
29
+ end
21
30
  end
22
31
 
32
+ private
33
+
23
34
  def self.handle_switches
24
- true
35
+ set_use_ruby_cop(!has_switch?("naked"))
36
+ end
37
+
38
+ def self.set_use_ruby_cop(on = true)
39
+ @ruby_cop = on
40
+ end
41
+
42
+ def self.ruby_cop?
43
+ @ruby_cop
44
+ end
45
+
46
+ def self.has_switch?(switch)
47
+ !!get_switch(switch)
48
+ end
49
+
50
+ def self.get_switch(switch)
51
+ dashes = switch.length == 1 ? "-" : "--"
52
+ @args.detect{|a| a.match(/\A#{dashes}#{switch}/) }
25
53
  end
26
54
 
27
55
  def self.show_user_tweets(user)
@@ -35,11 +63,36 @@ module Twix
35
63
  if tweet_index.match(/\A(1|)[0-9]\Z/).nil?
36
64
  puts "\n\nYour input must be between 0 and 19"
37
65
  show_user_tweets(user)
66
+ else
67
+ execute_tweet timeline[tweet_index.to_i]
38
68
  end
39
- eval timeline[tweet_index.to_i].text
40
69
  end
41
70
 
42
71
  def self.display_tweet(tweet, index)
43
- puts "\t#{index} - #{tweet.text}"
72
+ $stdout.puts "\t#{index} - #{tweet.text}"
73
+ end
74
+
75
+ def self.safe?(tweet)
76
+ !ruby_cop? || check_ruby_cop(tweet)
77
+ end
78
+
79
+ def self.check_ruby_cop(tweet)
80
+ policy = RubyCop::Policy.new
81
+ ast = RubyCop::NodeBuilder.build(tweet)
82
+ ast.accept(policy)
83
+ end
84
+
85
+ def self.execute_tweet(tweet)
86
+ text = sanitize_tweet(tweet.text)
87
+ if safe?(text)
88
+ eval(text)
89
+ else
90
+ puts "WARNING: That tweet is trying to do something dangerous! Here's the tweet:\n\t #{tweet.text}\n\t To remove the safety net, run `twix --naked #{tweet.id}`"
91
+ end
92
+ end
93
+
94
+ def self.sanitize_tweet(tweet)
95
+ tweet.gsub(/[”“]/, '"')
96
+ .gsub(/’/, "'")
44
97
  end
45
98
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-01 00:00:00.000000000 Z
12
+ date: 2012-11-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: twitter
@@ -32,17 +32,33 @@ dependencies:
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 0.4.7
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 0.4.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: rubycop
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.1
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: rspec
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -98,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
114
  version: '0'
99
115
  segments:
100
116
  - 0
101
- hash: 4532826585065384912
117
+ hash: 4092312738592471102
102
118
  required_rubygems_version: !ruby/object:Gem::Requirement
103
119
  none: false
104
120
  requirements:
@@ -107,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
123
  version: '0'
108
124
  requirements: []
109
125
  rubyforge_project:
110
- rubygems_version: 1.8.24
126
+ rubygems_version: 1.8.19
111
127
  signing_key:
112
128
  specification_version: 3
113
129
  summary: Executes a Tweet as Ruby Code