User Tools

Site Tools


userspace:webservice_corner:ruby-rpc

This is an old revision of the document!


Ruby JSON-RPC

This is an example for a json-rpc in Ruby

require 'net/http'                                                                                                                                                                                             
require 'uri'                                                                                                                                                                                                  
require 'json'                                                                                                                                                                                                 
 
class OpsiRPC                                                                                                                                                                                                  
  def initialize(service_url)                                                                                                                                                                                  
    @uri = URI.parse(service_url)                                                                                                                                                                              
  end                                                                                                                                                                                                          
 
  def method_missing(name, *args)                                                                                                                                                                              
    post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json                                                                                                                              
    resp = JSON.parse( http_post_request(post_body) )                                                                                                                                                          
    raise JSONRPCError, resp['error'] if resp['error']                                                                                                                                                         
    resp['result']                                                                                                                                                                                             
  end                                                                                                                                                                                                          
 
  def http_post_request(post_body)                                                                                                                                                                             
    http    = Net::HTTP.new(@uri.host, @uri.port)                                                                                                                                                              
    http.use_ssl = true                                                                                                                                                                                        
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE                                                                                                                                                               
    request = Net::HTTP::Post.new(@uri.request_uri)                                                                                                                                                            
    request.basic_auth(@uri.user, @uri.password)                                                                                                                                                               
    request.content_type = 'application/json'                                                                                                                                                                  
    request.body = post_body                                                                                                                                                                                   
    http.request(request).body                                                                                                                                                                                 
  end                                                                                                                                                                                                          
 
  class JSONRPCError < RuntimeError; end                                                                                                                                                                       
end                                                                                                                                                                                                            
 
  h = OpsiRPC.new('https://test:w00t@opsi-server:4447/rpc')                                                                                                                                               
 
 
prodcuts=h.product_getObjects                                                                                                                                                                                  
prodcuts.each do |product|                                                                                                                                                                                     
  puts product['id']                                                                                                                                                                                           
end       
userspace/webservice_corner/ruby-rpc.1403255080.txt.gz · Last modified: 2021/08/23 08:37 (external edit)