Class: Rescuetime::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, QueryBuildable
Defined in:
lib/rescuetime/collection.rb

Overview

Represents a potential rescuetime collection. It holds the query information and performs the query lazily, when #all or an Enumerable method is called.

Since:

  • v0.1.0

Constant Summary collapse

HOST =

Rescuetime Analytics API endpoint

Since:

  • v0.1.0

'https://www.rescuetime.com/anapi/data'.freeze

Constants included from QueryBuildable

QueryBuildable::BASE_PARAMS, QueryBuildable::VALID

Instance Method Summary collapse

Methods included from QueryBuildable

#activities, #categories, #date, #efficiency, #from, #order_by, #overview, #productivity, #to, #where

Constructor Details

#initialize(*terms) ⇒ Rescuetime::Collection

Returns a new Rescuetime collection. Default format is array.

Parameters:

  • terms (Array<Hash>)

    a list of parameter hashes

Since:

  • v0.1.0



22
23
24
25
# File 'lib/rescuetime/collection.rb', line 22

def initialize(*terms)
  @params = terms.reduce({}, :merge)
  @format = :array
end

Instance Method Details

#<<(terms) ⇒ Hash

Appends new terms onto the Rescuetime collection. In the case of duplicate keys, the later keys overwrite the former keys.

Parameters:

  • terms (Hash)

    new terms to add to collection params

Returns:

  • (Hash)

    collection params with terms merged in

Since:

  • v0.1.0



32
33
34
# File 'lib/rescuetime/collection.rb', line 32

def <<(terms)
  @params = params.merge terms
end

#allArray, CSV

Performs the rescuetime query and returns an array or csv response.

Returns:

  • (Array, CSV)

See Also:

  • Requester#get

Since:

  • v0.1.0



41
42
43
44
45
# File 'lib/rescuetime/collection.rb', line 41

def all
  requester = Rescuetime::Requester
  host      = HOST
  parse_response requester.get(host, params)
end

#each(&block) ⇒ Array, CSV

Enumerates over the collection of Rescuetime report records. Performs the query.

Returns:

  • (Array, CSV)

See Also:

Since:

  • v0.1.0



54
55
56
# File 'lib/rescuetime/collection.rb', line 54

def each(&block)
  all.each(&block)
end

#format(format) ⇒ Rescuetime::Collection

Sets the report format to a valid type

TODO: make chainable to the client

Parameters:

  • format (#to_s)

    desired report format (one of ‘array’ or ‘csv’)

Returns:

Since:

  • v0.1.0



64
65
66
67
68
69
70
71
# File 'lib/rescuetime/collection.rb', line 64

def format(format)
  # Guard: fail if the passed format isn't on the whitelist
  format = format.to_s
  formatters.all.include?(format) || raise(Errors::InvalidFormatError)

  @format = format
  self
end