Class: Twat::Actions

Inherits:
Object
  • Object
show all
Defined in:
lib/twat/actions.rb,
lib/twat/actions/add.rb,
lib/twat/actions/show.rb,
lib/twat/actions/tweet.rb,
lib/twat/actions/delete.rb,
lib/twat/actions/follow.rb,
lib/twat/actions/version.rb,
lib/twat/actions/setoption.rb,
lib/twat/actions/user_feed.rb,
lib/twat/actions/follow_user.rb,
lib/twat/actions/updateconfig.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActions

Returns a new instance of Actions.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/twat/actions/follow.rb', line 46

def initialize
  @failcount = 0

  def reader.filter
    case @buf.length
    when 140
      _print ""
    when 139
      _print ""
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Raises:



10
11
12
# File 'lib/twat/actions.rb', line 10

def method_missing(sym, *args, &block)
  raise NoSuchCommand
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/twat/actions.rb', line 8

def config
  @config
end

#failcountObject

Returns the value of attribute failcount.



8
9
10
# File 'lib/twat/actions.rb', line 8

def failcount
  @failcount
end

#optsObject

Returns the value of attribute opts.



8
9
10
# File 'lib/twat/actions.rb', line 8

def opts
  @opts
end

Instance Method Details

#addObject

Add is somewhat of a special case, everything else hangs off config for it’s magic, However we’re forced to do it manually here- config doesn’t know anything about it yet



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
# File 'lib/twat/actions/add.rb', line 7

def add
  endpoint = Endpoint.new(opts[:endpoint])
  v = endpoint.consumer_info.map do |key, value|
    value
  end

  oauth_options = { :site => endpoint.url }
  oauth_options.merge!(endpoint.oauth_options)

  oauth = OAuth::Consumer.new( v[0], v[1], oauth_options )
  token_request = oauth.get_request_token()
  reader.puts_above "Please authenticate the application at #{token_request.authorize_url} , then enter pin"
  pin = reader.get_line
  begin
    access_token = token_request.get_access_token(oauth_verifier: pin)
     = {
      oauth_token: access_token.token,
      oauth_token_secret: access_token.secret,
      endpoint: opts[:endpoint]
    }
    config.create_unless_exists!
    config.accounts[opts[:account]] = 
    config.save!
  rescue OAuth::Unauthorized
    reader.puts_above "Couldn't authenticate you, did you enter the pin correctly?"
  end
end

#deleteObject



4
5
6
7
8
9
10
11
# File 'lib/twat/actions/delete.rb', line 4

def delete
  if config.accounts.delete(opts[:account])
    config.save!
    puts "Successfully deleted"
  else
    puts "No such account"
  end
end

#fail_or_bailObject



59
60
61
62
63
64
65
66
# File 'lib/twat/actions/follow.rb', line 59

def fail_or_bail
  if @failcount > 2
    puts "3 consecutive failures, giving up"
  else
    @failcount += 1
    return true
  end
end

#followObject



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
# File 'lib/twat/actions/follow.rb', line 68

def follow
  twitter_auth
  failcount = 0
  @tweetstack = TweetStack.new

  # Get 5 tweets
  tweets = Twitter.home_timeline(:count => 5)
  while true do
    begin
      last_id = process_followed(tweets) if tweets.any?
      (config.polling_interval * POLLING_RESOLUTION).times do
        begin
          reader.tick
          reader.each_line { |inp| handle_input(inp) }
        rescue TweetTooLong
          puts "Too long".red
        end
        sleep 1.0/POLLING_RESOLUTION
      end
      tweets = Twitter.home_timeline(:since_id => last_id)
      failcount = 0
    rescue Interrupt
      break
    rescue Twitter::Error::ServiceUnavailable
      break unless fail_or_bail
      sleeptime = 60 * (@failcount + 1)
      reader.puts_above "#{"(__-){".red}: the fail whale has been rolled out, sleeping for #{sleeptime} seconds"
      sleep sleeptime
    rescue Errno::ECONNRESET, Errno::ETIMEDOUT, SocketError
      break unless fail_or_bail
    end
  end
end

#follow_user(user = nil) ⇒ Object



4
5
6
7
8
# File 'lib/twat/actions/follow_user.rb', line 4

def follow_user(user = nil)
  twitter_auth

  Twitter.follow(user || opts[:user])
end

#setoptionObject

Raises:



4
5
6
7
8
9
10
11
# File 'lib/twat/actions/setoption.rb', line 4

def setoption
  k, v = opts[:optval].split("=")
  raise RequiresOptVal unless v
  options = Options.new
  options.send(:"#{k}=", v)

  puts "Successfully set #{k} as #{v}"
end

#showObject



4
5
6
7
8
9
# File 'lib/twat/actions/show.rb', line 4

def show
  twitter_auth
  Twitter.home_timeline(:count => opts[:count]).reverse.each do |tweet|
    format(tweet)
  end
end

#tweetObject

Raises:



4
5
6
7
8
9
10
11
# File 'lib/twat/actions/tweet.rb', line 4

def tweet
  twitter_auth

  raise TweetTooLong if opts.msg.length > 140

  Twitter.update(opts.msg)
  #puts opts.msg
end

#updateconfigObject



4
5
6
# File 'lib/twat/actions/updateconfig.rb', line 4

def updateconfig
  config.update!
end

#user_feedObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/twat/actions/user_feed.rb', line 4

def user_feed
  twitter_auth

  begin
    Twitter.user_timeline(opts[:user], :count => opts[:count]).each do |tweet|
      format(tweet)
    end
  rescue Twitter::NotFound
    puts "#{opts[:user].bold.red} doesn't appear to be a valid user"
  end
end

#versionObject



4
5
6
# File 'lib/twat/actions/version.rb', line 4

def version
  puts "twat: #{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
end