class GxG::Networking::RestClient

REST Classes:

Public Class Methods

clear_system_proxy() click to toggle source
# File lib/gxg/net_clients.rb, line 1807
def self.clear_system_proxy()
  result = false
  begin
    ::RestClient.proxy = nil
    result = true
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {}})
  end
  result
end
set_system_proxy(the_url=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 1793
def self.set_system_proxy(the_url=nil)
  result = false
  begin
    unless the_url.is_a?(::URI::Generic)
      raise ArgumentError, "You MUST provide a valid proxy URI."
    end
    ::RestClient.proxy = the_url.to_s
    result = true
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:url => the_url}})
  end
  result
end

Public Instance Methods

clear_cache() click to toggle source
# File lib/gxg/net_clients.rb, line 1828
def clear_cache()
  @cookies = nil
  true
end
delete(the_url=nil, options={}, &block) click to toggle source
# File lib/gxg/net_clients.rb, line 2019
def delete(the_url=nil, options={}, &block)
  result = false
  begin
    unless the_url.is_a?(::URI::Generic)
      raise ArgumentError, "You MUST provide a valid URI."
    end
    unless options.is_a?(::Hash)
      options = {}
    end
    header_options = (options[:header_options] || {}).merge({:accept => :json})
    if @cookies
      unless header_options[:cookies]
        header_options[:cookies] = @cookies
      end
    end
    if options[:pem_file]
      pem_data = ::File.read(options[:pem_file])
      header_options = header_options.merge({:ssl_client_cert => OpenSSL::X509::Certificate.new(pem_data), :ssl_client_key => OpenSSL::PKey::RSA.new(pem_data, options[:pem_password]), :verify_ssl => OpenSSL::SSL::VERIFY_PEER})
    end
    if options[:proxy].is_a?(::URI::Generic)
      header_options[:proxy] = options[:proxy]
    end
    response = ::RestClient.delete(the_url.to_s, header_options, &block)
    if options[:raw_response] == true
      result = response
    else
      result = true
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:url => the_url, :options => options}})
  end
  result
end
get(the_url=nil, options={}, &block) click to toggle source
# File lib/gxg/net_clients.rb, line 1833
def get(the_url=nil, options={}, &block)
  result = nil
  begin
    unless the_url.is_a?(::URI::Generic)
      raise ArgumentError, "You MUST provide a valid URI."
    end
    unless options.is_a?(::Hash)
      options = {}
    end
    header_options = (options[:header_options] || {}).merge({:accept => :json})
    if @cookies
      unless header_options[:cookies]
        header_options[:cookies] = @cookies
      end
    end
    if options[:pem_file]
      pem_data = ::File.read(options[:pem_file])
      header_options = header_options.merge({:ssl_client_cert => OpenSSL::X509::Certificate.new(pem_data), :ssl_client_key => OpenSSL::PKey::RSA.new(pem_data, options[:pem_password]), :verify_ssl => OpenSSL::SSL::VERIFY_PEER})
    end
    if options[:proxy].is_a?(::URI::Generic)
      header_options[:proxy] = options[:proxy]
    end
    if options[:save_as].is_a?(::String)
      client = ::GxG::Networking::HttpClient.new
      result = client.get(the_url, {:save_as => options[:save_as], :proxy => options[:proxy]})
    else
      if options[:parameters].is_a?(::Hash)
        response = ::RestClient.get(the_url.to_s, header_options.merge({:params => options[:parameters]}), &block)
      else
        response = ::RestClient.get(the_url.to_s, header_options, &block)
      end
      #
      unless @cookies
        if response.cookies.is_a?(::Hash)
          if response.cookies.keys.size > 0
            @cookies = response.cookies
          end
        end
      end
      if options[:raw_response] == true
        result = response
      else
        the_data = response.body.to_s
        if the_data.base64?
          the_data = the_data.decode64()
        end
        if the_data.json?
          result = the_data.from_json()
          # post-process value strings for DateTime, etc patterns and parse to ruby instances.
          # post-process - symbolize keys
        else
          if the_data.xml?
            if options[:xml_simple] == true
              result = the_data.from_xml_simple()
            else
              result = the_data.from_xml()
            end
          else
            if the_data.html?
              result = the_data.from_html()
            else
              result = the_data
            end
          end
        end
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:url => the_url, :options => options}})
  end
  result
