Sha256: e60d20d93b45105094b91751e84dc073f5483d0e591cee6c9fb730a145a2a701

Contents?: true

Size: 1.68 KB

Versions: 61

Compression:

Stored size: 1.68 KB

Contents

class Marty::RpcCall
  # POST to a remote marty
  def self.marty_post(host, port, path, script, node, attrs, params, options={},
                      ssl=false)
    http = Net::HTTP.new(host, port)
    http.use_ssl = ssl
    request = Net::HTTP::Post.new(path)
    request.add_field('Content-Type', 'application/json')
    request.body = (options + {
                      "script" => script,
                      "node"   => node,
                      "attrs"  => attrs.to_json,
                      "params" => params.to_json,
                    }).to_json
    begin
      response = http.request(request)
    rescue => e
      raise "#{e.message} during RPC call to #{host}:#{port}"
    end

    res = JSON.parse(response.body)
    raise res["error"] if res.is_a?(Hash) && !res["error"].blank?
    res
  end

  def self.marty_download(host, port, path, job_id, ssl=false)
    params = {job_id: job_id}
    url = path + '?' + URI.encode(URI.encode_www_form(params))

    http = Net::HTTP.new(host, port)
    http.use_ssl = ssl
    request = Net::HTTP::Get.new(url)

    begin
      http.request(request)
    rescue => e
      raise "#{e.message} during download call to #{host}:#{port}"
    end
  end

  def self.xml_call(host, port, path, body, use_ssl)
    http = Net::HTTP.new(host, port)
    request = Net::HTTP::Post.new(path)
    http.use_ssl = use_ssl
    request.add_field('Content-Type', 'xml')
    request.add_field('Accept', 'xml')
    request.body = body

    begin
      response = http.request(request)
      raise "got #{response} during XML call" if response.class != Net::HTTPOK
    rescue => e
      raise "#{e.message} during RPC call to #{host}:#{port}#{path}"
    end

    response.body
  end
end

Version data entries

61 entries across 61 versions & 1 rubygems

Version Path
marty-2.3.8 lib/marty/rpc_call.rb
marty-2.3.7 lib/marty/rpc_call.rb
marty-2.3.5 lib/marty/rpc_call.rb
marty-2.3.4 lib/marty/rpc_call.rb
marty-2.1.5 lib/marty/rpc_call.rb
marty-2.3.2 lib/marty/rpc_call.rb
marty-2.3.1 lib/marty/rpc_call.rb
marty-2.3.0 lib/marty/rpc_call.rb
marty-2.1.4 lib/marty/rpc_call.rb
marty-2.1.3 lib/marty/rpc_call.rb
marty-2.1.2 lib/marty/rpc_call.rb
marty-2.1.1 lib/marty/rpc_call.rb
marty-2.1.0 lib/marty/rpc_call.rb
marty-2.0.9 lib/marty/rpc_call.rb
marty-2.0.8 lib/marty/rpc_call.rb
marty-2.0.7 lib/marty/rpc_call.rb
marty-2.0.6 lib/marty/rpc_call.rb
marty-2.0.5 lib/marty/rpc_call.rb
marty-2.0.4 lib/marty/rpc_call.rb
marty-2.0.3 lib/marty/rpc_call.rb