exercism 0.0.25 → 0.0.26

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.
@@ -6,7 +6,7 @@ require 'cli'
6
6
 
7
7
  begin
8
8
  Exercism::CLI.start
9
- rescue Exception => e
9
+ rescue StandardError => e
10
10
  puts
11
11
  puts "ERROR: Something went wrong. Exiting."
12
12
  puts e.message
@@ -21,10 +21,11 @@ Gem::Specification.new do |spec|
21
21
  spec.add_dependency "json"
22
22
  spec.add_dependency "faraday", ">= 0.8.8"
23
23
  spec.add_dependency "thor"
24
+ spec.add_dependency "launchy", "~> 2.3.0"
24
25
  spec.add_development_dependency "bundler", "~> 1.3"
25
26
  spec.add_development_dependency "minitest", '~> 5.0'
26
27
  spec.add_development_dependency "vcr", '~> 2.4'
27
- spec.add_development_dependency "fakeweb"
28
+ spec.add_development_dependency "webmock"
28
29
  spec.add_development_dependency "rake"
29
30
 
30
31
 
data/lib/cli.rb CHANGED
@@ -37,6 +37,13 @@ class Exercism
37
37
  report(assignments)
38
38
  end
39
39
 
40
+ desc "open", "Opens exercism.io in your default browser"
41
+ def open
42
+ require 'launchy'
43
+
44
+ Launchy.open("http://exercism.io")
45
+ end
46
+
40
47
  desc "peek", "Fetch upcoming assignment from exercism.io"
41
48
  method_option :host, :aliases => '-h', :default => 'http://exercism.io', :desc => 'the url of the exercism application'
42
49
  def peek
@@ -51,6 +58,7 @@ class Exercism
51
58
  method_option :ask, :aliases => '-a', :default => false, :type => :boolean, :desc => 'ask before submitting assignment'
52
59
  def submit(file)
53
60
  require 'exercism'
61
+ require 'cli/monitored_request'
54
62
 
55
63
  submission = Submission.new(file)
56
64
 
@@ -67,20 +75,23 @@ class Exercism
67
75
  end
68
76
  end
69
77
 
70
- begin
71
- response = Exercism::Api.new(options[:host], Exercism.user).submit(submission.path)
78
+ MonitoredRequest.new(api).request :submit, submission.path do |request, body|
79
+ say "Your assignment has been submitted."
80
+ url = "#{options[:host]}/#{Exercism.user.github_username}/#{body['language']}/#{body['exercise']}"
81
+ say "For feedback on your submission visit #{url}"
82
+ end
83
+ end
84
+
85
+ desc "unsubmit", "Delete the last submission"
86
+ method_option :host, :aliases => '-h', :default => 'http://exercism.io', :desc => 'the url of the exercism application'
87
+ def unsubmit
88
+ require 'exercism'
89
+ require 'cli/monitored_request'
72
90
 
73
- body = JSON.parse(response.body)
74
- if body["error"]
75
- say body["error"]
76
- else
77
- say "Your assignment has been submitted."
78
- url = "#{options[:host]}/#{Exercism.user.github_username}/#{body['language']}/#{body['exercise']}"
79
- say "For feedback on your submission visit #{url}"
91
+ MonitoredRequest.new(api).request :unsubmit do |request, body|
92
+ if response.status == 204
93
+ say "The last submission was successfully deleted."
80
94
  end
81
- rescue Exception => e
82
- puts "There was an issue with your submission."
83
- puts e.message
84
95
  end
85
96
  end
86
97
 
@@ -119,6 +130,7 @@ class Exercism
119
130
  ask("Your exercism.io API key:")
120
131
  end
121
132
 
133
+
122
134
  def project_dir
123
135
  default_dir = FileUtils.pwd
124
136
  say "What is your exercism exercises project path?"
@@ -142,6 +154,5 @@ class Exercism
142
154
  end
143
155
  end
144
156
  end
145
-
146
157
  end
147
158
  end
@@ -0,0 +1,24 @@
1
+ class Exercism
2
+ class CLI
3
+ class MonitoredRequest
4
+ attr_reader :api
5
+
6
+ def initialize(api)
7
+ @api = api
8
+ end
9
+
10
+ def request(action, *args)
11
+ begin
12
+ response = api.send(action, *args)
13
+ response_body = JSON.parse(response.body)
14
+
15
+ abort response_body["error"] if response_body["error"]
16
+
17
+ yield response, response_body
18
+ rescue Exception => e
19
+ abort "There was an issue with your request.\n#{e}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -32,13 +32,18 @@ class Exercism
32
32
  def submit(filename)
33
33
  path = File.join(filename)
34
34
  contents = File.read path
35
- response = conn.post do |req|
36
- req.url endpoint('user/assignments')
37
- req.headers['Accept'] = 'application/json'
38
- req.headers['Content-Type'] = 'application/json'
39
- req.body = {:code => contents, :key => user.key, :path => path}.to_json
40
- end
41
- response
35
+
36
+ json_request(:post, 'user/assignments', {
37
+ :key => user.key,
38
+ :code => contents,
39
+ :path => path
40
+ })
41
+ end
42
+
43
+ def unsubmit
44
+ json_request(:delete, 'user/assignments', {
45
+ :key => user.key
46
+ })
42
47
  end
