solve-cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/solve +143 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 648ba3344ed388ff6b72c0db5bcfec4b71b6fa0d
4
+ data.tar.gz: 092329e0b8f080e920e12b5fbafec39ab93d5c6b
5
+ SHA512:
6
+ metadata.gz: 74e31be0f8cc65c5c5c08c8c15e9f126dd714c8d22f90246d652fa4b40e9eaf169a9b32bb7023ac186b2c971dc6004c1e2788d05b65ebaf1ffa1e76931b7e3af
7
+ data.tar.gz: 8ed057226c7cdfd6af099913a4ec7e07ca365eaeda0911274332b712e32d1b46f524adf231a5be6ca8aba947f6c55978948cb2e0548b9fef09f91e9606c68c15
data/bin/solve ADDED
@@ -0,0 +1,143 @@
1
+ #!/usr/bin/env ruby
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+
6
+ API = 'http://solve.enflick.com'
7
+ CONFIG_PATH = File.expand_path('~/.solverc')
8
+
9
+ def print_usage
10
+ puts "Usage: solve <command> ...\n\n"
11
+ puts "register <username> <email>\t\tRegister a new solve account"
12
+ puts "link <username> <api_key>\t\tLink an existing solve account"
13
+ puts "key\t\t\t\t\tShow your api key"
14
+ puts "login\t\t\t\t\tLogin to site"
15
+ puts "problem <problem_id> <language> [file]\tSubmit a solution to a problem (code for the solution is expected in STDIN or as a file path on command line)"
16
+ exit
17
+ end
18
+
19
+ if ARGV.count == 0
20
+ print_usage
21
+ exit
22
+ end
23
+
24
+ command = ARGV.shift
25
+ if !['register', 'link', 'key', 'login', 'problem'].include?(command)
26
+ puts "Unknown command '#{command}'"
27
+ print_usage
28
+ end
29
+
30
+ api_key = nil
31
+ if File.exists?(CONFIG_PATH)
32
+ api_key = File.read(CONFIG_PATH).strip
33
+ unless api_key.empty?
34
+ res = Net::HTTP.get_response(URI("#{API}/users/key/#{api_key}"))
35
+ if res.code.to_i != 200
36
+ puts "The configured account does not exist, resetting configuration"
37
+ puts "Currently configured api key: #{api_key}"
38
+ puts "Please link a valid account or register another account"
39
+ File.delete(CONFIG_PATH)
40
+ exit
41
+ end
42
+ end
43
+ end
44
+
45
+ if (api_key.nil? || api_key.empty?) && !['register', 'link'].include?(command)
46
+ puts 'You do not appear to have a solve account configured.'
47
+ puts 'To register an account:'
48
+ puts 'solve register <username> <email>'
49
+ puts 'To link an existing account:'
50
+ puts 'solve link <username> <api_key>'
51
+ exit
52
+ end
53
+
54
+ case command
55
+ when 'key'
56
+ puts "Your API key: #{api_key}"
57
+ when 'login'
58
+ `open #{API}/users/login_cli?api_key=#{api_key}`
59
+ when 'problem'
60
+ unless ARGV.count >= 2
61
+ puts "Invalid command"
62
+ puts "Usage: solve problem <problem_id> <language> [file]"
63
+ exit
64
+ end
65
+
66
+ problem_id = ARGV.shift
67
+ language = ARGV.shift
68
+
69
+ code = ARGF.read
70
+
71
+ uri = URI("#{API}/problems/#{problem_id}/solutions.json")
72
+ req = Net::HTTP::Post.new uri, {
73
+ 'Content-Type' => 'application/json',
74
+ 'Accept' => 'application/json'
75
+ }
76
+
77
+ req.body = {
78
+ 'api_key' => api_key,
79
+ 'solution' => {
80
+ 'language' => language,
81
+ 'code' => code
82
+ }
83
+ }.to_json
84
+
85
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
86
+ http.request req
87
+ end
88
+
89
+ case res.code.to_i
90
+ when 201
91
+ puts "Submitted solution, and queued to test"
92
+ when 500
93
+ puts "Error processing request"
94
+ else
95
+ puts JSON.parse(res.body)['error']
96
+ end
97
+ when 'link'
98
+ unless ARGV.count == 2
99
+ puts "Invalid Command"
100
+ puts "Usage: solve link <username> <api_key>"
101
+ exit
102
+ end
103
+
104
+ File.open(CONFIG_PATH, 'w') do |f|
105
+ f.write ARGV[1]
106
+ end
107
+ when 'register'
108
+ unless ARGV.count == 2
109
+ puts "Invalid command"
110
+ puts "Usage: solve register <username> <email>"
111
+ exit
112
+ end
113
+
114
+ username = ARGV.shift
115
+ email =ARGV.shift
116
+
117
+ uri = URI("#{API}/users.json")
118
+ req = Net::HTTP::Post.new uri, {
119
+ 'Content-Type' => 'application/json',
120
+ 'Accept' => 'application/json'
121
+ }
122
+
123
+ req.body = {
124
+ 'user' => {
125
+ 'username' => username,
126
+ 'email' => email
127
+ }
128
+ }.to_json
129
+
130
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
131
+ http.request req
132
+ end
133
+
134
+ if res.code.to_i != 201
135
+ puts "Error registering..."
136
+ else
137
+ res = JSON.parse(res.body)
138
+
139
+ File.open(CONFIG_PATH, 'w') do |f|
140
+ f.write res['api_key']
141
+ end
142
+ end
143
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solve-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nan zhong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Command line interface for solve
14
+ email: nan@nine27.com
15
+ executables:
16
+ - solve
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/solve
21
+ homepage:
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.0.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Command line interface for solve
45
+ test_files: []