Class: ExecPHP::RemoteServer

Inherits:
Object
  • Object
show all
Defined in:
lib/execphp/remote_server.rb

Overview

Represents a remote server accessor.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(execphp_url, access_token) ⇒ RemoteServer

Initialize a new remote server accessor instance.

Parameters:

  • execphp_url (String)

    path to a remote server’s exec.php script

  • access_token (String)

    remote server access token



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_tokenString (readonly)

Returns remote server access token.

Returns:

  • (String)

    remote server access token



12
13
14
# File 'lib/execphp/remote_server.rb', line 12

def access_token
  @access_token
end

#execphp_uriURI (readonly)

Returns path to a remote server’s exec.php script.

Returns:

  • (URI)

    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.

Parameters:

  • filename (String)

    the name of 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.

Parameters:

  • batch (ScriptBatch)

    script batch to execute

  • block (Proc)

    optional callback



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.

Parameters:

  • filename (String)

    the name of 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

#versionString

Request a remote server’s exec.php script version.

Returns:

  • (String)

    version number definition



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