43
48
 
44
49
  private
@@ -51,6 +56,17 @@ class Exercism
51
56
  save response.body
52
57
  end
53
58
 
59
+ def json_request(verb, path, body)
60
+ response = conn.send(verb) do |request|
61
+ request.url endpoint(path)
62
+ request.headers['Accept'] = 'application/json'
63
+ request.headers['Content-Type'] = 'application/json'
64
+ request.body = body.to_json
65
+ end
66
+
67
+ response
68
+ end
69
+
54
70
  def user_agent
55
71
  "github.com/kytrinyx/exercism CLI v#{Exercism::VERSION}"
56
72
  end
@@ -11,7 +11,7 @@ class Exercism
11
11
  end
12
12
 
13
13
  def path
14
- File.absolute_path(file)
14
+ File.expand_path(file)
15
15
  end
16
16
 
17
17
  def test?
@@ -1,3 +1,3 @@
1
1
  class Exercism
2
- VERSION = "0.0.25"
2
+ VERSION = "0.0.26"
3
3
  end
@@ -0,0 +1,53 @@
1
+ require './test/test_helper'
2
+ require 'cli/monitored_request'
3
+
4
+ Struct.new('Response', :body)
5
+
6
+ class MonitoredRequestTest < MiniTest::Unit::TestCase
7
+ def setup
8
+ setup_api
9
+ @monitored_request = Exercism::CLI::MonitoredRequest.new(@api)
10
+ end
11
+
12
+ def test_yield
13
+ @monitored_request.request :success_action do |response, body|
14
+ assert_equal 'Hi.', body["message"]
15
+ end
16
+ end
17
+
18
+ def test_response_errors
19
+ out, err = capture_subprocess_io do
20
+ pid = fork { @monitored_request.request :failure_action }
21
+ Process.wait pid
22
+ end
23
+
24
+ assert_match 'Error.', err
25
+ end
26
+
27
+ def test_handle_request_exceptions
28
+ out, err = capture_subprocess_io do
29
+ pid = fork { @monitored_request.request :exception_action }
30
+ Process.wait pid
31
+ end
32
+
33
+ assert_match 'There was an issue with your request.', err
34
+ end
35
+
36
+ private
37
+
38
+ def setup_api
39
+ @api = Minitest::Mock.new
40
+
41
+ def @api.failure_action
42
+ Struct::Response.new('{"error":"Error."}')
43
+ end
44
+
45
+ def @api.success_action
46
+ Struct::Response.new('{"message":"Hi."}')
47
+ end
48
+
49
+ def @api.exception_action
50
+ raise Exception
51
+ end
52
+ end
53
+ end
@@ -6,7 +6,7 @@ test_dir = File.join(FileUtils.pwd, 'test/fixtures')
6
6
 
7
7
  VCR.configure do |c|
8
8
  c.cassette_library_dir = File.join(test_dir, 'vcr_cassettes')
9
- c.hook_into :fakeweb
9
+ c.hook_into :webmock
10
10
  end
11
11
 
12
12
  require 'approvals'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exercism
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.25
4
+ version: 0.0.26
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-19 00:00:00.000000000 Z
12
+ date: 2013-09-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: launchy
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.3.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.3.0
62
78
  - !ruby/object:Gem::Dependency
63
79
  name: bundler
64
80
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +124,7 @@ dependencies:
108
124
  - !ruby/object:Gem::Version
109
125
  version: '2.4'
110
126
  - !ruby/object:Gem::Dependency
111
- name: fakeweb
127
+ name: webmock
112
128
  requirement: !ruby/object:Gem::Requirement
113
129
  none: false
114
130
  requirements:
@@ -188,6 +204,7 @@ files:
188
204
  - bin/exercism
189
205
  - exercism.gemspec
190
206
  - lib/cli.rb
207
+ - lib/cli/monitored_request.rb
191
208
  - lib/exercism.rb
192
209
  - lib/exercism/api.rb
193
210
  - lib/exercism/assignment.rb
@@ -196,6 +213,7 @@ files:
196
213
  - lib/exercism/submission.rb
197
214
  - lib/exercism/user.rb
198
215
  - lib/exercism/version.rb
216
+ - test/cli/monitored_request_test.rb
199
217
  - test/exercism/api_test.rb
200
218
  - test/exercism/assignment_test.rb
201
219
  - test/exercism/config_test.rb
@@ -238,6 +256,7 @@ specification_version: 3
238
256
  summary: This gem provides a command line client to allow users of the exercism.io
239
257
  application to easily fetch and submit assignments.
240
258
  test_files:
259
+ - test/cli/monitored_request_test.rb
241
260
  - test/exercism/api_test.rb
242
261
  - test/exercism/assignment_test.rb
243
262
  - test/exercism/config_test.rb