Class: FxTwitter::TwitterWindow

Inherits:
Fox::FXMainWindow
  • Object
show all
Includes:
Fox
Defined in:
lib/fxtwitter.rb

Overview

The primary user interface for fxtwitter

Instance Method Summary collapse

Constructor Details

#initialize(app, username, password) ⇒ TwitterWindow

Returns a new instance of TwitterWindow.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
# File 'lib/fxtwitter.rb', line 17

def initialize(app, username, password)
  super(app, "fxtwitter", nil, nil, 
        :opts  => DECOR_TITLE |  DECOR_CLOSE | DECOR_RESIZE, 
        :width => 480)
  @username = username
  @password = password
    
  # Put together UI elements
  @tweet_display = FXText.new(self,
                               :opts => LAYOUT_FIX_WIDTH | LAYOUT_FIX_HEIGHT | 
                                       LAYOUT_FILL_X | LAYOUT_FILL_Y |
                                       TEXT_READONLY | TEXT_WORDWRAP,
                               :width => 480, :height => 200)

  @control_frame = FXHorizontalFrame.new(self)
  @quit_btn      = FXButton.new(@control_frame, "Quit")
  @refresh_btn   = FXButton.new(@control_frame, "Refresh")
  @post_btn      = FXButton.new(@control_frame, "Post")
  FXHorizontalSeparator.new(@control_frame)
  @length_lbl    = FXLabel.new(@control_frame, TWEET_LENGTH.to_s)
  @twitter_input = FXTextField.new(self, 80,
                                   :opts => LAYOUT_FILL_X | TEXTFIELD_ENTER_ONLY,
                                   :width => 450)
  @status_line   = FXStatusLine.new(self)
  @status_line.normalText = "fxtwitter"

  @post_btn.connect(SEL_COMMAND) do |sender, selector, data|
    post_tweet()
  end

  @quit_btn.connect(SEL_COMMAND) do |sender, selecter, data|
    quit_application()
  end

  @refresh_btn.connect(SEL_COMMAND) do |sender, selector, data|
    update_tweets()
  end

  @twitter_input.connect(SEL_CHANGED) do |sender, selector, data|
    @length_lbl.text = (TWEET_LENGTH - data.length).to_s
  end

  @twitter_input.connect(SEL_COMMAND) do |sender, selector, data|
    post_tweet()
  end

  # Set up text styles for the twitter display
  name_style = FXHiliteStyle.from_text(@tweet_display)
  name_style.style = FXText::STYLE_BOLD
  time_style = FXHiliteStyle.from_text(@tweet_display)
  time_style.normalForeColor = FXColor::MidnightBlue
  @tweet_display.styled = true
  @tweet_display.hiliteStyles = [ name_style, time_style ]

  # TODO: Replace global constant with config and/or user input.
  @tweets_updated_at = nil
  @tweets = []
end

Instance Method Details

#connect_to_twitter(login, password) ⇒ Object

Opens a validated connection to the Twitter service.



77
78
79
80
# File 'lib/fxtwitter.rb', line 77

def connect_to_twitter(, password)
  @status_line.text = "Connecting #{} to Twitter"
  return Twitter::Client.new(:login => , :password => password)
end

#createObject

Part of the GUI construction code



83
84
85
86
87
# File 'lib/fxtwitter.rb', line 83

def create()
  super
  show(PLACEMENT_SCREEN)
  setup_twitter()
end

#post_tweetObject

Submit user text from twitter_input



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fxtwitter.rb', line 90

def post_tweet()
  tweet_text = @twitter_input.text
  if tweet_text.length > 0 then
    if tweet_text.length <= TWEET_LENGTH then
      @status_line.text = "Posting Tweet..."
      status = Twitter::Status.create(:text => tweet_text, 
                                      :client => @twitter)
      @twitter_input.text = ""
      @length_lbl.text = TWEET_LENGTH.to_s
    else
      message = "Your Tweet must have #{TWEET_LENGTH} or less characters"
      FXMessageBox.warning(self, MBOX_OK, "Tweet too long!", message)
    end
  end
end

#quit_applicationObject

Perform any needed cleanup and exit



107
108
109
# File 'lib/fxtwitter.rb', line 107

def quit_application()
  exit
end

#setup_twitterObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fxtwitter.rb', line 111

def setup_twitter()
  if @username and @password then
    @twitter = connect_to_twitter(@username, @password)
    update_tweets()

    # Check for new Tweets every minute
    timeout_duration = 60 * 1000
    @refresh_timeout = app.addTimeout(timeout_duration, :repeat => true) do |sender, selector, data|
      update_tweets()
    end
  else
    # TODO: Login view
    raise "Username and password required. fxtwitter --help for usage"
  end
end

#update_tweetsObject



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
# File 'lib/fxtwitter.rb', line 127

def update_tweets()
  this_load = Time.now
  @status_line.text = "#{this_load}: Updating Tweets..."
  begin
    if @tweets and @tweets_updated_at then
      tweets = @twitter.timeline_for(:friends, :since => @tweets_updated_at)
      @tweets_updated_at = Time.now
    else
      tweets = @twitter.timeline_for(:friends)
      @tweets_updated_at = this_load
    end
  rescue Twitter::RESTError => e
    if e.message == "Not Modified" then
      @status_line.normalText = "#{this_load}: No new Tweets"
      return
    else
      raise e
    end
  end
  @tweets = tweets
  @tweets_updated_at = this_load
  @status_line.normalText = "#{@tweets_updated_at}: #{@tweets.length} new Tweets"

  # Now that we have new Tweets, display them.
  @tweets.reverse.each do |tweet|
    created_at = tweet.created_at.strftime("%I:%M %p")
    screen_name = CGI::unescapeHTML(tweet.user.screen_name)
    text = ": #{CGI::unescapeHTML(tweet.text)} -- "
    @tweet_display.appendStyledText(screen_name, 1)
    @tweet_display.appendText(text)
    @tweet_display.appendStyledText(created_at, 2)
    @tweet_display.appendStyledText("\n")
    @tweet_display.makePositionVisible(@tweet_display.cursorPos)
  end
end