Class: Freckle

Inherits:
Object
  • Object
show all
Defined in:
lib/freckle.rb

Defined Under Namespace

Classes: Base, Entry, Project, User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Freckle

Returns a new instance of Freckle.



9
10
11
12
13
14
15
16
# File 'lib/freckle.rb', line 9

def initialize(options = {})
  @account  = options.delete(:account)
  @token    = options.delete(:token)
  @logger   = options.delete(:logger) || Logger.new(STDOUT)
  RestClient.log = @logger
  @connection = RestClient::Resource.new( "http://#{@account}.letsfreckle.com/api",
                                          :headers => {"X-FreckleToken" => @token} )
end

Class Method Details

.connectionObject

Raises:

  • (RuntimeError)


21
22
23
24
# File 'lib/freckle.rb', line 21

def self.connection
  return @connection if @connection
  raise RuntimeError, "Freckle.establish_connection with :account and :token first"
end

.establish_connection(options) ⇒ Object



18
19
20
# File 'lib/freckle.rb', line 18

def self.establish_connection(options)
  @connection = new(options)
end

Instance Method Details

#entries(options = {}) ⇒ Object

people: comma separated user ids projects: comma separated project ids tags: comma separated tag ids from: entries from this date to: entries to this date billable: true only shows billable entries; false only shows unbillable entries



49
50
51
52
53
54
# File 'lib/freckle.rb', line 49

def entries(options = {})
  options.keys.each { |k| options["search[#{k}]"] = Array(options.delete(k)).flatten.join(',') }
  Array(JSON.parse(@connection['/entries.json'].get(:params => options))).flatten.map do |entry|
    Entry.new(entry['entry'], self)
  end
end

#projects(options = {}) ⇒ Object



37
38
39
40
41
# File 'lib/freckle.rb', line 37

def projects(options = {})
  Array(JSON.parse(@connection['/projects.json'].get)).flatten.map do |project|
    Project.new(project['project'], self)
  end
end

#user_by_email(email) ⇒ Object



33
34
35
# File 'lib/freckle.rb', line 33

def user_by_email(email)
  self.users.detect{ |u| u.email == email }
end

#users(options = {}) ⇒ Object

Query Methods



28
29
30
31
32
# File 'lib/freckle.rb', line 28

def users(options = {})
  Array(JSON.parse(@connection['/users.json'].get)).flatten.map do |user|
    User.new(user['user'], self)
  end
end