end
intitialize() click to toggle source
# File lib/gxg/net_clients.rb, line 1818
def intitialize()
  # REST Servers to test with:
  # http://services.groupkt.com/country/get/all
  # http://services.groupkt.com/state/get/{countryCode}/all
  # https://httpbin.org/get
  #
  @cookies = nil
  self
end
post(the_url=nil, form_data=nil, options={}, &block) click to toggle source
# File lib/gxg/net_clients.rb, line 1968
def post(the_url=nil, form_data=nil, options={}, &block)
  result = false
  begin
    unless the_url.is_a?(::URI::Generic)
      raise ArgumentError, "You MUST provide a valid URI."
    end
    unless form_data.is_a?(::Hash)
      raise Exception, "You MUST provide valid form data (in the form of a Hash)."
    end
    unless options.is_a?(::Hash)
      options = {}
    end
    header_options = (options[:header_options] || {}).merge({:accept => :json})
    if @cookies
      unless header_options[:cookies]
        header_options[:cookies] = @cookies
      end
    end
    if options[:pem_file]
      pem_data = ::File.read(options[:pem_file])
      header_options = header_options.merge({:ssl_client_cert => OpenSSL::X509::Certificate.new(pem_data), :ssl_client_key => OpenSSL::PKey::RSA.new(pem_data, options[:pem_password]), :verify_ssl => OpenSSL::SSL::VERIFY_PEER})
    end
    if options[:proxy].is_a?(::URI::Generic)
      header_options[:proxy] = options[:proxy]
    end
    if options[:upload_file].is_a?(::String)
      form_data[:file] = ::File.new(options[:upload_file], "rb")
      form_data[:multipart] = true
    else
      form_data.each_pair do |the_key, the_value|
        if the_value.is_a?(::String)
          if the_value.valid_path?
            if ::File.exist?(the_value)
              form_data[(the_key)] = ::File.new(the_value, "rb")
            end
          end
        end
      end
    end
    response = ::RestClient.post(the_url.to_s, form_data, header_options, &block)
    if options[:raw_response] == true
      result = response
    else
      result = true
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:url => the_url, :options => options}})
  end
  result
end
put(the_url=nil, data=nil, options={}, &block) click to toggle source
# File lib/gxg/net_clients.rb, line 1906
def put(the_url=nil, data=nil, options={}, &block)
  result = false
  begin
    unless the_url.is_a?(::URI::Generic)
      raise ArgumentError, "You MUST provide a valid URI."
    end
    unless data.is_a?(::Hash)
      raise Exception, "You MUST provide data to PUT to the server (in the form of a Hash)."
    end
    unless options.is_a?(::Hash)
      options = {}
    end
    header_options = (options[:header_options] || {}).merge({:accept => :json})
    if @cookies
      unless header_options[:cookies]
        header_options[:cookies] = @cookies
      end
    end
    if options[:pem_file]
      pem_data = ::File.read(options[:pem_file])
      header_options = header_options.merge({:ssl_client_cert => OpenSSL::X509::Certificate.new(pem_data), :ssl_client_key => OpenSSL::PKey::RSA.new(pem_data, options[:pem_password]), :verify_ssl => OpenSSL::SSL::VERIFY_PEER})
    end
    if options[:proxy].is_a?(::URI::Generic)
      header_options[:proxy] = options[:proxy]
    end
    if options[:to_json]
      data = data.to_json()
      header_options[:content_type] = :json
    else
      if options[:upload_file].is_a?(::String)
        if options[:upload_file].valid_path?
          unless ::File.exist?(options[:upload_file])
            raise ArgumentError, "File does not exist: #{options[:upload_file].inspect}"
          end
          data[(::Celluloid::UUID::random_generate.to_s.to_sym)] = ::File.new(options[:upload_file], "rb")
        else
          raise ArgumentError, "You MUST provide a valid path String if you use the :upload_file option."
        end
      else
        data.each_pair do |the_key, the_value|
          if the_value.is_a?(::String)
            if the_value.valid_path?
              if ::File.exist?(the_value)
                data[(the_key)] = ::File.new(the_value, "rb")
              end
            end
          end
        end
      end
    end
    response = ::RestClient.put(the_url.to_s, data, header_options, &block)
    if options[:raw_response] == true
      result = response
    else
      result = true
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:url => the_url, :options => options}})
  end
  result
end