Class: George

Inherits:
Object
  • Object
show all
Defined in:
lib/george.rb,
lib/george/config.rb,
lib/george/oauth_client.rb

Defined Under Namespace

Classes: Config, OAuthClient

Constant Summary collapse

ROOT =
File.expand_path('../..', __FILE__)
LIB =
ROOT + '/lib/george'
CONFIG_PATH =
ROOT + '/config/twitter.yml'
ANNA_VIM =
ROOT + '/config/anna.vim'
TEMPLATE_PATH =
ROOT + '/templates'
CALLBACK_PATH =
'/authenticate'
DEFAULT_PORT =
4180
DOTFILE_PATH =
File.expand_path('~/.georgerc')
SCRATCH_PATH =
File.expand_path('~/.GEORGE_TWEET')

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/george.rb', line 65

def method_missing(name, *args)
  template_path = File.join(TEMPLATE_PATH, "#{name}.yml")
  raise "Not a valid command: `george #{name}`" unless File.file?(template_path)

  templates = YAML.load_file(template_path)
  thing     = args.first
  messages  = (templates['custom'][thing] || []) + templates['generic']
  template  = messages[rand(messages.size)]
  message   = ERB.new(template).result(binding)

  post_to_twitter(message)
end

Class Method Details

.run(argv) ⇒ Object



22
23
24
# File 'lib/george.rb', line 22

def self.run(argv)
  new.run(argv)
end

Instance Method Details

#configObject



26
27
28
# File 'lib/george.rb', line 26

def config
  @config ||= Config.new(CONFIG_PATH)
end

#install(*args) ⇒ Object



39
40
41
42
43
44
# File 'lib/george.rb', line 39

def install(*args)
  client = OAuthClient.new(config)
  client.boot
  credentials = client.get_oauth_credentials
  File.open(DOTFILE_PATH, 'w') { |f| f.write(JSON.dump(credentials)) }
end

#post_to_twitter(message) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/george.rb', line 78

def post_to_twitter(message)
  message.strip!
  return if message == ''
  tweet = "@#{twitter_username} #{message}"
  if ENV['DEBUG']
    puts tweet
  else
    twitter.update(tweet)
  end
end

#run(argv) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/george.rb', line 30

def run(argv)
  command = argv.first
  __send__(command, *argv[1..-1])
  exit 0
rescue => e
  $stderr.puts(e.message)
  exit 1
end

#twitterObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/george.rb', line 89

def twitter
  unless File.file?(DOTFILE_PATH)
    raise "Please run `george install` to authenticate with Twitter"
  end

  credentials = JSON.parse(File.read(DOTFILE_PATH))
  Twitter::Client.new(
    :consumer_key       => config.consumer_key,
    :consumer_secret    => config.consumer_secret,
    :oauth_token        => credentials['token'],
    :oauth_token_secret => credentials['secret']
  )
end

#twitter_usernameObject



103
104
105
# File 'lib/george.rb', line 103

def twitter_username
  ENV['GEORGE_USERNAME'] || config.twitter_username
end

#vim(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/george.rb', line 46

def vim(*args)
  File.open(SCRATCH_PATH, 'w') do |f|
    f.write "# Compose your tweet in George's favourite editor!\n"
  end
  vim = ChildProcess.build('vim', '-u', ANNA_VIM, SCRATCH_PATH)
  vim.io.inherit!
  vim.start
  sleep 0.01 until vim.exited?
  message = File.read(SCRATCH_PATH).split("\n").delete_if { |l| l =~ /^\s*#/ }.join("\n")

  post_to_twitter(message)
ensure
  File.unlink(SCRATCH_PATH)
end