Class: Fluidinfo::Client
- Inherits:
-
Object
- Object
- Fluidinfo::Client
- Defined in:
- lib/fluidinfo/client.rb
Overview
Client handles all of the communication between your application and Fluidinfo. You should create a new Client anytime you want to begin making calls as a different user. All Client methods return an instance of Response.
Example Usage:
# start with anonymous calls
fi = Fluidinfo::Client.new
fi.get "/values", :query => "has terry/rating > 4 and eric/seen",
:tags => ["imdb.com/title", "amazon.com/price"]
# now log in
fi = Fluidinfo::Client.new :user => "user", :password => "password"
fi.put "/about/Inception/user/comment", :body => "Awesome!"
Instance Method Summary collapse
-
#delete(path, options = {}) ⇒ Object
Call DELETE on one of the APIs.
-
#get(path, options = {}) ⇒ Object
Call GET on one of the APIs.
-
#head(path, options = {}) ⇒ Object
Call HEAD on one of the APIs.
-
#initialize(options = {}) ⇒ Client
constructor
Create a new instance of Client.
-
#post(path, options = {}) ⇒ Object
Call POST on one of the APIs.
-
#put(path, options = {}) ⇒ Object
Call PUT on one of the APIs.
Constructor Details
#initialize(options = {}) ⇒ Client
Create a new instance of Fluidinfo::Client.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fluidinfo/client.rb', line 36 def initialize(={}) base_url = [:instance] || Fluidinfo::MAIN headers = { :accept => "*/*", :user_agent => "fluidinfo.rb/#{Fluidinfo.version}" }.merge([:headers] || {}) @client = RestClient::Resource.new base_url, :user => [:user], :password => [:password], :headers => headers end |
Instance Method Details
#delete(path, options = {}) ⇒ Object
Call DELETE on one of the APIs.
133 134 135 136 137 138 139 140 |
# File 'lib/fluidinfo/client.rb', line 133 def delete(path, ={}) url = build_url path, headers = [:headers] || {} # nothing returned in response body for DELETE Response.new(@client[url].delete headers) rescue RestClient::Exception => e Response.new e.response end |
#get(path, options = {}) ⇒ Object
Call GET on one of the APIs.
57 58 59 60 61 62 63 |
# File 'lib/fluidinfo/client.rb', line 57 def get(path, ={}) url = build_url path, headers = [:headers] || {} Response.new(@client[url].get headers) rescue RestClient::Exception => e Response.new e.response end |
#head(path, options = {}) ⇒ Object
Call HEAD on one of the APIs. Only used to check for the existence of a tag using /about
or /objects
.
73 74 75 76 77 78 79 |
# File 'lib/fluidinfo/client.rb', line 73 def head(path, ={}) url = build_url path, headers = [:headers] || {} Response.new(@client[url].head headers) rescue RestClient::Exception => e Response.new e.response end |
#post(path, options = {}) ⇒ Object
Call POST on one of the APIs.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/fluidinfo/client.rb', line 89 def post(path, ={}) url = build_url path, body = [:body] headers = [:headers] || {} if body # the body for a POST will always be app/json, so no need # to waste cycles in build_payload body = Yajl.dump body headers.merge! :content_type => "application/json" end Response.new(@client[url].post body, headers) rescue RestClient::Exception => e Response.new e.response end |
#put(path, options = {}) ⇒ Object
Call PUT on one of the APIs.
114 115 116 117 118 119 120 121 |
# File 'lib/fluidinfo/client.rb', line 114 def put(path, ={}) url = build_url path, body, mime = build_payload headers = ([:headers] || {}).merge :content_type => mime Response.new(@client[url].put body, headers) rescue RestClient::Exception => e Response.new e.response end |