Class: Tweetwine::Client

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

Defined Under Namespace

Classes: StatusUpdate, StatusUpdateFactory

Constant Summary collapse

COMMANDS =
[:home, :mentions, :user, :update, :friends, :followers]
DEFAULT_NUM_STATUSES =
20
DEFAULT_PAGE_NUM =
1
MAX_STATUS_LENGTH =
140

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dependencies, options) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tweetwine/client.rb', line 14

def initialize(dependencies, options)
  @io = dependencies[:io]
  @rest_client = dependencies[:rest_client]
  @username = options[:username].to_s
  raise ArgumentError, "No authentication data given" if @username.empty?
  @base_url = "https://#{@username}:#{options[:password]}@twitter.com/"
  @num_statuses = Util.parse_int_gt(options[:num_statuses], DEFAULT_NUM_STATUSES, 1, "number of statuses_to_show")
  @page_num = Util.parse_int_gt(options[:page_num], DEFAULT_PAGE_NUM, 1, "page number")
  @url_shortener = if options[:shorten_urls] && options[:shorten_urls][:enable]
    dependencies[:url_shortener].call(options[:shorten_urls])
  else
    nil
  end
  @status_update_factory = StatusUpdateFactory.new(@io, @url_shortener)
end

Instance Attribute Details

#num_statusesObject (readonly)

Returns the value of attribute num_statuses.



6
7
8
# File 'lib/tweetwine/client.rb', line 6

def num_statuses
  @num_statuses
end

#page_numObject (readonly)

Returns the value of attribute page_num.



6
7
8
# File 'lib/tweetwine/client.rb', line 6

def page_num
  @page_num
end

Instance Method Details

#followersObject



61
62
63
# File 'lib/tweetwine/client.rb', line 61

def followers
  show_users(get_response_as_json("statuses/followers/#{@username}", :page))
end

#friendsObject



57
58
59
# File 'lib/tweetwine/client.rb', line 57

def friends
  show_users(get_response_as_json("statuses/friends/#{@username}", :page))
end

#homeObject



30
31
32
# File 'lib/tweetwine/client.rb', line 30

def home
  show_statuses(get_response_as_json("statuses/friends_timeline", :num_statuses, :page))
end

#mentionsObject



34
35
36
# File 'lib/tweetwine/client.rb', line 34

def mentions
  show_statuses(get_response_as_json("statuses/mentions", :num_statuses, :page))
end

#update(new_status = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tweetwine/client.rb', line 42

def update(new_status = nil)
  new_status = @status_update_factory.prepare(new_status)
  completed = false
  unless new_status.empty?
    @io.show_status_preview(new_status)
    if @io.confirm("Really send?")
      status = JSON.parse(post("statuses/update.json", {:status => new_status.to_s}))
      @io.info "Sent status update.\n\n"
      show_statuses([status])
      completed = true
    end
  end
  @io.info "Cancelled." unless completed
end

#user(user = @username) ⇒ Object



38
39
40
# File 'lib/tweetwine/client.rb', line 38

def user(user = @username)
  show_statuses(get_response_as_json("statuses/user_timeline/#{user}", :num_statuses, :page))
end