winnie 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,34 +1,34 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- winnie (0.0.5)
4
+ winnie (0.0.6)
5
5
  json
6
6
  rest-client
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
+ ZenTest (4.5.0)
11
12
  diff-lcs (1.1.2)
12
13
  fakefs (0.2.1)
13
- json (1.4.6)
14
+ json (1.5.1)
14
15
  mime-types (1.16)
15
16
  rest-client (1.6.1)
16
17
  mime-types (>= 1.16)
17
- rspec (2.1.0)
18
- rspec-core (~> 2.1.0)
19
- rspec-expectations (~> 2.1.0)
20
- rspec-mocks (~> 2.1.0)
21
- rspec-core (2.1.0)
22
- rspec-expectations (2.1.0)
18
+ rspec (2.2.0)
19
+ rspec-core (~> 2.2)
20
+ rspec-expectations (~> 2.2)
21
+ rspec-mocks (~> 2.2)
22
+ rspec-core (2.2.1)
23
+ rspec-expectations (2.2.0)
23
24
  diff-lcs (~> 1.1.2)
24
- rspec-mocks (2.1.0)
25
+ rspec-mocks (2.2.0)
25
26
 
26
27
  PLATFORMS
27
28
  ruby
28
29
 
29
30
  DEPENDENCIES
31
+ ZenTest
30
32
  fakefs
31
- json
32
- rest-client
33
- rspec (~> 2.1.0)
33
+ rspec (~> 2.2.0)
34
34
  winnie!
data/lib/winnie.rb CHANGED
@@ -1,11 +1,12 @@
1
- # TODO: loop here, and load all commands
2
1
  require 'rubygems'
3
2
  require 'rest_client'
4
3
  require 'json'
5
4
  require 'optparse'
6
- require 'winnie/version'
7
- require 'winnie/helpers'
8
- require 'winnie/client'
9
- require 'winnie/commands/base'
10
- require 'winnie/command'
11
- Dir["#{File.dirname(__FILE__)}/winnie/commands/*"].each { |c| require c }
5
+
6
+ lib_path = File.expand_path(File.dirname(__FILE__))
7
+
8
+ %w(version helpers client command commands/base).each do |file|
9
+ require File.join(lib_path, "winnie", file)
10
+ end
11
+
12
+ Dir["#{lib_path}/winnie/commands/*"].each { |c| require c }
data/lib/winnie/client.rb CHANGED
@@ -40,10 +40,6 @@ module Winnie
40
40
  ) { |response, request| process_response(response) }
41
41
  end
42
42
 
43
- class UnauthorizedException < Exception; end
44
- class ResourceNotFoundException < Exception; end
45
- class CommandFailedException < Exception; end
46
-
47
43
  def self.version
48
44
  VERSION
49
45
  end
@@ -51,6 +47,7 @@ module Winnie
51
47
  private
52
48
 
53
49
  def self.winnie_headers
50
+ # User-Agent header is used by winnie-app to check compatibility
54
51
  {'User-Agent' => "winnie-gem-#{version}",
55
52
  'X-Ruby-Version' => RUBY_VERSION}
56
53
  end
@@ -58,7 +55,6 @@ module Winnie
58
55
  def process_response(response)
59
56
  # TODO: this fragment could look better
60
57
  if [404, 500, 401].include?(response.code)
61
-
62
58
  exception = case response.code
63
59
  when 404; ResourceNotFoundException
64
60
  when 500; CommandFailedException
@@ -69,13 +65,18 @@ module Winnie
69
65
  raise exception.new(error)
70
66
  end
71
67
 
72
- response.return!
73
-
74
- if response.code == 302 and response.args[:url] =~ /user_session/
75
- raise UnauthorizedException.new
68
+ begin
69
+ response.return!
70
+ JSON.parse(response.body)
71
+ rescue RestClient::RequestFailed => e
72
+ raise UnauthorizedException.new if e.http_code == 406
73
+ raise UnsupportedException.new
76
74
  end
77
-
78
- JSON.parse(response.body)
79
75
  end
76
+
77
+ class UnauthorizedException < Exception; end
78
+ class ResourceNotFoundException < Exception; end
79
+ class CommandFailedException < Exception; end
80
+ class UnsupportedException < Exception; end
80
81
  end
81
82
  end
@@ -4,12 +4,13 @@ module Winnie
4
4
  def run
5
5
  list
6
6
  end
7
-
7
+
8
8
  def command
9
- response = winnie.command(args.first, code_name)
9
+ command = File.exists?(args.first) ? File.read(args.first) : args.first
10
+ response = winnie.command(command, code_name)
10
11
  display response['result']
11
12
  end
12
-
13
+
13
14
  def list
14
15
  apps = winnie.apps
15
16
 
@@ -2,34 +2,34 @@ module Winnie
2
2
  module Commands
