piston_sdk 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f0b14b2788360074d5bfbeda204336c00985fd5c02e208f278583da7451ad1da
4
+ data.tar.gz: 406925f3cb0152ab40b0e3661d778429fc499cfb45e0cc960f17454e5e8c3f69
5
+ SHA512:
6
+ metadata.gz: 412636f7247d509bf44532b557de671657702c9fc955013d89be1ba7f65c9823f8b0a87ed51cc946d4b83cbe442aafc89f2b1f2744e904eab3c53e403f95cd3a
7
+ data.tar.gz: e134bcf385bbc4fe5a737c3043409730522011cc9e5f31f0718bb9b15f671f8fff25131a675d973c6d33dd004a78b1ffcd1aa69b9b5553ea2f59945c349817c9
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Metrics/AbcSize:
5
+ Enabled: false
6
+
7
+ Metrics/BlockLength:
8
+ Enabled: false
9
+
10
+ Metrics/CyclomaticComplexity:
11
+ Enabled: false
12
+
13
+ Metrics/MethodLength:
14
+ Enabled: false
15
+
16
+ Metrics/ParameterLists:
17
+ Enabled: false
18
+
19
+ Metrics/PerceivedComplexity:
20
+ Enabled: false
21
+
22
+ Style/StringLiterals:
23
+ EnforcedStyle: double_quotes
24
+
25
+ Style/StringLiteralsInInterpolation:
26
+ EnforcedStyle: double_quotes
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Zahin Zaman
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ <p align="center">
2
+ <img alt="piston_sdk logo" src="img/logo.svg" width=125 />
3
+ </p>
4
+
5
+ <h1 align="center">
6
+ Piston Ruby SDK
7
+ </h1>
8
+
9
+ <p align="center">
10
+ <strong><i>piston_sdk</i></strong> is a lightweight Ruby gem executing code using the <a href="https://github.com/engineer-man/piston"><i>Piston API</i></a>.
11
+ </p>
12
+
13
+ <div align="center">
14
+
15
+ [![GitHub Actions](https://img.shields.io/github/actions/workflow/status/alvii147/piston-ruby-sdk/main.yml?branch=main&label=GitHub%20Actions&logo=github)](https://github.com/alvii147/piston-ruby-sdk/actions) [![License](https://img.shields.io/github/license/alvii147/piston-ruby-sdk)](https://github.com/alvii147/piston-ruby-sdk/blob/main/LICENSE)
16
+
17
+ </div>
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ gem install piston_sdk
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require 'piston_sdk'
29
+
30
+ client = PistonSDK::Client.new
31
+ client.add_file(content: "puts 42", name: "app.rb")
32
+ results = client.execute(language: "ruby", version: "3.0.1")
33
+
34
+ puts results.run.stdout # 42
35
+ ```
36
+
37
+ ## Acknowledgements
38
+
39
+ * [Piston API](https://github.com/engineer-man/piston)
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/img/logo.svg ADDED
@@ -0,0 +1,24 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 512 512">
2
+ <defs>
3
+ <style>
4
+ .cls-1{
5
+ fill: #330a00;
6
+ }
7
+
8
+ .cls-2{
9
+ fill:url(#linear-gradient);
10
+ }
11
+ </style>
12
+ <linearGradient id="linear-gradient" x1="313.84" y1="162.5" x2="158.19" y2="318.15" gradientUnits="userSpaceOnUse">
13
+ <stop offset="0" stop-color="#ff8566"/>
14
+ <stop offset="0.5" stop-color="#ff1a1a"/>
15
+ <stop offset="1" stop-color="#991f00"/>
16
+ </linearGradient>
17
+ </defs>
18
+ <g id="Layer_2" data-name="Layer 2">
19
+ <g id="Layer_4" data-name="Layer 4">
20
+ <rect class="cls-1" width="512" height="512"/>
21
+ <path class="cls-2" d="M227.17,255.54V371.65l-44.95-29.47V281.23Zm30.38,47.15,98.23-51.21v-47l-128.19-64.1L182.22,163.2v55.13l45.37-25.54,69.09,35.38-39,17.08Z"/>
22
+ </g>
23
+ </g>
24
+ </svg>
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PistonSDK
4
+ VERSION = "0.1.0"
5
+ end
data/lib/piston_sdk.rb ADDED
@@ -0,0 +1,184 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+ require_relative "piston_sdk/version"
6
+
7
+ # A minimal Ruby gem for using the [Piston API](https://github.com/engineer-man/piston).
8
+ module PistonSDK
9
+ # Data representation of a Piston runtime.
10
+ class Runtime
11
+ attr_reader :language, :version, :aliases, :runtime
12
+
13
+ # Create a new Piston runtime.
14
+ #
15
+ # @param data [Hash] JSON data.
16
+ def initialize(data)
17
+ @language = data["language"]
18
+ @version = data["version"]
19
+ @aliases = data["aliases"]
20
+ @runtime = data["runtime"]
21
+ end
22
+ end
23
+
24
+ # Data representation of Piston execution step details.
25
+ class ExecutionStepDetails
26
+ attr_reader :stdout, :stderr, :output, :code, :signal, :message, :status, :cpu_time, :wall_time, :memory
27
+
28
+ # Create new Piston execution step details.
29
+ #
30
+ # @param data [Hash] JSON data.
31
+ def initialize(data)
32
+ @stdout = data["stdout"] unless data["stdout"].nil?
33
+ @stderr = data["stderr"] unless data["stderr"].nil?
34
+ @output = data["output"] unless data["output"].nil?
35
+ @code = data["code"] unless data["code"].nil?
36
+ @signal = data["signal"] unless data["signal"].nil?
37
+ @message = data["message"] unless data["message"].nil?
38
+ @status = data["status"] unless data["status"].nil?
39
+ @cpu_time = data["cpu_time"] unless data["cpu_time"].nil?
40
+ @wall_time = data["wall_time"] unless data["wall_time"].nil?
41
+ @memory = data["memory"] unless data["memory"].nil?
42
+ end
43
+ end
44
+
45
+ # Data representation of Piston execution results.
46
+ class ExecutionResults
47
+ attr_reader :language, :version, :run, :compile
48
+
49
+ # Create new Piston execution results.
50
+ #
51
+ # @param data [Hash] JSON data.
52
+ def initialize(data)
53
+ @language = data["language"]
54
+ @version = data["version"]
55
+ @run = ExecutionStepDetails.new(data["run"]) unless data["run"].nil?
56
+ @compile = ExecutionStepDetails.new(data["compile"]) unless data["compile"].nil?
57
+ end
58
+ end
59
+
60
+ # HTTP client for the Piston API.
61
+ class Client
62
+ # Create a new client.
63
+ #
64
+ # @param base_url [String] Base URL for API.
65
+ # @param compile_timeout [Integer, nil] The maximum wall-time allowed for the compile stage to finish before bailing
66
+ # out in milliseconds.
67
+ # @param run_timeout [Integer, nil] The maximum wall-time allowed for the run stage to finish before bailing out in
68
+ # milliseconds.
69
+ # @param compile_cpu_time [Integer, nil] The maximum CPU-time allowed for the compile stage to finish before bailing
70
+ # out in milliseconds.
71
+ # @param run_cpu_time [Integer, nil] The maximum CPU-time allowed for the run stage to finish before bailing out in
72
+ # milliseconds.
73
+ # @param compile_memory_limit [Integer, nil] The maximum amount of memory the compile stage is allowed to use in
74
+ # bytes.
75
+ # @param run_memory_limit [Integer, nil] The maximum amount of memory the run stage is allowed to use in bytes.
76
+ def initialize(
77
+ base_url: "https://emkc.org/api/v2/piston",
78
+ retries: 3,
79
+ compile_timeout: nil,
80
+ run_timeout: nil,
81
+ compile_cpu_time: nil,
82
+ run_cpu_time: nil,
83
+ compile_memory_limit: nil,
84
+ run_memory_limit: nil
85
+ )
86
+ @uri = URI(base_url)
87
+ @retries = retries
88
+ @compile_timeout = compile_timeout
89
+ @run_timeout = run_timeout
90
+ @compile_cpu_time = compile_cpu_time
91
+ @run_cpu_time = run_cpu_time
92
+ @compile_memory_limit = compile_memory_limit
93
+ @run_memory_limit = run_memory_limit
94
+ @files = []
95
+
96
+ @http = Net::HTTP.new(@uri.host, @uri.port)
97
+ @http.use_ssl = true
98
+
99
+ @method_classes = Hash.new(Net::HTTP::Get)
100
+ @method_classes[:get] = Net::HTTP::Get
101
+ @method_classes[:post] = Net::HTTP::Post
102
+ end
103
+
104
+ # Perform raw HTTP request using exponential backoff on retries.
105
+ #
106
+ # @param method [Symbol] HTTP method.
107
+ # @param path [String] URL Path.
108
+ # @param body [Hash, nil] Optional request body
109
+ # @return [Hash, Array] JSON response.
110
+ def request(method: :get, path: "", body: nil)
111
+ req = @method_classes[method].new("#{@uri}#{path}")
112
+ req["Content-Type"] = "application/json"
113
+ req.body = body.to_json unless body.nil?
114
+ res = @http.request(req)
115
+
116
+ (0...@retries).each do |attempt|
117
+ res = @http.request(req)
118
+
119
+ case res
120
+ when Net::HTTPOK
121
+ return JSON.parse(res.body)
122
+ when Net::HTTPTooManyRequests
123
+ sleep 2**attempt
124
+ else
125
+ raise "Request failed with status code #{res.code}, #{res.body}"
126
+ end
127
+ end
128
+ end
129
+
130
+ # Add file to be sent to Piston for execution.
131
+ #
132
+ # @param content [String] Content of the files to upload.
133
+ # @param name [String, nil] Name of the file to upload.
134
+ # @param encoding [String, nil] Encoding scheme used for the file content.
135
+ def add_file(content:, name: nil, encoding: nil)
136
+ file = {
137
+ name: name,
138
+ content: content,
139
+ encoding: encoding
140
+ }.compact
141
+
142
+ @files << file
143
+ end
144
+
145
+ # Clear all files.
146
+ def clear_files
147
+ @files.clear
148
+ end
149
+
150
+ # Get supported languages along with the current version and aliases.
151
+ #
152
+ # @return [Array<Runtime>] Array of runtimes.
153
+ def runtimes
154
+ request(method: :get, path: "/runtimes").map { |data| Runtime.new(data) }
155
+ end
156
+
157
+ # Execute code for a given language and version using the added files.
158
+ #
159
+ # @param language [String] Language to use for execution.
160
+ # @param version [String] Version of the language to use for execution.
161
+ # @param stdin [String, nil] Text to pass as stdin to the program.
162
+ # @param args [Array<String>] Arguments to pass to the program.
163
+ # @return [ExecutionResults] Execution results.
164
+ def execute(language:, version:, stdin: nil, args: nil)
165
+ body = {
166
+ language: language,
167
+ version: version,
168
+ files: @files,
169
+ stdin: stdin,
170
+ args: args,
171
+ compile_timeout: @compile_timeout,
172
+ run_timeout: @run_timeout,
173
+ compile_cpu_time: @compile_cpu_time,
174
+ run_cpu_time: @run_cpu_time,
175
+ compile_memory_limit: @compile_memory_limit,
176
+ run_memory_limit: @run_memory_limit
177
+ }.compact
178
+
179
+ ExecutionResults.new(request(method: :post, path: "/execute", body: body))
180
+ end
181
+
182
+ private :request
183
+ end
184
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: piston_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zahin Zaman
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: piston_sdk is a lightweight Ruby gem for using the Piston API (see https://github.com/engineer-man/piston).
13
+ email:
14
+ - alvii147@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".rspec"
20
+ - ".rubocop.yml"
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
24
+ - img/logo.svg
25
+ - lib/piston_sdk.rb
26
+ - lib/piston_sdk/version.rb
27
+ homepage: https://github.com/alvii147/piston-ruby-sdk
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 3.1.0
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 3.6.9
46
+ specification_version: 4
47
+ summary: A lightweight Ruby gem for using the Piston API.
48
+ test_files: []