forked from tsamb/oink-tweeter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoink-tweeter.rb
More file actions
60 lines (50 loc) · 1.51 KB
/
oink-tweeter.rb
File metadata and controls
60 lines (50 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'sinatra'
require 'twitter'
require 'pry'
CLIENT = Twitter::REST::Client.new do |config|
config.consumer_key = ENV["TWITTER_CONSUMER_KEY"]
config.consumer_secret = ENV["TWITTER_CONUMER_SECRET"]
config.access_token = ENV["TWITTER_ACCESS_TOKEN"]
config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
end
get '/' do
@tweets = CLIENT.user_timeline.map { |tweet| pig_latinizer(tweet.text) }
erb :index
end
post '/tweet' do
CLIENT.update(pig_latinizer(params[:tweet]))
redirect '/'
end
def pig_latinizer(string)
string.split(" ").map { |word| pig_latin_logic(word) }.join(" ")
end
def pig_latin_logic(word)
starts_with_letter?(word) ? substitution_rule_chooser(word) : word
end
def substitution_rule_chooser(word)
starts_with_vowel?(word) ? vowel_rule(word) : consonant_rule(word)
end
def starts_with_letter?(word)
word[0] =~ /[a-zA-Z]/ && !(word =~ /\Ahttp/i)
end
def starts_with_vowel?(word)
word[0] =~ /[AEIOUaeiou]/
end
def vowel_rule(word)
trailing_punctuation = word.match(/\W+\z/).to_s
word.sub!(/\W+\z/, "")
word + "way" + trailing_punctuation
end
def consonant_rule(word)
pattern = /\A[^AEIOUaeiou_\W\d]+/
pig_word = word.sub(pattern, "")
# check for end bits like !,; etc
trailing_punctuation = pig_word.match(/\W+\z/).to_s
pig_word.sub!(/\W+\z/, "")
# apply consonant pig latin rule
word.match(pattern) { |m| pig_word = "#{pig_word}#{m}ay" }
# add end bits
pig_word += trailing_punctuation
# return the freshly formatted word
pig_word
end