Class: TwitterClone::Client

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

Constant Summary collapse

SUB_COMMANDS =
%w(show create delete)

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



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

def initialize
  @opts = Trollop::options do
    banner "Twitter clone RESTful client\nAvailable commands: show, create, delete"
    stop_on SUB_COMMANDS
  end

  @cmd = ARGV.shift # get the subcommand
  @cmd_opts = case @cmd
              when "show"
                Trollop::options do
                  opt :user, "User name or id that you want to show tweets for", :type => :string
                end
              when "create"
                Trollop::options do
                  opt :login, "Login", :type => :string, :required => true
                  opt :password, "Password", :type => :string, :required => true
                  opt :text, "Your status", :type => :string, :required => true
                end
              when "delete"
                Trollop::options do
                  opt :login, "Login", :type => :string, :required => true
                  opt :password, "Password", :type => :string, :required => true
                  opt :id, "ID of the tweet you want to delete", :type => :int, :required => true
                end
              else
                Trollop::die "unknown subcommand #{@cmd.inspect}"
              end
        
  self.send(@cmd) # execute the subcommand
end

Instance Method Details

#createObject



66
67
68
69
70
71
72
73
74
# File 'lib/twitter_clone_client.rb', line 66

def create
  setup_credentials
  begin
    Status.create(:body => @cmd_opts[:text], :user_id => @cmd_opts[:login])
    puts "Status created successfully"
  rescue ActiveResource::UnauthorizedAccess => e
    puts e.message and exit
  end
end

#deleteObject



76
77
78
79
80
81
82
83
84
# File 'lib/twitter_clone_client.rb', line 76

def delete
  setup_credentials
  begin
    Status.delete(@cmd_opts[:id], :user_id => @cmd_opts[:login])
    puts "Status destroy successfully"
  rescue ActiveResource::UnauthorizedAccess => e
    puts e.message and exit
  end
end

#showObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/twitter_clone_client.rb', line 53

def show
  if @cmd_opts[:user]
    params = {:user_id => @cmd_opts[:user]}
  else
    params = {}
    Status.prefix = "/"
  end
  @statuses = Status.find(:all, :params => params)
  @statuses.each do |status|
    puts status
  end
end