Class: RubyRest::Client::Abstract
- Inherits:
-
Object
- Object
- RubyRest::Client::Abstract
- Defined in:
- lib/rubyrest/client.rb
Overview
Default client for web services deployed with Ruby-on-Rest. Translates create, retrieve, update and delete methods into POST, GET, PUT and DELETE http requests
Instance Method Summary collapse
-
#create(path, data, api_key = nil) ⇒ Object
Converts the specified hash of data into a simplified Atom Entry, then performs a POST http request.
-
#dashboard(api_key) ⇒ Object
Convenience method that returns the service document from the service endpoint.
-
#delete(path, data = nil, api_key = nil) ⇒ Object
Converts the specified hash of data into a query string, then performs a DELETE http request.
-
#encode_path(path) ⇒ Object
Encodes the specified path, by replacing spaces for the + character.
-
#http ⇒ Object
Convenience method that returns a new HTTP object for each call.
-
#initialize(host, port) ⇒ Abstract
constructor
Configures the server name and port.
-
#prepare_headers(api_key = nil) ⇒ Object
Builds a headers hash, with the specified authorization token if not nil.
-
#retrieve(path, data = nil, api_key = nil) ⇒ Object
Converts the specified hash of data into a query string, then performs a GET http request.
-
#to_query_string(data) ⇒ Object
Reusable method that creates a query string with the specified hash of data.
-
#update(path, data, api_key = nil) ⇒ Object
Converts the specified hash of data into a simplified Atom Entry, then performs a PUT http request.
Constructor Details
#initialize(host, port) ⇒ Abstract
Configures the server name and port
13 14 15 16 |
# File 'lib/rubyrest/client.rb', line 13 def initialize( host, port ) @host = host @port = port end |
Instance Method Details
#create(path, data, api_key = nil) ⇒ Object
Converts the specified hash of data into a simplified Atom Entry, then performs a POST http request. The response body is returned as an an Atom Entry if the response status code is 201.
39 40 41 42 43 44 45 |
# File 'lib/rubyrest/client.rb', line 39 def create( path, data, api_key = nil ) body = nil body = serialize_data( data ).to_s if data != nil headers = prepare_headers( api_key ) rsp = http.post( encode_path( path ), body, headers ) return hidrate_data( rsp ) if rsp.code.to_i == 201 end |
#dashboard(api_key) ⇒ Object
Convenience method that returns the service document from the service endpoint
72 73 74 |
# File 'lib/rubyrest/client.rb', line 72 def dashboard( api_key ) retrieve( "/", nil, api_key ) end |
#delete(path, data = nil, api_key = nil) ⇒ Object
Converts the specified hash of data into a query string, then performs a DELETE http request. This method simply returns the response status code
62 63 64 65 66 67 68 |
# File 'lib/rubyrest/client.rb', line 62 def delete( path, data =nil, api_key = nil ) path << to_query_string( data ) if data headers = prepare_headers( api_key ) rsp = http.delete( encode_path( path ), headers ) raise "error when deleting" if rsp.code.to_i != 200 return nil end |
#encode_path(path) ⇒ Object
Encodes the specified path, by replacing spaces for the + character. The server will decode
20 21 22 |
# File 'lib/rubyrest/client.rb', line 20 def encode_path( path ) path.gsub( " ", "+" ) end |
#http ⇒ Object
Convenience method that returns a new HTTP object for each call
78 79 80 |
# File 'lib/rubyrest/client.rb', line 78 def http Net::HTTP.new( @host, @port ) end |
#prepare_headers(api_key = nil) ⇒ Object
Builds a headers hash, with the specified authorization token if not nil
84 85 86 87 88 89 |
# File 'lib/rubyrest/client.rb', line 84 def prepare_headers( api_key = nil ) headers = { "Content-Type" => "text/xml; charset=utf-8" } headers[ "token" ] = api_key if api_key != nil headers["format"]=@format return headers end |
#retrieve(path, data = nil, api_key = nil) ⇒ Object
Converts the specified hash of data into a query string, then performs a GET http request. The response body is returned as an an Atom document if the response status code is 200.
28 29 30 31 32 33 |
# File 'lib/rubyrest/client.rb', line 28 def retrieve( path, data = nil, api_key = nil ) path << to_query_string( data ) if data headers = prepare_headers( api_key ) rsp = http.get( encode_path( path ), headers ) return hidrate_data( rsp ) if rsp.code.to_i == 200 end |
#to_query_string(data) ⇒ Object
Reusable method that creates a query string with the specified hash of data
93 94 95 96 97 |
# File 'lib/rubyrest/client.rb', line 93 def to_query_string( data ) qs = "?" params.each{ |k,v| qs << "#{k}=#{v}&" } qs.chop! end |
#update(path, data, api_key = nil) ⇒ Object
Converts the specified hash of data into a simplified Atom Entry, then performs a PUT http request. The response body is returned as an an Atom Entry if the response status code is 200.
51 52 53 54 55 56 57 |
# File 'lib/rubyrest/client.rb', line 51 def update( path, data, api_key = nil) body = nil body = serialize_data( data ).to_s if data != nil headers = prepare_headers( api_key ) rsp = http.put( encode_path( path ), body, headers ) return hidrate_data( rsp ) if rsp.code.to_i == 200 end |