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_missingObject

Raises:



86
87
88
# File 'lib/twat/actions.rb', line 86

def method_missing
  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

#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



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

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



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

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

#setdefaultObject



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

def setdefault
  unless config.accounts.include?(opts[:account])
    raise NoSuchAccount
  end

  config[:default] = opts[:account]
  config.save!
  puts "Successfully set #{opts[:account]} as default"
end

#showObject



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

def show
  twitter_auth
  Twitter.home_timeline.each_with_index do |tweet, idx|
    format(tweet)

    break if idx == opts[:count]
  end
end

#tweetObject



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

def tweet
  twitter_auth

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

#updateconfigObject



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

def updateconfig
  config.update!
end

#user_feedObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/twat/actions.rb', line 70

def user_feed
  twitter_auth

  begin
    Twitter.user_timeline(opts[:user]).first.tap do |tweet|
      puts "#{tweet.user.screen_name.bold.cyan}: #{tweet.text}"
    end
  rescue Twitter::NotFound
    puts "#{opts[:user].bold.red} doesn't appear to be a valid user"
  end
end

#versionObject



82
83
84
# File 'lib/twat/actions.rb', line 82

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