Class: Tweetwine::Twitter

Inherits:
Object
  • Object
show all
Defined in:
lib/tweetwine/twitter.rb

Constant Summary collapse

MAX_STATUS_LENGTH =
140
REST_API_STATUS_PATHS =
{
  :from_user  => %w{user screen_name},
  :to_user    => %w{in_reply_to_screen_name},
  :retweet    => %w{retweeted_status},
  :created_at => %w{created_at},
  :status     => %w{text}
}
REST_API_USER_PATHS =
{
  :from_user  => %w{screen_name},
  :to_user    => %w{status in_reply_to_screen_name},
  :retweet    => %w{retweeted_status},
  :created_at => %w{status created_at},
  :status     => %w{status text}
}
SEARCH_API_STATUS_PATHS =
{
  :from_user  => %w{from_user},
  :to_user    => %w{to_user},
  :retweet    => %w{retweeted_status},
  :created_at => %w{created_at},
  :status     => %w{text}
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Twitter

Returns a new instance of Twitter.



35
36
37
38
39
40
41
# File 'lib/tweetwine/twitter.rb', line 35

def initialize(options = {})
  @num_tweets = Support.parse_int_gt(options[:num_tweets], CLI::DEFAULT_CONFIG[:num_tweets], 1, "number of tweets to show")
  @page       = Support.parse_int_gt(options[:page], CLI::DEFAULT_CONFIG[:page], 1, "page number")
  @username   = options[:username].to_s
rescue ArgumentError => e
  raise CommandLineError, e
end

Instance Attribute Details

#num_tweetsObject (readonly)

Returns the value of attribute num_tweets.



33
34
35
# File 'lib/tweetwine/twitter.rb', line 33

def num_tweets
  @num_tweets
end

#pageObject (readonly)

Returns the value of attribute page.



33
34
35
# File 'lib/tweetwine/twitter.rb', line 33

def page
  @page
end

#usernameObject (readonly)

Returns the value of attribute username.



33
34
35
# File 'lib/tweetwine/twitter.rb', line 33

def username
  @username
end

Instance Method Details

#followersObject



43
44
45
# File 'lib/tweetwine/twitter.rb', line 43

def followers
  show_users_from_rest_api(get_from_rest_api("statuses/followers"))
end

#friendsObject



47
48
49
# File 'lib/tweetwine/twitter.rb', line 47

def friends
  show_users_from_rest_api(get_from_rest_api("statuses/friends"))
end

#homeObject



51
52
53
# File 'lib/tweetwine/twitter.rb', line 51

def home
  show_tweets_from_rest_api(get_from_rest_api("statuses/home_timeline"))
end

#mentionsObject



55
56
57
# File 'lib/tweetwine/twitter.rb', line 55

def mentions
  show_tweets_from_rest_api(get_from_rest_api("statuses/mentions"))
end

#search(words = [], operator = nil) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
# File 'lib/tweetwine/twitter.rb', line 59

def search(words = [], operator = nil)
  raise ArgumentError, "No search words" if words.empty?
  operator = :and unless operator
  query = operator == :and ? words.join(' ') : words.join(' OR ')
  response = get_from_search_api query
  show_tweets_from_search_api(response["results"])
end

#update(msg = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tweetwine/twitter.rb', line 67

def update(msg = nil)
  new_status = create_status_update(msg)
  completed = false
  unless new_status.empty?
    CLI.ui.show_status_preview(new_status)
    status_in_utf8 = CharacterEncoding.to_utf8 new_status
    if CLI.ui.confirm("Really send?")
      response = post_to_rest_api("statuses/update", :status => status_in_utf8)
      CLI.ui.info "Sent status update.\n\n"
      show_tweets_from_rest_api([response])
      completed = true
    end
  end
  CLI.ui.info "Cancelled." unless completed
end

#user(who = username) ⇒ Object



83
84
85
86
87
88
# File 'lib/tweetwine/twitter.rb', line 83

def user(who = username)
  show_tweets_from_rest_api(get_from_rest_api(
    "statuses/user_timeline",
    common_rest_api_query_params.merge!({ :screen_name => who })
  ))
end