3
3
  class Base
4
4
  attr_reader :winnie, :args
5
-
5
+
6
6
  include Winnie::Helpers
7
-
7
+
8
8
  def initialize(*args)
9
9
  @args = args.flatten
10
10
  extract_options(@args)
11
11
  end
12
-
12
+
13
13
  def extract_options(args)
14
14
  return if args.empty?
15
-
15
+
16
16
  OptionParser.new do |opts|
17
17
  opts.on("-a", "--app [CODE_NAME]", :text, "Run for given application") do |a|
18
18
  @code_name = a
19
19
  end
20
20
  end.parse!(@args)
21
21
  end
22
-
22
+
23
23
  def code_name
24
24
  raise ApplicationNotSpecyfiedException.new unless @code_name
25
25
  @code_name
26
26
  end
27
-
27
+
28
28
  def winnie
29
29
  auth = Winnie::Commands::Auth.new
30
30
  @winnie ||= Winnie::Client.new(auth.api_key)
31
31
  end
32
-
32
+
33
33
  def config_path
34
34
  File.expand_path('~/.winnie')
35
35
  end
@@ -3,7 +3,7 @@ module Winnie
3
3
  class Help < Base
4
4
  def run
5
5
  display_columns 'auth', 'Configure your winnie username and password'
6
- display_columns 'command', 'Run single ruby command'
6
+ display_columns 'command <command or file>', 'Run ruby command, a file with commands can be given'
7
7
  display_columns 'list', 'List your applications'
8
8
  display_columns 'help', 'Show this information'
9
9
  end
@@ -4,25 +4,25 @@ module Winnie
4
4
  STDOUT << message
5
5
  STDOUT << "\n" if new_line
6
6
  end
7
-
7
+
8
8
  def display_columns(*columns)
9
- columns.each { |field| display(field.to_s.ljust(20), false) }
9
+ columns.each { |field| display(field.to_s.ljust(30), false) }
10
10
  display ''
11
11
  end
12
-
12
+
13
13
  def line
14
14
  display '-' * 45
15
15
  end
16
-
16
+
17
17
  def error(message)
18
18
  STDERR << message << "\n"
19
19
  exit 1
20
20
  end
21
-
21
+
22
22
  def ask
23
23
  gets.strip
24
24
  end
25
-
25
+
26
26
  def confirm(message)
27
27
  loop do
28
28
  STDERR << message << " (Y/N): "
@@ -1,3 +1,3 @@
1
1
  module Winnie
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/spec/client_spec.rb CHANGED
@@ -60,38 +60,59 @@ describe Winnie::Client do
60
60
  @client.get('/api/account')
61
61
  end
62
62
 
63
- it "should raise UnauthorizedException when response has 401 code" do
64
- @response.stub(:code).and_return(401)
65
- lambda {
66
- @client.get('/api/account')
67
- }.should raise_error(Winnie::Client::UnauthorizedException)
63
+ context "on 401 response code" do
64
+ it "should raise UnauthorizedException" do
65
+ @response.stub(:code).and_return(401)
66
+ lambda {
67
+ @client.get('/api/account')
68
+ }.should raise_error(Winnie::Client::UnauthorizedException)
69
+ end
68
70
  end
69
71
 
70
- it "should raise UnauthorizedException when it gets redirected to login page" do
71
- @response.stub(:args).and_return({:url => 'user_session'})
72
- @response.stub(:code).and_return(302)
72
+ context "on 406 response code" do
73
+ it "should raise UnauthorizedException" do
74
+ exception = RestClient::RequestFailed.new
75
+ exception.stub(:http_code).and_return(406)
76
+ @response.should_receive(:return!).and_raise(exception)
73
77
 
74
- lambda {
75
- @client.get('/api/account')
76
- }.should raise_error(Winnie::Client::UnauthorizedException)
78
+ lambda {
79
+ @client.get('/api/account')
80
+ }.should raise_error(Winnie::Client::UnauthorizedException)
81
+ end
77
82
  end
78
83
 
79
- it "should raise ResourceNotFoundException when response has 404 code" do
80
- @response.stub(:code).and_return(404)
81
- @response.stub(:body).and_return({'error' => 'App not found'}.to_json)
84
+ context "on 404 response code" do
85
+ it "should raise ResourceNotFoundException" do
86
+ @response.stub(:code).and_return(404)
87
+ @response.stub(:body).and_return({'error' => 'App not found'}.to_json)
82
88
 
83
- lambda {
84
- @client.post('/api/apps/flower/command', :body => 'puts User.count')
85
- }.should raise_error(Winnie::Client::ResourceNotFoundException, 'App not found')
89
+ lambda {
90
+ @client.post('/api/apps/flower/command', :body => 'puts User.count')
91
+ }.should raise_error(Winnie::Client::ResourceNotFoundException, 'App not found')
92
+ end
86
93
  end
