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

def initialize
  @opts = Trollop::options do
    banner "Twitter clone RESTful client"
    version "0.0.1"
    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



67
68
69
70
71
# File 'lib/twitter_clone_client.rb', line 67

def create
  setup_credentials
  Status.create(:body => @cmd_opts[:text], :user_id => @cmd_opts[:login])
  puts "Status created successfully"
end

#deleteObject



73
74
75
76
77
# File 'lib/twitter_clone_client.rb', line 73

def delete
  setup_credentials
  Status.delete(@cmd_opts[:id], :user_id => @cmd_opts[:login])
  puts "Status destroy successfully"
end

#showObject



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

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