codecube 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 416995da9d24911d7286cede8247327976701a50
4
+ data.tar.gz: c1643b4deca8c98a1a21182833726d4a02e8f170
5
+ SHA512:
6
+ metadata.gz: 54bff8218c1552f0da6856b3b0f7e96bd9b43f3b6b345287477aba089ecc2ef134184c918f3c1f5e931f12e0e5d9fa9c446802a0c33a5b4f7dfa721f595cf90a
7
+ data.tar.gz: c52a86212fed50602ed3be19554df3255423c37d32aa9616f057bb7b33cb805560b7c88212044215bf1711a63349ad1e84ef7b96c81b2d07759e15834c8059d0
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Harry Marr
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ # Codecube Ruby Client Library
2
+
3
+ A Ruby client library for the codecube.io API
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'codecube'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install codecube
18
+
19
+ ## Usage
20
+
21
+ Initialize with your API key:
22
+
23
+ ```ruby
24
+ CodeCube.api_key = ENV.fetch('CODECUBE_API_KEY')
25
+ ```
26
+
27
+ Run code:
28
+
29
+ ```ruby
30
+ response = CodeCube.run_sync(language: "ruby", code: "puts 'hello!'")
31
+ puts response.text_output
32
+ # => hello!
33
+ ```
34
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'codecube/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "codecube"
8
+ spec.version = Codecube::VERSION
9
+ spec.authors = ["Harry Marr"]
10
+ spec.email = ["harry.marr@gmail.com"]
11
+ spec.summary = %q{Ruby client library for codecube.io}
12
+ spec.homepage = "http://codecube.io"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "typhoeus", "~> 0.6"
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.3"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "webmock", "~> 1.18"
25
+ spec.add_development_dependency "vcr", "~> 2.9"
26
+ end
@@ -0,0 +1,49 @@
1
+ require "json"
2
+ require "uri"
3
+ require "typhoeus"
4
+ require "codecube/response"
5
+
6
+ module CodeCube
7
+ class AuthenticationError < StandardError; end
8
+ class ApiError < StandardError; end
9
+
10
+ API_BASE_URL = "http://api.codecube.io/"
11
+
12
+ class << self
13
+ attr_accessor :api_key
14
+
15
+ def run_sync(args = {})
16
+ require_args!(args, [:language, :code])
17
+ check_api_key!
18
+
19
+ response = Typhoeus.post(api_url("/sync-run/").to_s,
20
+ body: JSON.dump(args),
21
+ headers: { 'Authorization' => api_key })
22
+ case response.code
23
+ when 200 then return Response.new(JSON.parse(response.body))
24
+ when 401 then raise AuthenticationError, response.body
25
+ else raise ApiError, response.body
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def check_api_key!
32
+ raise AuthenticationError, "no api_key set" if api_key.nil?
33
+ end
34
+
35
+ def require_args!(args, required_args)
36
+ Array(required_args).each do |arg|
37
+ if args[arg].nil?
38
+ raise ArgumentError, "missing required argument '#{arg}'"
39
+ end
40
+ end
41
+ end
42
+
43
+ def api_url(path)
44
+ uri = URI(API_BASE_URL)
45
+ uri.path = path
46
+ uri
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,25 @@
1
+ module CodeCube
2
+ class Response
3
+ def initialize(data)
4
+ @data = data
5
+ end
6
+
7
+ def duration
8
+ @data["duration_ms"]
9
+ end
10
+
11
+ def timed_out?
12
+ @data["timed_out"]
13
+ end
14
+
15
+ def output
16
+ @data["output"]
17
+ end
18
+
19
+ def text_output
20
+ @data["output"].select { |l| %w(stdout stderr).include?(l["stream"]) }
21
+ .map { |l| l["body"] }
22
+ .join("\n")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Codecube
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://api.codecube.io/sync-run/
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"code":"puts 1","language":"ruby"}'
9
+ headers:
10
+ User-Agent:
11
+ - Typhoeus - https://github.com/typhoeus/typhoeus
12
+ Authorization:
13
+ - invalid-api-key
14
+ response:
15
+ status:
16
+ code: 401
17
+ message: Unauthorized
18
+ headers:
19
+ Content-Type:
20
+ - text/plain; charset=utf-8
21
+ Date:
22
+ - Wed, 16 Jul 2014 22:47:51 GMT
23
+ Content-Length:
24
+ - '34'
25
+ body:
26
+ encoding: UTF-8
27
+ string: |
28
+ invalid authorization credentials
29
+ http_version:
30
+ recorded_at: Wed, 16 Jul 2014 22:47:47 GMT
31
+ recorded_with: VCR 2.9.2
@@ -0,0 +1,41 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://api.codecube.io/sync-run/
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"code":"puts 1","language":"ruby"}'
9
+ headers:
10
+ User-Agent:
11
+ - Typhoeus - https://github.com/typhoeus/typhoeus
12
+ Authorization:
13
+ - valid-api-key
14
+ response:
15
+ status:
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ Access-Control-Allow-Headers:
20
+ - Accept, Authorization, Content-Type, Origin
21
+ Access-Control-Allow-Methods:
22
+ - GET, POST
23
+ Access-Control-Allow-Origin:
24
+ - '*'
25
+ Cache-Control:
26
+ - no-cache
27
+ X-Quota-Remaining:
28
+ - '999'
29
+ Date:
30
+ - Wed, 16 Jul 2014 22:43:58 GMT
31
+ Content-Length:
32
+ - '96'
33
+ Content-Type:
34
+ - text/plain; charset=utf-8
35
+ body:
36
+ encoding: UTF-8
37
+ string: |
38
+ {"output":[{"type":"output","body":"1","stream":"stdout"}],"timed_out":false,"duration_ms":156}
39
+ http_version:
40
+ recorded_at: Wed, 16 Jul 2014 22:43:54 GMT
41
+ recorded_with: VCR 2.9.2
@@ -0,0 +1,76 @@
1
+ require "spec_helper"
2
+
3
+ describe "CodeCube" do
4
+ describe ".api_key=" do
5
+ it "sets the api key" do
6
+ expect { CodeCube.api_key = 'foo' }.to change { CodeCube.api_key }
7
+ end
8
+ end
9
+
10
+ describe ".run_sync" do
11
+ subject { CodeCube.run_sync }
12
+
13
+ context "when required args are missing" do
14
+ it "throws an exception" do
15
+ expect { CodeCube.run_sync }.to raise_error(ArgumentError)
16
+ end
17
+ end
18
+
19
+ context "with valid params" do
20
+ let(:api_endpoint) { "http://api.codecube.io/sync-run/" }
21
+ let(:api_key) { "valid-api-key" }
22
+ let(:params) { { code: "puts 1", language: "ruby" } }
23
+ let(:run_code) do
24
+ VCR.use_cassette("run_sync", match_requests_on: [:headers]) do
25
+ CodeCube.run_sync(params)
26
+ end
27
+ end
28
+
29
+ before { CodeCube.api_key = api_key }
30
+
31
+ context "but no api key" do
32
+ let(:api_key) { nil }
33
+
34
+ it "throws an exception" do
35
+ expect { run_code }.to raise_error(CodeCube::AuthenticationError)
36
+ end
37
+ end
38
+
39
+ context "but an invalid api key" do
40
+ let(:api_key) { "invalid-api-key" }
41
+
42
+ it "throws an exception" do
43
+ expect {
44
+ VCR.use_cassette("bad_auth") do
45
+ CodeCube.run_sync(params)
46
+ end
47
+ }.to raise_error(CodeCube::AuthenticationError)
48
+ end
49
+ end
50
+
51
+ it "hits the codecube api" do
52
+ run_code
53
+ expect(WebMock).to have_requested(:post, api_endpoint)
54
+ end
55
+
56
+ it "sends a json body" do
57
+ run_code
58
+ expect(WebMock).to have_requested(:post, api_endpoint).with do |req|
59
+ JSON.parse(req.body, symbolize_names: true) == params
60
+ end
61
+ end
62
+
63
+ it "includes an auth header" do
64
+ run_code
65
+ expect(WebMock).to have_requested(:post, api_endpoint).
66
+ with(headers: { "Authorization" => /#{api_key}/ })
67
+ end
68
+
69
+ it "returns a Response object" do
70
+ response = run_code
71
+ expect(response).to be_a(CodeCube::Response)
72
+ expect(response.text_output).to eq("1")
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,8 @@
1
+ require "codecube"
2
+ require "webmock/rspec"
3
+ require "vcr"
4
+
5
+ VCR.configure do |c|
6
+ c.cassette_library_dir = "spec/cassettes"
7
+ c.hook_into :webmock
8
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codecube
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Harry Marr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.18'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.18'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '2.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '2.9'
97
+ description:
98
+ email:
99
+ - harry.marr@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - codecube-ruby.gemspec
110
+ - lib/codecube.rb
111
+ - lib/codecube/response.rb
112
+ - lib/codecube/version.rb
113
+ - spec/cassettes/bad_auth.yml
114
+ - spec/cassettes/run_sync.yml
115
+ - spec/codecube_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: http://codecube.io
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.2.2
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Ruby client library for codecube.io
141
+ test_files:
142
+ - spec/cassettes/bad_auth.yml
143
+ - spec/cassettes/run_sync.yml
144
+ - spec/codecube_spec.rb
145
+ - spec/spec_helper.rb