Class: RubyRest::Client::Default

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.host ⇒ Object

Returns the hostname of the service to connect to



20
21
22
# File 'lib/rubyrest/client.rb', line 20

def self.host
  @host
end

.port ⇒ Object

Returns the port number on which to connect to



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

def self.port
  @port 
end

.with_server(name, port) ⇒ Object

Configures the server name and port



13
14
15
16
# File 'lib/rubyrest/client.rb', line 13

def self.with_server name, port
  @host = name
  @port = port
end

Instance Method Details

#create(path, data, api_key) ⇒ 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.



45
46
47
48
49
50
51
# File 'lib/rubyrest/client.rb', line 45

def create( path, data, api_key )
  body = nil
  body = hash2entry( data ).to_s if data != nil
  headers = prepare_headers( api_key )
  rsp = http.post( 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



77
78
79
# File 'lib/rubyrest/client.rb', line 77

def dashboard( api_key )
  retrieve( "/", nil, api_key )
end

#delete(path, data, api_key) ⇒ 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



68
69
70
71
72
73
# File 'lib/rubyrest/client.rb', line 68

def delete( path, data, api_key )
  path << to_query_string( data ) if data
  headers = prepare_headers( api_key )
  rsp = http.delete( path, headers )
  rsp.code.to_i
end

#hash2entry(data) ⇒ Object

Converts the specified hash of data into a simplified Atom Entry document.



97
98
99
100
101
102
103
104
105
106
# File 'lib/rubyrest/client.rb', line 97

def hash2entry( data )
  doc = RubyRest::Atom::Entry::Document.new
  entry = REXML::Element.new( "entry", doc  )
  content = REXML::Element.new( "content", entry )
  data.each { |name,value| 
    body = REXML::Element.new( name.to_s, content )
    body.text = value
  }
  return doc
end

#http ⇒ Object

Convenience method that returns a new HTTP object for each call



83
84
85
# File 'lib/rubyrest/client.rb', line 83

def http
  Net::HTTP.new( self.class.host, self.class.port )
end

#prepare_headers(api_key = nil) ⇒ Object

Builds a headers hash, with the specified authorization token if not nil



89
90
91
92
93
# File 'lib/rubyrest/client.rb', line 89

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, api_key) ⇒ 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.



34
35
36
37
38
39
# File 'lib/rubyrest/client.rb', line 34

def retrieve( path, data, api_key )
  path << to_query_string( data ) if data
  headers = prepare_headers( api_key )
  rsp = http.get( 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



124
125
126
127
128
# File 'lib/rubyrest/client.rb', line 124

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



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rubyrest/client.rb', line 110

def to_xml( rsp )
  begin
    RubyRest::Atom::Entry::Document.new( rsp.body ) if rsp.body
  rescue => e
    puts "unable to parse response body: " + e.message
    puts "---- response body ----"
    puts rsp.body
    puts "-----------------------"
    return nil
  end
end

#update(path, data, api_key) ⇒ 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.



57
58
59
60
61
62
63
# File 'lib/rubyrest/client.rb', line 57

def update( path, data, api_key )
  body = nil
  body = hash2entry( data ).to_s if data != nil
  headers = prepare_headers( api_key )
  rsp = http.put( path, body, headers )
  return to_xml( rsp ) if rsp.code.to_i == 200
end