87
94
 
88
- it "should raise CommandFailedException when response has 500 code" do
89
- @response.stub(:code).and_return(500)
90
- @response.stub(:body).and_return({'error' => 'random error happened'}.to_json)
95
+ context "on 500 response code" do
96
+ it "should raise CommandFailedException" do
97
+ @response.stub(:code).and_return(500)
98
+ @response.stub(:body).and_return({'error' => 'random error happened'}.to_json)
91
99
 
92
- lambda {
93
- @client.post('/api/apps/flower/command', :body => 'puts User.count')
94
- }.should raise_error(Winnie::Client::CommandFailedException, 'random error happened')
100
+ lambda {
101
+ @client.post('/api/apps/flower/command', :body => 'puts User.count')
102
+ }.should raise_error(Winnie::Client::CommandFailedException, 'random error happened')
103
+ end
104
+ end
105
+
106
+ context "on unsupported response code" do
107
+ it "should raise UnknownResponse exception" do
108
+ exception = RestClient::RequestFailed.new
109
+ exception.stub(:http_code).and_return(409)
110
+ @response.should_receive(:return!).and_raise(exception)
111
+
112
+ lambda {
113
+ @client.get('/api/account')
114
+ }.should raise_error(Winnie::Client::UnsupportedException)
115
+ end
95
116
  end
96
117
  end
97
118
 
data/spec/command_spec.rb CHANGED
@@ -7,7 +7,7 @@ describe Winnie::Command do
7
7
  @command.stub!(:respond_to?).and_return(true)
8
8
  Winnie::Commands::Help.stub!(:new).and_return(@command)
9
9
  end
10
-
10
+
11
11
  it "should create command class object" do
12
12
  Winnie::Command.should_receive(:eval).with('Winnie::Commands::Help').and_return(Winnie::Commands::Help)
13
13
  Winnie::Command.run('help')
@@ -20,7 +20,7 @@ describe Winnie::Command do
20
20
  Winnie::Command.run('help:connection')
21
21
  end
22
22
  end
23
-
23
+
24
24
  describe "and command name is not provided" do
25
25
  it "should invoke run method" do
26
26
  @command.should_receive(:run).and_return(true)
@@ -28,34 +28,34 @@ describe Winnie::Command do
28
28
  end
29
29
  end
30
30
  end
31
-
31
+
32
32
  describe "when command class doesn't exist" do
33
33
  before do
34
34
  @app = mock('Winnie::Commands::App', :list => [])
35
35
  Winnie::Commands::App.stub(:new).and_return(@app)
36
36
  end
37
-
37
+
38
38
  it "should use App command class as default" do
39
39
  Winnie::Commands::App.should_receive(:new).and_return(@app)
40
40
  Winnie::Command.run('list')
41
41
  end
42
-
42
+
43
43
  it "should use command class name as command name" do
44
44
  @app.should_receive(:list)
45
45
  Winnie::Command.run('list')
46
- end
46
+ end
47
47
  end
48
-
48
+
49
49
  it "should notify user when resource doesn't exist" do
50
50
  app = mock(Winnie::Commands::App)
51
51
  app.should_receive(:command).and_raise(Winnie::Client::ResourceNotFoundException.new('App not found'))
52
52
  Winnie::Commands::App.stub(:new).and_return(app)
53
-
53
+
54
54
  Winnie::Command.should_receive(:error).with('App not found')
55
-
55
+
56
56
  Winnie::Command.run('command', ["'puts User.count'", '--app', 'flower'])
57
57
  end
58
-
58
+
59
59
  it "should notify user when command doesn't exist" do
60
60
  Winnie::Command.should_receive(:error).with('Unknown command')
61
61
  Winnie::Command.run('unknown')
@@ -2,13 +2,14 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe Winnie::Commands::App do
4
4
  before do
5
- @app = Winnie::Commands::App.new('--app', 'flower')
5
+ @client = mock(Winnie::Client)
6
+ @app = Winnie::Commands::App.new('--app', 'flower', 'Product.cleanup_cache!')
7
+ @app.stub(:winnie).and_return(@client)
6
8
  @app.stub(:display_columns)
9
+ @app.stub(:display)
7
10
  @app.stub(:line)
8
- @client = mock(Winnie::Client.new('secret-api-key'))
9
- @app.stub(:winnie).and_return(@client)
10
11
  end
11
-
12
+
12
13
  describe "when user has some apps" do
13
14
  it "should list apps" do
14
15
  @client.should_receive(:apps).and_return(apps)
@@ -17,9 +18,9 @@ describe Winnie::Commands::App do
17
18
  @app.list
18
19
  end
19
20
  end
20
-
21
+
21
22
  describe "when user doesn't have any app" do
