Class: Twat::Actions

Inherits:
Object
  • Object
show all
Defined in:
lib/twat/actions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Raises:



128
129
130
# File 'lib/twat/actions.rb', line 128

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

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#failcountObject

Returns the value of attribute failcount.



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

def failcount
  @failcount
end

#optsObject

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#addObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/twat/actions.rb', line 17

def add
  v = Config.consumer_info.map do |key, value|
    value
  end
  # FIXME probably allow something other than 
  # twitter
  oauth = OAuth::Consumer.new( v[0], v[1],
          { site: "http://twitter.com" })
  token_request = oauth.get_request_token()
  puts "Please authenticate the application at #{token_request.authorize_url}, then enter pin"
  pin = gets.chomp
  begin
    access_token = token_request.get_access_token(oauth_verifier: pin)
    config.accounts[opts[:account]] = {
      oauth_token: access_token.token,
      oauth_token_secret: access_token.secret
    }
    config.save!
  rescue OAuth::Unauthorized
    puts "Couldn't authenticate you, did you enter the pin correctly?"
  end
end

#deleteObject



40
41
42
43
44
45
46
47
# File 'lib/twat/actions.rb', line 40

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

#followObject



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

def follow
  # I can't see any way to poll the server for updates, so in the meantime
  # we will have to retrieve a few tweets from the timeline, and then poll
  # occasionally :/
  twitter_auth
  failcount = 0

  # Get 5 tweets
  tweets = Twitter.home_timeline(:count => 5)
  while true do
    begin
      last_id = process_followed(tweets) if tweets.any?
      sleep config.polling_interval
      tweets = Twitter.home_timeline(:since_id => last_id)
      failcount = 0
    rescue Interrupt
      break
    rescue Errno::ECONNRESET
      if failcount > 2
        puts "3 consecutive failures, giving up"
      else
        failcount += 1
      end
    end
  end
end

#setoptionObject

Raises:



49
50
51
52
53
54
55
56
# File 'lib/twat/actions.rb', line 49

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



62
63
64
65
66
67
# File 'lib/twat/actions.rb', line 62

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

#tweetObject

Raises:



8
9
10
11
12
13
14
15
# File 'lib/twat/actions.rb', line 8

def tweet
  twitter_auth

  raise TweetTooLong if opts.msg.length > 140

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

#updateconfigObject



58
59
60
# File 'lib/twat/actions.rb', line 58

def updateconfig
  config.update!
end

#user_feedObject



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/twat/actions.rb', line 112

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



124
125
126
# File 'lib/twat/actions.rb', line 124

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