Class: Pft::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pft/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



5
6
7
8
# File 'lib/pft/base.rb', line 5

def initialize(*args)
  options = args.pop || {}
  @oauth = Oauth.new
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



3
4
5
# File 'lib/pft/base.rb', line 3

def configuration
  @configuration
end

#messageObject

Returns the value of attribute message.



3
4
5
# File 'lib/pft/base.rb', line 3

def message
  @message
end

#oauthObject

Returns the value of attribute oauth.



3
4
5
# File 'lib/pft/base.rb', line 3

def oauth
  @oauth
end

Class Method Details

.inputObject



102
103
104
# File 'lib/pft/base.rb', line 102

def self.input
  @@input ||= IO.new(2,"r")
end

.options(file = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/pft/base.rb', line 85

def self.options(file=nil)
  @@options ||= begin
    @@file = File.expand_path(file || "~/.pftrc")
    configuration = []
    File.open(@@file).each do |line|
      configuration << [$1.to_sym, $2.chomp] if line =~ /(\w+)=(.+)/
    end rescue nil
    Hash[*configuration.flatten]
  end
end

.tweet(*options) ⇒ Object



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
# File 'lib/pft/base.rb', line 52

def self.tweet(*options)
  base = self.new
  message = options.pop || []
  message = base.parse_urls(message.join(" "))
  options = options.pop || {}
  
  if options[:authorize]
    base.oauth.authorize
  elsif options[:reply]
    puts base.reply_to(options[:reply], message)
  elsif options[:rt]
    puts base.retweet(options[:rt])
  elsif options[:dm]
    puts base.direct_message(options[:dm], message)
  elsif message.size > 0
    base.oauth.client.update(message)
    puts base.message_for('tweet')
  else
    raise "Please provide a message"
      # class HighLine
      #   public :get_character
      # end
      # input = HighLine.new
      # while (c = input.get_character) != ?\e do
      #   puts "You typed #{c.chr.inspect}"
      # end
  end
end

.versionObject



81
82
83
# File 'lib/pft/base.rb', line 81

def self.version
  "0.1"
end

.write_optionsObject



96
97
98
99
100
# File 'lib/pft/base.rb', line 96

def self.write_options
  File.open(@@file, "w") do |f|
    options.each { |key, value| f.puts "#{key}=#{value}" }
  end
end

Instance Method Details

#direct_message(user, message) ⇒ Object



22
23
24
25
26
# File 'lib/pft/base.rb', line 22

def direct_message(user, message)
  user = get_user(user).screen_name
  @oauth.client.direct_message_create(user, message)
  message_for('direct message', user)
end

#get_user(user) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/pft/base.rb', line 28

def get_user(user)
  user = @oauth.client.user(user) rescue false
  if !user || !user.status
    raise "User or last tweet not found"
  end
  user
end

#message_for(type, user = nil) ⇒ Object



48
49
50
# File 'lib/pft/base.rb', line 48

def message_for(type, user = nil)
  "pft! successfully sent a #{type}#{" to #{user}" if user}"
end

#parse_urls(message) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pft/base.rb', line 36

def parse_urls(message)
  return message unless message.=~(/(?:http:\/\/|ftp:\/\/|https:\/\/|www\.|ftp\.[\w]+)(?:[\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])/)
  parsed_message = ""
  message.each(" ") do |word|
    if word.=~(/(?:http:\/\/|ftp:\/\/|https:\/\/|www\.|ftp\.[\w]+)(?:[\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])/)
      word = "#{TinyUrl.compact(word.delete(' '))} "
    end
    parsed_message << word
  end
  parsed_message
end

#reply_to(user, message) ⇒ Object



10
11
12
13
14
# File 'lib/pft/base.rb', line 10

def reply_to(user, message)
  user = get_user(user)
  @oauth.client.update("@#{user.screen_name} #{message}", :in_reply_to_status_id => user.status.id)
  message_for('reply', user)
end

#retweet(user) ⇒ Object



16
17
18
19
20
# File 'lib/pft/base.rb', line 16

def retweet(user)
  user = get_user(user)
  @oauth.client.update("RT @#{user.screen_name} #{user.status.text}")
  message_for('retweet', user)
end