Class: Fluidinfo::Client

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

Constructor Details

#initialize(options = {}) ⇒ Client

Create a new instance of Fluidinfo::Client.

Parameters:

  • options (Hash) (defaults to: {})

    Initialization options.

Options Hash (options):

  • :user (String)

    The username to use for authentication.

  • :password (String)

    The password to use for authentication.

  • :headers (Hash)

    Additional headers to send with every API call made via this client.



36
37
38
39
40
41
42
43
44
45
# File 'lib/fluidinfo/client.rb', line 36

def initialize(options={})
  base_url = options[:instance] || Fluidinfo::MAIN
  headers = {
    :accept => "*/*",
    :user_agent => "fluidinfo.rb/#{Fluidinfo.version}"
  }.merge(options[:headers] || {})
  @client = RestClient::Resource.new base_url, :user => options[:user],
                                               :password => options[:password],
                                               :headers => headers
end

Instance Method Details

#delete(path, options = {}) ⇒ Object

Call DELETE on one of the APIs.

Parameters:

  • path (String)

    The path segment of the API call.

  • options (Hash) (defaults to: {})

    Additional arguments for the DELETE call.

Options Hash (options):

  • :headers (Hash)

    Additional headers to send.

  • :query (String)

    A Fluidinfo query for objects, only used in /values.

  • :tags (Array)

    Tags to be deleted, only used in /values.



133
134
135
136
137
138
139
140
# File 'lib/fluidinfo/client.rb', line 133

def delete(path, options={})
  url = build_url path, options
  headers = options[: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.

Parameters:

  • path (String)

    The path segment of the API call.

  • options (Hash) (defaults to: {})

    Additional arguments for the GET call.

Options Hash (options):

  • :headers (Hash)

    Additional headers to send.

  • :query (String)

    A Fluidinfo query for objects, only used in /objects and /values.

  • :tags (Array)

    Tags to be deleted, only used in /values.



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

def get(path, options={})
  url = build_url path, options
  headers = options[: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.

Parameters:

  • path (String)

    The path segment of the API call.

  • options (Hash) (defaults to: {})

    Additional arguments for the GET call.

Options Hash (options):

  • :headers (Hash)

    Additional headers to send.



73
74
75
76
77
78
79
# File 'lib/fluidinfo/client.rb', line 73

def head(path, options={})
  url = build_url path, options
  headers = options[: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.

Parameters:

  • path (String)

    The path segment of the API call.

  • options (Hash) (defaults to: {})

    Additional arguments for the POST call.

Options Hash (options):

  • :headers (Hash)

    Additional headers to send.

  • :body (Hash)

    The payload to send.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fluidinfo/client.rb', line 89

def post(path, options={})
  url = build_url path, options
  body = options[:body]
  headers = options[: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.

Parameters:

  • path (String)

    The path segment of the API call.

  • options (Hash) (defaults to: {})

    Additional arguments for the PUT call.

Options Hash (options):

  • :headers (Hash)

    Additional headers to send.

  • :body (Hash, other)

    The payload to send. Type should be Hash unless sending a tag-value.

  • :mime (String)

    The mime-type of an opaque tag-value.



114
115
116
117
118
119
120
121
# File 'lib/fluidinfo/client.rb', line 114

def put(path, options={})
  url = build_url path, options
  body, mime = build_payload options
  headers = (options[:headers] || {}).merge :content_type => mime
  Response.new(@client[url].put body, headers)
rescue RestClient::Exception => e
  Response.new e.response
end