Class: ExecPHP::RemoteServer
- Inherits:
-
Object
- Object
- ExecPHP::RemoteServer
- Defined in:
- lib/execphp/remote_server.rb
Overview
Represents a remote server accessor.
Instance Attribute Summary collapse
-
#access_token ⇒ String
readonly
Remote server access token.
-
#execphp_uri ⇒ URI
readonly
Path to a remote server’s
exec.phpscript.
Class Method Summary collapse
-
.from_file(filename) ⇒ Object
Initialize a remote server accessor using a configuration file.
Instance Method Summary collapse
-
#exec(batch, &block) ⇒ Object
Send a given script batch to a remote server for execution.
-
#initialize(execphp_url, access_token) ⇒ RemoteServer
constructor
Initialize a new remote server accessor instance.
-
#save_as(filename) ⇒ Object
Save current instance as a configuration file.
-
#version ⇒ String
Request a remote server’s
exec.phpscript version.
Constructor Details
#initialize(execphp_url, access_token) ⇒ RemoteServer
Initialize a new remote server accessor instance.
17 18 19 20 |
# File 'lib/execphp/remote_server.rb', line 17 def initialize(execphp_url, access_token) @execphp_uri = URI(execphp_url) @access_token = access_token end |
Instance Attribute Details
#access_token ⇒ String (readonly)
Returns remote server access token.
12 13 14 |
# File 'lib/execphp/remote_server.rb', line 12 def access_token @access_token end |
#execphp_uri ⇒ URI (readonly)
Returns path to a remote server’s exec.php script.
9 10 11 |
# File 'lib/execphp/remote_server.rb', line 9 def execphp_uri @execphp_uri end |
Class Method Details
.from_file(filename) ⇒ Object
Initialize a remote server accessor using a configuration file.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/execphp/remote_server.rb', line 44 def self.from_file(filename) format = File.extname(filename) config = case format when '.yaml' YAML.load_file(filename) when '.json' JSON.load(File.read filename) else raise "Unrecognized config file format (#{format})" end new(config['execphp_url'], config['access_token']) end |
Instance Method Details
#exec(batch, &block) ⇒ Object
Send a given script batch to a remote server for execution.
62 63 64 65 66 67 68 69 70 |
# File 'lib/execphp/remote_server.rb', line 62 def exec(batch, &block) @http ||= Net::HTTP.new(@execphp_uri.host, @execphp_uri.port) request = Net::HTTP::Post.new(@execphp_uri.request_uri) request.set_form_data('@' => @access_token, '$' => batch.to_s) @http.request(request, &block) end |
#save_as(filename) ⇒ Object
Save current instance as a configuration file.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/execphp/remote_server.rb', line 24 def save_as(filename) format = File.extname(filename) config = { 'execphp_url' => @execphp_uri.to_s, 'access_token' => @access_token } File.write(filename, case format when '.yaml' YAML.dump(config) when '.json' JSON.pretty_generate(config) else raise "Unrecognized config file format (#{format})" end) end |
#version ⇒ String
Request a remote server’s exec.php script version.
74 75 76 77 78 |
# File 'lib/execphp/remote_server.rb', line 74 def version script = ScriptBatch.new { |s| s << 'echo EXECPHP_VERSION;' } version = exec(script).body version if version =~ /^\d\.\d\.\d(?:\.\w+)?$/ end |