Class: Love::Client

Inherits:
Object
  • Object
show all
Includes:
ResourceURI
Defined in:
lib/love.rb

Overview

The Love::Client class acts as a client to the Tender REST API. Obtain an instance of this class by calling connect instead of instantiating this class directly.

You can either fetch individual resources using #get_user, #get_discussion, and similar methods, or iterate over collections using #each_discussion, #each_category and similar methods.

Constant Summary collapse

TENDER_API_HOST =

The Tender API host to connect to.

'api.tenderapp.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResourceURI

#append_query, #collection_uri, #singleton_uri

Constructor Details

#initialize(site, api_key, options = {}) ⇒ Client

Initializes the client.

Parameters:

  • site (String)

    The site to work with.

  • api_key (String)

    The API key for this site.

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

    Connectivity options.

Options Hash (options):

  • :persistent (Boolean) — default: false

    Whether to create a persistent TCP connection.

  • :sleep_between_requests (Float) — default: 0.5

    The time between requests in seconds.

See Also:



151
152
153
154
155
156
157
# File 'lib/love.rb', line 151

def initialize(site, api_key, options = {})
  @site, @api_key = site, api_key

  # Handle options
  @persistent = !!options[:persistent]
  @sleep_between_requests = options[:sleep_between_requests] || 0.5
end

Instance Attribute Details

#api_keyString (readonly)

Returns The API key to authenticate with.

Returns:

  • (String)

    The API key to authenticate with.



139
140
141
# File 'lib/love.rb', line 139

def api_key
  @api_key
end

#siteString (readonly)

Returns The site to work with.

Returns:

  • (String)

    The site to work with



136
137
138
# File 'lib/love.rb', line 136

def site
  @site
end

#sleep_between_requestsFloat

Returns The number of seconds to sleep between paged requests.

Returns:

  • (Float)

    The number of seconds to sleep between paged requests.



142
143
144
# File 'lib/love.rb', line 142

def sleep_between_requests
  @sleep_between_requests
end

Instance Method Details

#close_connectionnil

Closes the persistent connectio to the server

Returns:

  • (nil)


240
241
242
# File 'lib/love.rb', line 240

def close_connection
  @connection.finish if connected?
end

#connected?Boolean

Returns true iff the client currently has a TCP connection with the Tender API server.

Returns:

  • (Boolean)

    true iff the client currently has a TCP connection with the Tender API server.



245
246
247
# File 'lib/love.rb', line 245

def connected?
  @connection && @connection.started?
end

#connectionNet::HTTP

Returns a persistent connection to the server, reusing a connection of it was previously established.

This method is mainly used for internal use but can be used to do advanced HTTP connectivity with the Tender API server.

Returns:

  • (Net::HTTP)

    The net/http connection instance.



230
231
232
233
234
235
236
# File 'lib/love.rb', line 230

def connection
  @connection ||= Net::HTTP.new(TENDER_API_HOST, Net::HTTP.https_default_port).tap do |http|
    http.use_ssl = true
    # http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.start
  end
end

#each_category(options = {}) {|Hash| ... } ⇒ nil

Iterates over all Tender categories.

Options Hash (options):

  • :since (Date)

    Only include records updated since the provided date. Caution: not supported by all resources.

  • :start_page (Integer)

    The initial page number to request.

  • :end_page (Integer)

    The final page number to request.

Yields:

  • (Hash)

    The attributes of each category will be yielded as (nested) Hash.

Returns:

  • (nil)


195
196
197
# File 'lib/love.rb', line 195

def each_category(options = {}, &block)
  paged_each(collection_uri('categories'), 'categories', options, &block)
end

#each_discussion(options = {}) {|Hash| ... } ⇒ nil

Iterates over all Tender discussions.

Options Hash (options):

  • :since (Date)

    Only include records updated since the provided date. Caution: not supported by all resources.

  • :start_page (Integer)

    The initial page number to request.

  • :end_page (Integer)

    The final page number to request.

Yields:

  • (Hash)

    The attributes of each discussion will be yielded as (nested) Hash.

Returns:

  • (nil)


219
220
221
# File 'lib/love.rb', line 219

def each_discussion(options = {}, &block)
  paged_each(collection_uri('discussions'), 'discussions', options, &block)
end

#each_queue(options = {}) {|Hash| ... } ⇒ nil

Iterates over all Tender users.

Options Hash (options):

  • :since (Date)

    Only include records updated since the provided date. Caution: not supported by all resources.

  • :start_page (Integer)

    The initial page number to request.

  • :end_page (Integer)

    The final page number to request.

Yields:

  • (Hash)

    The attributes of each user will be yielded as (nested) Hash.

Returns:

  • (nil)


203
204
205
# File 'lib/love.rb', line 203

def each_queue(options = {}, &block)
  paged_each(collection_uri('queues'), 'named_queues', options, &block)
end

#each_user(options = {}) {|Hash| ... } ⇒ nil

Iterates over all Tender users.

Options Hash (options):

  • :since (Date)

    Only include records updated since the provided date. Caution: not supported by all resources.

  • :start_page (Integer)

    The initial page number to request.

  • :end_page (Integer)

    The final page number to request.

Yields:

  • (Hash)

    The attributes of each user will be yielded as (nested) Hash.

Returns:

  • (nil)


211
212
213
# File 'lib/love.rb', line 211

def each_user(options = {}, &block)
  paged_each(collection_uri('users'), 'users', options, &block)
end

#get_category(id_or_href) ⇒ Hash

Returns a single Tender category.

Parameters:

  • id_or_href (URI, String, Integer)

    The category ID or HREF. Can be either a URI instance, a string containing a URI, or a category ID as a numeric string or integer.

Returns:

  • (Hash)

    The category attributes in a Hash.



179
180
181
# File 'lib/love.rb', line 179

def get_category(id_or_href)
  get(singleton_uri(id_or_href, 'categories'))
end

#get_discussion(id_or_href) ⇒ Hash

Returns a single Tender discussion.

Parameters:

  • id_or_href (URI, String, Integer)

    The discussion ID or HREF. Can be either a URI instance, a string containing a URI, or a discussion ID as a numeric string or integer.

Returns:

  • (Hash)

    The discussion attributes in a Hash.



171
172
173
# File 'lib/love.rb', line 171

def get_discussion(id_or_href)
  get(singleton_uri(id_or_href, 'discussions'))
end

#get_queue(id_or_href) ⇒ Hash

Returns a single Tender queue.

Parameters:

  • id_or_href (URI, String, Integer)

    The queue ID or HREF. Can be either a URI instance, a string containing a URI, or a queue ID as a numeric string or integer.

Returns:

  • (Hash)

    The queue attributes in a Hash.



187
188
189
# File 'lib/love.rb', line 187

def get_queue(id_or_href)
  get(singleton_uri(id_or_href, 'queues'), options)
end

#get_user(id_or_href) ⇒ Hash

Returns a single Tender user.

Parameters:

  • id_or_href (URI, String, Integer)

    The user ID or HREF. Can be either a URI instance, a string containing a URI, or a user ID as a numeric string or integer.

Returns:

  • (Hash)

    The user attributes in a Hash.



163
164
165
# File 'lib/love.rb', line 163

def get_user(id_or_href)
  get(singleton_uri(id_or_href, 'users'))
end

#persistent?Boolean

Returns true iff the client is using persistent connections.

Returns:

  • (Boolean)

    true iff the client is using persistent connections.



250
251
252
# File 'lib/love.rb', line 250

def persistent?
  @persistent
end