Major refactoring to provide OAuth support for all APIs.
[twitter4r-core.git] / bin / t4rsh
blob9efba54a3c15dfc119553b08aabc85bd69d2c54b
1 #!/usr/bin/env ruby
3 require("irb")
4 require("irb/completion")
5 require("rubygems")
7 begin
8 gem('twitter4r', '>0.3.0')
9 require("twitter")
10 require("twitter/console")
11 rescue Gem::LoadError
12 begin
13 gem("mbbx6spp-twitter4r", '>=0.3.1')
14 require("twitter")
15 require("twitter/console")
16 rescue Gem::LoadError
17 abort("Error: You must install either twitter4r gem from Rubyforge with version 0.3.1 or greater or the mbbx6spp-twitter4r gem from GitHub's servers with version 0.3.1 or greater (and make sure it is a recent version of the gem).")
18 end
19 end
21 module Twitter
22 class Console
23 class << self
24 def config_file
25 result = ENV["T4R_CONFIG"]
26 file_name = File.expand_path('twitter.yml')
27 result ||= file_name if File.exists?(file_name)
28 file_name = File.expand_path('twitter.yml', 'config')
29 result ||= file_name if File.exists?(file_name)
30 file_name = File.expand_path('~/.twitter.yml')
31 result ||= file_name if File.exists?(file_name)
32 result
33 end
35 def account
36 ENV["T4R_ENV"] || ENV["MERB_ENV"] || ENV["RAILS_ENV"]
37 end
39 def run(file)
40 IRB.init_config(nil)
41 # configuration...
42 IRB.conf[:IRB_NAME] = "t4rsh"
43 IRB.conf[:VERSION] = Twitter::Version.to_version
44 IRB.conf[:USE_READLINE] = true
45 IRB.conf[:PROMPT_MODE] = :T4RSH
46 IRB.conf[:PROMPT][:T4RSH] = {
47 :PROMPT_I => "%N[%3n:%i]> ", # top level prompt
48 :PROMPT_C => "%N[%3n:%i]* ", # after conditional like "if"
49 :PROMPT_S => "%N[%3n:%i]* ", # during continuing string
50 :RETURN => "=> %s\n", # return value
52 IRB.start(file)
53 end
54 end
55 end
56 end
58 if __FILE__ == $0
59 @twitter = nil
60 config_file = Twitter::Console.config_file
61 account = Twitter::Console.account
63 if config_file && account
64 @twitter = Twitter::Client.from_config(config_file, account)
65 puts "Used #{config_file} to create client for #{account} account."
66 puts "Access @twitter for instantiated client."
67 Twitter::Console.run(__FILE__)
68 else
69 abort("Please make sure #{config_file} exists and contains your Twitter credentials (separated by account/environment) and that you specify the account/environment to use, e.g. if you have a 'test' section in your configuration file that you want to use set/export T4R_ENV=test as an environment variable or RAILS_ENV=test or MERB_ENV=test")
70 end
71 end