Class: Tw::App::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/tw/app/cmds.rb,
lib/tw/app/main.rb,
lib/tw/app/opt_parser.rb

Instance Method Summary collapse

Constructor Details

#initializeMain

Returns a new instance of Main.



13
14
15
16
17
18
19
20
21
# File 'lib/tw/app/main.rb', line 13

def initialize
  on_exit do
    exit 0
  end

  on_error do
    exit 1
  end
end

Instance Method Details

#all_requests?(args) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/tw/app/opt_parser.rb', line 16

def all_requests?(args)
  !args.map{|arg| request? arg}.include?(false)
end

#clientObject



23
24
25
# File 'lib/tw/app/main.rb', line 23

def client
  @client ||= Tw::Client.new
end

#listname?(arg) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/tw/app/opt_parser.rb', line 8

def listname?(arg)
  arg =~ /^@[a-zA-Z0-9_]+\/[a-zA-Z0-9_-]+$/ ? arg.scan(/^@([a-zA-Z0-9_]+)\/([a-zA-Z0-9_-]+)$/)[0] : false
end

#on_error(&block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/tw/app/main.rb', line 35

def on_error(&block)
  if block_given?
    @on_error = block
  else
    @on_error.call if @on_error
  end
end

#on_exit(&block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/tw/app/main.rb', line 27

def on_exit(&block)
  if block_given?
    @on_exit = block
  else
    @on_exit.call if @on_exit
  end
end

#request?(arg) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/tw/app/opt_parser.rb', line 12

def request?(arg)
  username?(arg) or listname?(arg)
end

#run(argv) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/tw/app/main.rb', line 43

def run(argv)
  @args = ArgsParser.parse argv, :style => :equal do
    arg :user, 'user account', :alias => :u
    arg 'user:add', 'add user'
    arg 'user:list', 'show user list'
    arg 'user:default', 'set default user'
    arg :timeline, 'show timeline', :alias => :tl
    arg :dm, 'show direct messages'
    arg 'dm:to', 'create direct message'
    arg :favorite, 'favorite tweet', :alias => :fav
    arg :retweet, 'retweet', :alias => :rt
    arg :delete, 'delete tweet', :alias => :del
    arg :search, 'search public timeline', :alias => :s
    arg :stream, 'show user stream', :alias => :st
    arg :status_id, 'show status_id', :alias => :id
    arg :file, 'upload file'
    arg :pipe, 'pipe tweet'
    arg :format, 'output format', :default => 'text'
    arg :silent, 'silent mode'
    arg :yes, 'do not show dialogue'
    arg :conf, 'config file', :default => Tw::Conf.conf_file
    arg :version, 'show version', :alias => :v
    arg :help, 'show help', :alias => :h

    validate :user, 'invalid user name' do |v|
      v =~ /^[a-zA-Z0-9_]+$/
    end

    validate 'user:default', 'invalid user name' do |v|
      v =~ /^[a-zA-Z0-9_]+$/
    end

    validate 'dm:to', 'invalid user name' do |v|
      v =~ /^[a-zA-Z0-9_]+$/
    end

    validate :file, "file does not exists" do |v|
      File.exists? v
    end
  end

  if @args.has_option? :help
    STDERR.puts "Tw - Twitter client on Ruby v#{Tw::VERSION}"
    STDERR.puts "     http://shokai.github.io/tw"
    STDERR.puts
    STDERR.puts @args.help
    STDERR.puts
    STDERR.puts "e.g."
    STDERR.puts "tweet  tw hello world"
    STDERR.puts "       echo 'hello' | tw --pipe"
    STDERR.puts "       tw 'yummy!!' --file=food.jpg --yes"
    STDERR.puts "read   tw @username"
    STDERR.puts "       tw @username @user2 @user2/listname"
    STDERR.puts "       tw --search=ruby"
    STDERR.puts "       tw --stream"
    STDERR.puts "       tw --stream:filter=ruby,java"
    STDERR.puts "       tw --dm:to=username \"hello!\""
    STDERR.puts "id     tw @shokai --id"
    STDERR.puts "       tw --id=334749349588377601"
    STDERR.puts 'reply  tw "@shokai wow!!" --id=334749349588377601'
    STDERR.puts "       tw --fav=334749349588377601"
    STDERR.puts "       tw --rt=334749349588377601"
    STDERR.puts "       tw --format=json"
    STDERR.puts '       tw --format="@#{user} #{text} - http://twitter.com/#{user}/#{id}"'
    STDERR.puts "delete tw --del=334749349588377601"
    on_exit
  end

  Render.silent = (@args.has_option? :silent or @args.has_param? :format)
  Render.show_status_id = @args[:status_id]
  Tw::Conf.conf_file = @args[:conf]

  regist_cmds

  cmds.each do |name, cmd|
    next unless @args[name]
    cmd.call @args[name], @args
  end

  auth
  if @args.argv.size < 1
    if @args.has_param? :status_id
      client.show_status @args[:status_id]
    else
      Render.display client.mentions, @args[:format]
    end
  elsif all_requests?(@args.argv)
    Render.display Parallel.map(@args.argv, :in_threads => @args.argv.size){|arg|
      if user = username?(arg)
        res = client.user_timeline user
      elsif (user, list = listname?(arg)) != false
        res = client.list_timeline(user, list)
      end
      res
    }, @args[:format]
  else
    message = @args.argv.join(' ')
    tweet_opts = {}
    if (len = message.char_length_with_t_co) > 280
      STDERR.puts "tweet too long (#{len} chars)"
      on_error
    else
      if @args.has_param? :status_id
        client.show_status @args[:status_id]
        puts "--"
        puts "reply \"#{message}\"? (#{len} chars)"
        tweet_opts[:in_reply_to_status_id] = @args[:status_id]
      else
        puts "tweet \"#{message}\"?  (#{len} chars)"
        if @args.has_param? :file
          puts "upload \"#{@args[:file]}\"? (#{File.size @args[:file]} bytes)"
        end
      end
      unless @args.has_option? :yes
        puts '[Y/n]'
        on_exit if STDIN.gets.strip =~ /^n/i
      end
    end
    begin
      if @args.has_param? :file
        client.tweet_with_file message, File.open(@args[:file]), tweet_opts
      else
        client.tweet message, tweet_opts
      end
    rescue => e
      STDERR.puts e.message
    end
  end
end

#username?(arg) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/tw/app/opt_parser.rb', line 4

def username?(arg)
  arg =~ /^@[a-zA-Z0-9_]+$/ ? arg.scan(/^@([a-zA-Z0-9_]+)$/)[0][0] : false
end