22
- it "should inform the user that he doesn't have any app" do
23
+ it "should inform the user that he doesn't have any apps" do
23
24
  @client.should_receive(:apps).and_return([])
24
25
  @app.should_receive(:display).with("You don't have any apps yet")
25
26
  @app.list
@@ -27,23 +28,34 @@ describe Winnie::Commands::App do
27
28
  end
28
29
 
29
30
  describe "running commands" do
30
- before do
31
- @app = Winnie::Commands::App.new('--app', 'shop-16', "Product.cleanup_cache!")
32
- @app.stub(:display_columns)
33
- @app.stub(:display)
34
- @app.stub(:line)
35
- @app.stub(:winnie).and_return(@client)
36
- end
37
-
38
- it "should send command to winnie given as a command line parameter" do
39
- @client.should_receive(:command).with('Product.cleanup_cache!', 'shop-16').and_return({})
40
- @app.command
31
+ context "command given by command line parameter" do
32
+ it "should send command to winnie given as a command line parameter" do
33
+ @client.should_receive(:command).with('Product.cleanup_cache!', 'flower').and_return({})
34
+ @app.command
35
+ end
36
+
37
+ it "should display the result" do
38
+ @client.should_receive(:command).with('Product.cleanup_cache!', 'flower').and_return('result' => 'foo')
39
+ @app.should_receive(:display).with('foo')
40
+ @app.command
41
+ end
41
42
  end
42
-
43
- it "should display the result" do
44
- @client.should_receive(:command).with('Product.cleanup_cache!', 'shop-16').and_return('result' => 'foo')
45
- @app.should_receive(:display).with('foo')
46
- @app.command
43
+
44
+ context "command given by a file" do
45
+ include FakeFS::SpecHelpers
46
+ before do
47
+ File.open('test-command.rb', 'w') { |f| f << '10 + 10' }
48
+ @app = Winnie::Commands::App.new('--app', 'flower', 'test-command.rb')
49
+ @app.stub(:winnie).and_return(@client)
50
+ @app.stub(:display_columns)
51
+ @app.stub(:display)
52
+ @app.stub(:line)
53
+ end
54
+
55
+ it "should read the file and execute the command" do
56
+ @client.should_receive(:command).with('10 + 10', 'flower').and_return({})
57
+ @app.command
58
+ end
47
59
  end
48
60
  end
49
61
  end
@@ -6,7 +6,7 @@ describe Winnie::Commands::Base do
6
6
  Winnie::Commands::Base.new.config_path.should == File.expand_path('~/.winnie')
7
7
  end
8
8
  end
9
-
9
+
10
10
  describe "extract_options" do
11
11
  it "should extract app code name if provided" do
12
12
  base = Winnie::Commands::Base.new('--app', 'crazy-one')
data/winnie.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_dependency "json"
23
23
  s.add_dependency "rest-client"
24
- s.add_development_dependency "rspec", "~> 2.1.0"
24
+ s.add_development_dependency "rspec", "~> 2.2.0"
25
25
  s.add_development_dependency "fakefs"
26
+ s.add_development_dependency "ZenTest"
26
27
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: winnie
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
5
- prerelease: false
4
+ hash: 19
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - winnie
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-19 00:00:00 +01:00
18
+ date: 2011-02-23 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -54,12 +54,12 @@ dependencies:
54
54
  requirements:
55
55
  - - ~>
56
56
  - !ruby/object:Gem::Version
57
- hash: 11
57
+ hash: 7
58
58
  segments:
59
59
  - 2
60
- - 1
60
+ - 2
61
61
  - 0
62
- version: 2.1.0
62
+ version: 2.2.0
63
63
  type: :development
64
64
  version_requirements: *id003
65
65
  - !ruby/object:Gem::Dependency
@@ -76,6 +76,20 @@ dependencies:
76
76
  version: "0"
77
77
  type: :development
78
78
  version_requirements: *id004
79
+ - !ruby/object:Gem::Dependency
80
+ name: ZenTest
81
+ prerelease: false
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ type: :development
92
+ version_requirements: *id005
79
93
  description: Command line tool which allows interacting with winnie's API
80
94
  email:
81
95
  - winnie-devs@ragnarson.com
@@ -140,9 +154,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
154
  requirements: []
141
155
 
142
156
  rubyforge_project: winnie
143
- rubygems_version: 1.3.7
157
+ rubygems_version: 1.5.0
144
158
  signing_key:
145
159
  specification_version: 3
146
160
  summary: Winnie command line tool
147
- test_files: []
148
-
161
+ test_files:
162
+ - spec/client_spec.rb
163
+ - spec/command_spec.rb
164
+ - spec/commands/app_spec.rb
165
+ - spec/commands/auth_spec.rb
166
+ - spec/commands/base_spec.rb
167
+ - spec/spec_helper.rb
168
+ - spec/winnie_spec.rb