Module: TheTwittHit::CLI::Helpers

Defined in:
lib/thetwitthit/cli/helpers.rb

Constant Summary collapse

LAUNCH_AGENT_PLIST =
<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.sagmor.TheTwittHit</string>
	<key>OnDemand</key>
	<false/>
	<key>ProgramArguments</key>
	<array>
		<string>#s</string>
		<string>start</string>
	</array>
</dict>
</plist>
EOF
PLIST_FILE =
File.join(ENV['HOME'],'Library','LaunchAgents','com.sagmor.thetwitthit.plist')

Instance Method Summary collapse

Instance Method Details

#authorizeObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/thetwitthit/cli/helpers.rb', line 75

def authorize
  config = TheTwittHit::Config.new

  if config.access_token
    puts "There is a twitter access configured!"
    clear = agree("do you want to re-authorize TheTwittHit? [yes/no]")
    return unless clear
    
    config.access_token = nil
  end

  while config.access_token.nil? do
    puts "Authenticating with Twitter:"
    begin
      auth = config.auth(false)
      rtoken, rsecret = auth.request_token.token, auth.request_token.secret

      puts "The script will open your browser,"
      puts "authorize the app at twitter and come back here"

      sleep 2
      %x(open #{auth.request_token.authorize_url})

      until agree("are you ready? [yes/no]")
      end

      auth = config.auth(false,true)
      auth.authorize_from_request(rtoken, rsecret)
      twitter = Twitter::Base.new(auth)
      twitter.friends_timeline

      config.access_token  = auth.access_token.token
      config.access_secret = auth.access_token.secret

      config.auth(true,true)

    rescue
      puts "Authorization Failed!"
    end
  end
  
  config.save
end

#configureObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/thetwitthit/cli/helpers.rb', line 50

def configure
  config = TheTwittHit::Config.new
  puts "load \#todo twitts from [yes/no]:"
  config.load_user_timeline_todos = agree(" * your timeline?")
  config.load_replies_todos = agree(" * your replies?")
  config.load_direct_messages_todos = agree(" * your direct messages?")
  puts
  config.sleep_time = 60*ask(
                  "how often dou you want to load your twitts? (in minutes)", 
                  Integer) { |q| q.default = 60 }

  config.save
end

#daemon(mode) ⇒ Object



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
# File 'lib/thetwitthit/cli/helpers.rb', line 24

def daemon(mode)
  Daemons.run_proc('TheTwittHit', :dir_mode => :normal,
                                  :log_output => :true,
                                  :dir => TheTwittHit::Config::APP_SUPPORT,
                                  :ARGV => [mode]) do

    Signal.trap("TERM") do 
      puts "#{Time.now} - Stoping TheTwittHit"
      exit(0)
    end
    
    puts "#{Time.now} - Starting TheTwittHit"

    while(true) do
      puts "#{Time.now} - Fetching last Tweets"
      w = TheTwittHit::Worker.new
      w.load

      sleep w.config.sleep_time
    end

    

  end
end

#start_on_loginObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/thetwitthit/cli/helpers.rb', line 64

def 
  if agree("Do you want to configure TheTwittHit to start on login? [yes/no]")
    File.open(PLIST_FILE,'w') do |f|
      bin = File.join(File.dirname(__FILE__),'..','..','..','bin','thetwitthit')
      f.puts LAUNCH_AGENT_PLIST % bin
    end
  else
    File.delete(PLIST_FILE) if File.exists?(PLIST_FILE)
  end
end