Class | Simpy::ClientBase |
In: |
lib/simpy.rb
|
Parent: | Object |
A basic Simpy client interface
This class provides a basic interface to the Simpy web system. It provides only the bare minimum needed to interact with Simpy, and it makes very few assumptions about the features or behavior of the REST API. It provides no mechanisms to make Simpy interaction conducive to Ruby programming, and the methods return plain XML or strings rather than Link instances.
Instantiating this class directly is not recommended. It is appropriate to use this as a base class for your own Simpy client interfaces. This class is inherited by Simpy::Client, and the methods provided by this class should be used in a Simpy::Client instance in cases where plain XML output is needed, or when it is necessary to use a function of the REST API that has not yet been exposed within Simpy::Client.
simpy.command "SaveLink", "href" => "http://www.simpy.com", "accessType" => 1, "title" => "test")
simpy.command "GetLinks", "q" => "test", "beforeDate" => "2006-05-30"
simpy.command "GetTags"
Create a new ClientBase instance
This class method creates a new ClientBase instance. It can be redefined or overloaded in a subclass to alter the domain, realm, authentication mechanism, and user agent specified by this library. Altering those values is generally not recommended, but it could be useful to do so under certain circumstances.
Developers that wish to change the user agent string should alter the global $USER_AGENT variable rather than changing the @headers value assigned in this method.
user: | The user’s Simpy login username |
passw: | The user’s Simpy login password |
Simpy::ClientBase.new("segphault", "xxxxxxx")
# File lib/simpy.rb, line 396 396: def initialize user, passw 397: @domain = "www.simpy.com"; @realm = "/simpy/api/rest/" 398: @auth = "Basic #{Base64.encode64 user+":"+passw}" 399: @headers = {"Authorization" => @auth, "User-Agent" => $USER_AGENT} 400: end
Generate a URL query from a hash
This method generates a complete URL query from the keys and values of a hash table. Keys that are associated with blank values (like nil, "", and []) are not included in the query. The values are automatically converted to strings and properly encoded using URI.esc.
hash: | The hash table from which to build the query |
client.args("a" => 1, "b" => 2, "c" => 3) # => "a=1&b=2&c=3"
# File lib/simpy.rb, line 421 421: def args hash 422: hash.reject! {|x,y| [nil, "", []].include? y} 423: hash.map {|x,y| "#{x}=#{URI.esc y}"}.join "&" 424: end
Transmit a command to the Simpy server
This methods sends a command to the Simpy server and returns a REXML::Document instance that contains the output content. It uses the connection method to interact with the Simpy server.
This method should be used in cases where the wrapper methods of the Simpy::Client class aren’t necessary or applicable. In particular, it should be used when it is necessary to use a feature of the REST API that isn’t yet exposed by the Simpy::Client wrapper methods. It is also useful in situations where parsed XML is the desired output rather than specialized instances.
command: | The name of the Simpy command to perform. The Simpy .do suffix does not need to be included in this value. |
args: | A hash containing the parameters to transmit with the command. The data in the hash is converted into a URL query string with the args method. |
client.command "GetLinks", "q" => "dtrace", "beforeDate" => "2006-05-30"
# File lib/simpy.rb, line 487 487: def command command, args={} 488: puts "#{command}.do?#{args(args)}" 489: REXML::Document.new connect("#{command}.do?#{args(args)}") 490: end
Open a connection to the Simpy server
This method opens a connection to the Simpy server to the specified path and returns the output in a string. It handles Simpy server redirects and appropriately leverages Simpy’s user login mechanism. This method is used by the command method to connect and retrieve data. It should only be called explicitly in cases where output is needed in string form or when the REST API path is generated by some other mechanism and needs to be passed to the server directly.
This method can be overloaded in subclasses to customize the Simpy interaction procses. It can also be redefined to alter the behavior of the methods in Simpy::Client.
path: | A string containing a complete Simpy URL command and query |
client.connect "GetLinks.do?q=test&beforeDate=2006-06-30"
# File lib/simpy.rb, line 451 451: def connect path 452: resp, data = Net::HTTP.new(@domain).get(@realm + path, @headers) 453: raise "Service Unavailable" if ["503", "404"].include? resp.code 454: resp.code == "302" ? connect(resp["location"].split(@realm)[1]) : data 455: end