hackerearth_api 0.0.2
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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hackerearth_api.gemspec +26 -0
- data/lib/hackerearth_api.rb +20 -0
- data/lib/hackerearth_api/client.rb +34 -0
- data/lib/hackerearth_api/compile_parameters.rb +39 -0
- data/lib/hackerearth_api/exception_handler.rb +4 -0
- data/lib/hackerearth_api/message.rb +22 -0
- data/lib/hackerearth_api/response.rb +28 -0
- data/lib/hackerearth_api/run_parameters.rb +29 -0
- data/lib/hackerearth_api/validation.rb +25 -0
- data/lib/hackerearth_api/version.rb +3 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5537254bf289c52e0db918429bb18a5aab7cc2d4be4bbc908fee5a5e976c0f23
|
4
|
+
data.tar.gz: 514b215aa77243bc71bb5722f687659aeb4653992e6b1098ae7daa8ba9f8ba1c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 549efd586ba13969c86551c9755c0c3c56e26990fe317854cce5c157dc4640d57c20217ca089683d8ae3f3df69218912087a9cbf2e9b6bd2a2061f48b45a90c2
|
7
|
+
data.tar.gz: 30706976ff1c5cdde6d13faec86d36242483a996cfbd7ba66a3795d5d458f7784007731f440873422fab4229da797424aa645c3404ab38f51f5c11f4f0592a4d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Amandeep Singh
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# HackerearthApi
|
2
|
+
|
3
|
+
A Ruby client for the official [Hackerearth API](https://www.hackerearth.com/docs/wiki/developers/v3/). Supports both the API endpoints. Can be used to compile and run code.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'hackerearth_api'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hackerearth_api
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Creating a Instance
|
24
|
+
To start using gem, you can create a new client instance by passing the client_secret to the constructor as
|
25
|
+
```ruby
|
26
|
+
hacker = HackerearthApi.new("[your-client-secret]")
|
27
|
+
```
|
28
|
+
|
29
|
+
### Setting Parameters
|
30
|
+
Since, the instance already has the client_secret you only need to pass rest of the parameters to make a request. 'source'(source code) and 'lang'(language) are the required parameters for both the calls, not passing them will raise an ```ExceptionHandler::InvalidParameterException``` exception.
|
31
|
+
|
32
|
+
To view the list of languages supported you can do as follows
|
33
|
+
```ruby
|
34
|
+
HackerearthApi.languages
|
35
|
+
```
|
36
|
+
|
37
|
+
'time_limit' and 'memory_limit' should be atmost 5 and 262144 respectively, any values greater than maximum value are set to the maximum value.
|
38
|
+
|
39
|
+
### Making a Request
|
40
|
+
Now you can further make compile and run calls to the API as follows by passing the parameters mentioned in the API documentation, in the form of a hash.
|
41
|
+
```ruby
|
42
|
+
hacker.compile({:source=>"puts('Hello World!')", :lang=>"RUBY"})
|
43
|
+
|
44
|
+
hacker.run({:source=>"puts('Hello World!')", :lang=>"RUBY"})
|
45
|
+
```
|
46
|
+
|
47
|
+
### Response from API
|
48
|
+
The result of both the calls is in the form of a ruby hash.
|
49
|
+
It will contain the response same as mentioned in the API documentation, along with another key in hash named "status", which will be the http status of the reponse.
|
50
|
+
|
51
|
+
### Editing the Instance
|
52
|
+
You can view or change the current objects client_secret
|
53
|
+
```ruby
|
54
|
+
# To read
|
55
|
+
hacker.client_secret_key
|
56
|
+
|
57
|
+
# To change
|
58
|
+
hacker.client_secret= [new-client-secret]
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/amandeep511997/hackerearth_api.
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hackerearth_api"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "hackerearth_api/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "hackerearth_api"
|
7
|
+
spec.version = HackerearthApi::VERSION
|
8
|
+
spec.authors = ["Amandeep Singh"]
|
9
|
+
spec.email = ["amandeep511997@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Ruby library for the Hackerearth API.}
|
12
|
+
spec.homepage = "https://github.com/amandeep511997/hackerearth_api"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_dependency "httparty", "~> 0.15.6"
|
26
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative "hackerearth_api/version"
|
2
|
+
require_relative "hackerearth_api/client"
|
3
|
+
require_relative "hackerearth_api/exception_handler"
|
4
|
+
require_relative "hackerearth_api/message"
|
5
|
+
require_relative "hackerearth_api/validation"
|
6
|
+
|
7
|
+
module HackerearthApi
|
8
|
+
|
9
|
+
def self.new(*args)
|
10
|
+
if args.length > 1
|
11
|
+
raise(ExceptionHandler::InvalidParameterException, Message.invalid_argument_length(1, args.length))
|
12
|
+
end
|
13
|
+
Client.new(args[0])
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.languages
|
17
|
+
return Validation.supported_languages
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'exception_handler'
|
2
|
+
require_relative 'message'
|
3
|
+
require_relative 'compile_parameters'
|
4
|
+
require_relative 'run_parameters'
|
5
|
+
require_relative 'response'
|
6
|
+
|
7
|
+
class Client
|
8
|
+
|
9
|
+
attr_accessor :client_secret_key
|
10
|
+
|
11
|
+
def initialize(client_secret_key)
|
12
|
+
if client_secret_key == nil
|
13
|
+
raise(ExceptionHandler::InvalidParameterException, Message.client_secret_nil)
|
14
|
+
end
|
15
|
+
@client_secret_key = client_secret_key
|
16
|
+
end
|
17
|
+
|
18
|
+
# compiles the code of the current object
|
19
|
+
# params must be a hash
|
20
|
+
def compile(params)
|
21
|
+
filtered_parameters = CompileParameters.get_params(params)
|
22
|
+
filtered_parameters[:client_secret] = @client_secret_key
|
23
|
+
Response.compile(filtered_parameters)
|
24
|
+
end
|
25
|
+
|
26
|
+
# run the code of the current object
|
27
|
+
# params must be a hash
|
28
|
+
def run(params)
|
29
|
+
filtered_parameters = RunParameters.get_params(params)
|
30
|
+
filtered_parameters[:client_secret] = @client_secret_key
|
31
|
+
Response.run(filtered_parameters)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative "validation"
|
2
|
+
|
3
|
+
class CompileParameters
|
4
|
+
|
5
|
+
def self.get_params(params)
|
6
|
+
|
7
|
+
filtered_params = {}
|
8
|
+
|
9
|
+
# Required parameters
|
10
|
+
unless params.key?(:source)
|
11
|
+
raise(ExceptionHandler::InvalidParameterException, Message.source_nil)
|
12
|
+
end
|
13
|
+
filtered_params[:source] = params[:source]
|
14
|
+
|
15
|
+
unless params.key?(:lang)
|
16
|
+
raise(ExceptionHandler::InvalidParameterException, Message.lang_nil)
|
17
|
+
end
|
18
|
+
unless Validation.on_language(params[:lang])
|
19
|
+
raise(ExceptionHandler::InvalidParameterException, Message.language_not_supported)
|
20
|
+
end
|
21
|
+
filtered_params[:lang] = params[:lang]
|
22
|
+
|
23
|
+
# Optional Parameters
|
24
|
+
if params.key?(:async)
|
25
|
+
filtered_params[:async] = params[:async]
|
26
|
+
end
|
27
|
+
|
28
|
+
if params.key?(:id)
|
29
|
+
filtered_params[:id] = params[:id]
|
30
|
+
end
|
31
|
+
|
32
|
+
if params.key?(:callback)
|
33
|
+
filtered_params[:callback] = params[:callback]
|
34
|
+
end
|
35
|
+
|
36
|
+
return filtered_params
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Message
|
2
|
+
def self.client_secret_nil
|
3
|
+
"client_secret is needed. If you do not have a valid client_secret,
|
4
|
+
please register your client at 'https://www.hackerearth.com/api/register'"
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.source_nil
|
8
|
+
"source code is a required parameter"
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.lang_nil
|
12
|
+
"lang is a required parameter"
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.language_not_supported
|
16
|
+
"lang is either invalid or not supported"
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.invalid_argument_length(req, passed)
|
20
|
+
"wrong number of arguments passed, required #{req}, passed #{passed}"
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "httparty"
|
2
|
+
|
3
|
+
module Response
|
4
|
+
|
5
|
+
def self.compile(params)
|
6
|
+
compile_endpoint = "https://api.hackerearth.com/v3/code/compile/"
|
7
|
+
api_response = HTTParty.post(compile_endpoint, body: params)
|
8
|
+
parsed(api_response)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.run(params)
|
12
|
+
run_endpoint = "https://api.hackerearth.com/v3/code/run/"
|
13
|
+
api_response = HTTParty.post(run_endpoint, body: params)
|
14
|
+
parsed(api_response)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def self.parsed(api_response)
|
20
|
+
response = api_response.parsed_response
|
21
|
+
response["status"] = api_response.code
|
22
|
+
return response
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
=begin
|
27
|
+
HTTParty.post("https://api.hackerearth.com/v3/code/compile/", body: { :client_secret=> "3905bf1332df91f3d9fc3b505dc18701ddae39a1", :source=> "puts('Hello World')", :lang=> "RUBY"})
|
28
|
+
=end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative "validation"
|
2
|
+
require_relative "compile_parameters"
|
3
|
+
|
4
|
+
class RunParameters
|
5
|
+
|
6
|
+
def self.get_params(params)
|
7
|
+
filtered_params = CompileParameters.get_params(params)
|
8
|
+
|
9
|
+
if params.key?(:time_limit) and params[:time_limit] < 6
|
10
|
+
filtered_params[:time_limit] = params[:time_limit]
|
11
|
+
else
|
12
|
+
filtered_params[:time_limit] = 5
|
13
|
+
end
|
14
|
+
|
15
|
+
if params.key?(:memory_limit) and params[:memory_limit] < 262145
|
16
|
+
filtered_params[:memory_limit] = params[:memory_limit]
|
17
|
+
else
|
18
|
+
filtered_params[:memory_limit] = 262144
|
19
|
+
end
|
20
|
+
|
21
|
+
if params.key?(:input)
|
22
|
+
filtered_params[:input] = params[:input]
|
23
|
+
end
|
24
|
+
|
25
|
+
return filtered_params
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Validation
|
2
|
+
|
3
|
+
@@supported_languages = [
|
4
|
+
'C',
|
5
|
+
'CPP',
|
6
|
+
'CPP11',
|
7
|
+
'CLOJURE',
|
8
|
+
'CSHARP',
|
9
|
+
'JAVA',
|
10
|
+
'JAVASCRIPT',
|
11
|
+
'HASKELL',
|
12
|
+
'PERL',
|
13
|
+
'PHP',
|
14
|
+
'PYTHON',
|
15
|
+
'RUBY'
|
16
|
+
]
|
17
|
+
|
18
|
+
def self.supported_languages
|
19
|
+
@@supported_languages
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.on_language(lang)
|
23
|
+
@@supported_languages.include? lang
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hackerearth_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Amandeep Singh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: httparty
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.15.6
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.15.6
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- amandeep511997@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- hackerearth_api.gemspec
|
86
|
+
- lib/hackerearth_api.rb
|
87
|
+
- lib/hackerearth_api/client.rb
|
88
|
+
- lib/hackerearth_api/compile_parameters.rb
|
89
|
+
- lib/hackerearth_api/exception_handler.rb
|
90
|
+
- lib/hackerearth_api/message.rb
|
91
|
+
- lib/hackerearth_api/response.rb
|
92
|
+
- lib/hackerearth_api/run_parameters.rb
|
93
|
+
- lib/hackerearth_api/validation.rb
|
94
|
+
- lib/hackerearth_api/version.rb
|
95
|
+
homepage: https://github.com/amandeep511997/hackerearth_api
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.7.3
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Ruby library for the Hackerearth API.
|
119
|
+
test_files: []
|