class RestBaby::Client

Sends and receives data to a restful web service

Constants

PARAM_SEPARATOR
PARAM_STARTER

Attributes

code[R]

Response Code

password[R]

The password for the user (for authentication)

user[R]

The user (for authentication)

wsresponse[R]

The WebService Response from the last call

Public Class Methods

new(url, headers = {}, user = nil, password = nil) click to toggle source

Creates a new rest client

@param url [String] url of the rest service eg. myrestservice.com:80/time @param headers [Hash] default headers to use. eg. '{ "Content-Type" => "application/json"}' Can be multiple headers separated by commas inside the brackets.

# File lib/rest_baby.rb, line 37
def initialize(url, headers = {}, user = nil, password = nil)
  @url = url
  @headers = headers
  @user = user
  @password = password
  @verbose = (ENV['DEBUG_HTTP'] != 'false')
end

Public Instance Methods

add_headers(headers) click to toggle source

Modifies the headers by merging new headers with current headers.

@param headers [Hash] new headers to merge with current headers

# File lib/rest_baby.rb, line 57
def add_headers(headers)
  @headers = @headers.merge(headers)
end
delete(headers = {}, path = '', parameters = {}) click to toggle source

Basic web services Delete command

@param path [String] Path of service to send the command to @param headers [Hash] header parameters including

authorization and Content-Type

@return The response from the rest server is returned

# File lib/rest_baby.rb, line 97
def delete(headers = {}, path = '', parameters = {})
  full_path = [path, URI.encode_www_form(parameters)].join(PARAM_STARTER)
  uri = URI.parse("#{@url}#{full_path}")
  request = Net::HTTP::Delete.new(uri.request_uri)
  request.body = nil
  send_request(uri, request, headers)
end
get(headers = {}, path = '', parameters = {}) click to toggle source

Basic web services Get command

@param headers [Hash] header parameters including authorization

and Content-Type

@param path [String] Path of service to send the command to @param parameters [Hash] query string that added as part of the URL @return The response from the rest server is returned

# File lib/rest_baby.rb, line 68
def get(headers = {}, path = '', parameters = {})
  full_path = [path, URI.encode_www_form(parameters)].join(PARAM_STARTER)
  uri = URI.parse("#{@url}#{full_path}")
  request = Net::HTTP::Get.new(uri.request_uri)
  request.body = nil
  send_request(uri, request, headers)
end
post(body = nil, headers = {}, path = '') click to toggle source

Basic web services Post command

@param path [String] Path of service to send the command to @param body [Any type of string] Data to put in the

body such as json or xml

@param headers [Hash] header parameters including authorization

and Content-Type

@return The response from the rest server is returned

# File lib/rest_baby.rb, line 84
def post(body = nil, headers = {}, path = '')
  uri = URI.parse("#{@url}#{path}")
  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = body
  send_request(uri, request, headers)
end
put(body = nil, headers = {}, path = '') click to toggle source

Basic web services Put command

@param path [String] Path of service to send the command to @param body [String] data to put in the body @param headers [Hash] header parameters including authorization

and Content-Type

@return Response from the rest server is returned

# File lib/rest_baby.rb, line 112
def put(body = nil, headers = {}, path = '')
  uri = URI.parse("#{@url}#{path}")
  request = Net::HTTP::Put.new(uri.request_uri)
  request.body = body
  send_request(uri, request, headers)
end
set_auth(user, password) click to toggle source

Adds user/password to the rest client

@param user [String] user that has authorization @param password [String] authorized user's password

# File lib/rest_baby.rb, line 49
def set_auth(user, password)
  @user = user
  @password = password
end

Private Instance Methods

body_text(type, response_body) click to toggle source
# File lib/rest_baby.rb, line 159
def body_text(type, response_body)
  response_body ||= '[Empty]'
  case type
  when 'application/json'
    pretty_json(response_body)
  when 'application/xml'
    pretty_xml(response_body)
  else
    "#{response_body}\n<"
  end
end
header_text(message, pointer) click to toggle source
# File lib/rest_baby.rb, line 153
def header_text(message, pointer)
  headers = ''
  message.each { |key, value| headers << "#{pointer} #{key} = #{value}\n" }
  headers
end
pretty_json(json) click to toggle source
# File lib/rest_baby.rb, line 171
def pretty_json(json)
  json = JSON(json) unless json.class == Hash
  JSON.pretty_generate(json)
end
pretty_xml(xml) click to toggle source
# File lib/rest_baby.rb, line 176
def pretty_xml(xml)
  doc = Nokogiri.XML(xml) { |config| config.default_xml.noblanks }
  doc.to_xml(indent: 2)
end
print_request(request, uri) click to toggle source
print_response(response) click to toggle source
send_request(uri, request, headers) click to toggle source
# File lib/rest_baby.rb, line 121
def send_request(uri, request, headers)
  @headers.merge(headers).each { |key, value| request[key] = value }
  request.basic_auth(@user, @password)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'
  print_request(request, uri)
  @wsresponse = http.request(request)
  print_response(@wsresponse)
  @wsresponse
end