Class: RestBaby::Client
- Inherits:
-
Object
- Object
- RestBaby::Client
- 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)
-
- (Object) code
readonly
Response Code.
-
- (Object) password
readonly
The password for the user (for authentication).
-
- (Object) user
readonly
The user (for authentication).
-
- (Object) wsresponse
readonly
The WebService Response from the last call.
Instance Method Summary (collapse)
-
- (Object) add_headers(headers)
Modifies the headers by merging new headers with current headers.
-
- (Object) delete(headers = {}, path = '', parameters = {})
Basic web services Delete command.
-
- (Object) get(headers = {}, path = '', parameters = {})
Basic web services Get command.
-
- (Client) initialize(url, headers = {}, user = nil, password = nil)
constructor
Creates a new rest client.
-
- (Object) post(body = nil, headers = {}, path = '')
Basic web services Post command.
-
- (Object) put(body = nil, headers = {}, path = '')
Basic web services Put command.
-
- (Object) set_auth(user, password)
Adds user/password to the rest client.
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.
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.
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
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
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
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
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
49 50 51 52 |
# File 'lib/rest_baby.rb', line 49 def set_auth(user, password) @user = user @password = password end |