Class: RubyRest::Client::Default
- Inherits:
-
Object
- Object
- RubyRest::Client::Default
- 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.
-
#hash2entry(data) ⇒ Object
Converts the specified hash of data into a simplified Atom Entry document.
-
#http ⇒ Object
Convenience method that returns a new HTTP object for each call.
-
#initialize(host, port) ⇒ Default
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.
-
#to_xml(rsp) ⇒ Object
Parses the response body string as a Ruby-on-Rest XML Document, which can be a Feed or Entry, or Service Document.
-
#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) ⇒ Default
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 = hash2entry( data ).to_s if data != nil headers = prepare_headers( api_key ) rsp = http.post( encode_path( path ), body, headers ) return to_xml( rsp ) if rsp.code.to_i == 201 end |
#dashboard(api_key) ⇒ Object
Convenience method that returns the service document from the service endpoint
71 72 73 |
# File 'lib/rubyrest/client.rb', line 71 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 |
# 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 ) rsp.code.to_i 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 |
#hash2entry(data) ⇒ Object
Converts the specified hash of data into a simplified Atom Entry document.
91 92 93 94 95 96 |
# File 'lib/rubyrest/client.rb', line 91 def hash2entry( data ) doc = RubyRest::Atom::Document.new content = doc.add_element( "entry" ).add_element( "rubyrest:content" ) data.each { |name,value| content.add_element( name.to_s ).add_text( value.to_s ) } return doc end |
#http ⇒ Object
Convenience method that returns a new HTTP object for each call
77 78 79 |
# File 'lib/rubyrest/client.rb', line 77 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
83 84 85 86 87 |
# File 'lib/rubyrest/client.rb', line 83 def prepare_headers( api_key = nil ) headers = { "Content-Type" => "text/xml; charset=utf-8" } headers[ "token" ] = api_key if api_key != nil 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 to_xml( 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
114 115 116 117 118 |
# File 'lib/rubyrest/client.rb', line 114 def to_query_string( data ) qs = "?" params.each{ |k,v| qs << "#{k}=#{v}&" } qs.chop! end |
#to_xml(rsp) ⇒ Object
Parses the response body string as a Ruby-on-Rest XML Document, which can be a Feed or Entry, or Service Document
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rubyrest/client.rb', line 100 def to_xml( rsp ) begin RubyRest::Atom::Document.new( rsp.body ) if rsp.body rescue => e puts "unable to parse response body: " + e. puts "---- response body ----" puts rsp.body puts "-----------------------" return nil end 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 = hash2entry( data ).to_s if data != nil headers = prepare_headers( api_key ) rsp = http.put( encode_path( path ), body, headers ) return to_xml( rsp ) if rsp.code.to_i == 200 end |