Class: Canopus::Plugins::API
- Inherits:
-
Object
- Object
- Canopus::Plugins::API
- Defined in:
- lib/canopus/plugins/api.rb,
sig/canopus.rbs,
sig/workspace_services.rbs
Instance Method Summary collapse
- #files ⇒ Array[String]
- #http_get(url) ⇒ String
-
#initialize(workspace, permissions) ⇒ API
constructor
A new instance of API.
- #notify(message) ⇒ String
- #permit!(permission) ⇒ void
- #replace(range, text) ⇒ patch?
- #run(command) ⇒ [String, String, Process::Status]
- #text ⇒ String
Constructor Details
#initialize(workspace, permissions) ⇒ API
Returns a new instance of API.
8 9 10 |
# File 'lib/canopus/plugins/api.rb', line 8 def initialize(workspace, ) @workspace, = workspace, end |
Instance Method Details
#files ⇒ Array[String]
28 29 30 31 |
# File 'lib/canopus/plugins/api.rb', line 28 def files permit!("read_project") @workspace.files.dup.freeze end |
#http_get(url) ⇒ String
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/canopus/plugins/api.rb', line 38 def http_get(url) permit!("network") require "net/http" uri = URI(url) raise ArgumentError, "HTTP or HTTPS URL required" unless %w[http https].include?(uri.scheme) body = +"" Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: 5, read_timeout: 5) do |http| http.request_get(uri.request_uri) do |response| response.read_body do |chunk| raise Canopus::Error, "plugin HTTP response exceeds 1 MiB" if body.bytesize + chunk.bytesize > 1 << 20 body << chunk end end end body end |
#notify(message) ⇒ String
32 |
# File 'lib/canopus/plugins/api.rb', line 32 def notify() = @workspace. = .to_s |
#permit!(permission) ⇒ void
This method returns an undefined value.
11 12 13 14 15 |
# File 'lib/canopus/plugins/api.rb', line 11 def permit!() requested = .to_s normalized = requested == "process" ? "exec" : requested raise Canopus::Plugins::PermissionDenied, "plugin requires #{requested}" unless .include?(normalized) end |
#replace(range, text) ⇒ patch?
24 25 26 27 |
# File 'lib/canopus/plugins/api.rb', line 24 def replace(range, text) permit!("edit_buffer") @workspace.editor.buffer.edit([[range, text]], kind: :plugin) end |
#run(command) ⇒ [String, String, Process::Status]
33 34 35 36 37 |
# File 'lib/canopus/plugins/api.rb', line 33 def run(command) permit!("exec") raise ArgumentError, "command must be a nonempty argument array" unless command.is_a?(Array) && !command.empty? Open3.capture3(*command, chdir: @workspace.root) end |
#text ⇒ String
16 17 18 19 20 21 22 23 |
# File 'lib/canopus/plugins/api.rb', line 16 def text permit!("read_buffer") buffer = @workspace.editor&.buffer raise Canopus::Error, "buffer is not available" unless buffer raise Canopus::Error, "plugin buffer text exceeds 1 MiB" if buffer.rope.bytesize > Plugins::BUFFER_CONTEXT_LIMIT buffer.text.dup.freeze end |