Class: RestBaby::Client

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

Overview

Sends and receives data to a restful web service

Constant Summary

PARAM_STARTER =
'?'
PARAM_SEPARATOR =
'&'

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Client) initialize(url, headers = {}, user = nil, password = nil)

Creates a new rest client

eg. myrestservice.com:80/time eg. '{ "Content-Type" => "application/json"}' Can be multiple headers separated by commas inside the brackets.

Parameters:

  • url (String)

    url of the rest service

  • headers (Hash) (defaults to: {})

    default headers to use.



37
38
39
40
41
42
43
# 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

Instance Attribute Details

- (Object) code (readonly)

Response Code



28
29
30
# File 'lib/rest_baby.rb', line 28

def code
  @code
end

- (Object) password (readonly)

The password for the user (for authentication)



26
27
28
# File 'lib/rest_baby.rb', line 26

def password
  @password
end

- (Object) user (readonly)

The user (for authentication)



24
25
26
# File 'lib/rest_baby.rb', line 24

def user
  @user
end

- (Object) wsresponse (readonly)

The WebService Response from the last call



22
23
24
# File 'lib/rest_baby.rb', line 22

def wsresponse
  @wsresponse
end

Instance Method Details

- (Object) add_headers(headers)

Modifies the headers by merging new headers with current headers.

Parameters:

  • headers (Hash)

    new headers to merge with current headers



57
58
59
# File 'lib/rest_baby.rb', line 57

def add_headers(headers)
  @headers = @headers.merge(headers)
end

- (Object) delete(headers = {}, path = '', parameters = {})

Basic web services Delete command

Parameters:

  • path (String) (defaults to: '')

    Path of service to send the command to

  • headers (Hash) (defaults to: {})

    header parameters including authorization and Content-Type

Returns:

  • The response from the rest server is returned



97
98
99
100
101
102
103
# 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

- (Object) get(headers = {}, path = '', parameters = {})

Basic web services Get command

Parameters:

  • headers (Hash) (defaults to: {})

    header parameters including authorization and Content-Type

  • path (String) (defaults to: '')

    Path of service to send the command to

  • parameters (Hash) (defaults to: {})

    query string that added as part of the URL

Returns:

  • The response from the rest server is returned



68
69
70
71
72
73
74
# 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

- (Object) post(body = nil, headers = {}, path = '')

Basic web services Post command

Parameters:

  • path (String) (defaults to: '')

    Path of service to send the command to

  • body (Any type of string) (defaults to: nil)

    Data to put in the body such as json or xml

  • headers (Hash) (defaults to: {})

    header parameters including authorization and Content-Type

Returns:

  • The response from the rest server is returned



84
85
86
87
88
89
# 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 unless body.nil?
  send_request(uri, request, headers)
end

- (Object) put(body = nil, headers = {}, path = '')

Basic web services Put command

Parameters:

  • path (String) (defaults to: '')

    Path of service to send the command to

  • body (String) (defaults to: nil)

    data to put in the body

  • headers (Hash) (defaults to: {})

    header parameters including authorization and Content-Type

Returns:

  • Response from the rest server is returned



112
113
114
115
116
117
# 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 unless body.nil?
  send_request(uri, request, headers)
end

- (Object) set_auth(user, password)

Adds user/password to the rest client

Parameters:

  • user (String)

    user that has authorization

  • password (String)

    authorized user's password



49
50
51
52
# File 'lib/rest_baby.rb', line 49

def set_auth(user, password)
  @user = user
  @password = password
end