Class: Rescuetime::Client

Inherits:
Object
  • Object
show all
Includes:
QueryBuildable
Defined in:
lib/rescuetime/client.rb

Overview

Rescuetime::Client makes HTTP requests to the RescueTime API and returns the appropriate values

Since:

  • v0.1.0

Constant Summary

Constants included from QueryBuildable

QueryBuildable::BASE_PARAMS, QueryBuildable::VALID

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QueryBuildable

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

Constructor Details

#initialize(key = nil, api_key: nil) ⇒ Rescuetime::Client

Creates a new Rescuetime client. The API key can be passed as either a parameter or option (with “key” taking priority over “:api_key”)

Examples:

Basic Use

Rescuetime::Client.new
# => #<Rescuetime::Client:0x007f938489b6c8 @api_key=nil>

Setting the API Key

Rescuetime::Client.new
# => #<Rescuetime::Client:0x007f938489b6c8 @api_key=nil>

Rescuetime::Client.new('12345')
# => #<Rescuetime::Client:0x007f938489b6c8 @api_key="12345">

Rescuetime::Client.new(api_key: 12345')
# => #<Rescuetime::Client:0x007f938489b6c8 @api_key="12345">

Rescuetime::Client.new('123', api_key: '456')
# => #<Rescuetime::Client:0x007f938489b6c8 @api_key="123">

Parameters:

  • key (String, nil) (defaults to: nil)

    API key

  • api_key (String) (defaults to: nil)

    Rescuetime API key

Since:

  • v0.1.0



46
47
48
# File 'lib/rescuetime/client.rb', line 46

def initialize(key = nil, api_key: nil)
  @api_key = key || api_key
end

Instance Attribute Details

#api_keyString?

Represents the client’s Rescuetime API key, required for successful requests

Returns:

  • (String, nil)

    Rescuetime API key

Since:

  • v0.1.0



21
22
23
# File 'lib/rescuetime/client.rb', line 21

def api_key
  @api_key
end

Instance Method Details

#api_key?Boolean

Returns true if the API key is present.

Examples:

Basic Use

client = Rescuetime::Client.new
client.api_key?
# => false

client.api_key = ''
client.api_key?
# => false

client.api_key = '12345'
client.api_key?
# => true

Returns:

  • (Boolean)

Since:

  • v0.1.0



66
67
68
69
# File 'lib/rescuetime/client.rb', line 66

def api_key?
  key = CoreExtensions::String.new api_key.to_s
  key.present?
end

#valid_credentials?Boolean

Returns true if the provided api key is valid. Performs a request to the Rescuetime API.

Examples:

Basic Use

# Assuming that INVALID_KEY is an invalid Rescuetime API key
# and VALID_KEY is a valid one

client = Rescuetime::Client
client.valid_credentials?
# => false

client.api_key = INVALID_KEY
client.valid_credentials? # Performs a request to the Rescuetime API
# => false

client.api_key = VALID_KEY
client.valid_credentials? # Performs a request to the Rescuetime API
# => true

Returns:

  • (Boolean)

Since:

  • v0.1.0



91
92
93
94
95
96
# File 'lib/rescuetime/client.rb', line 91

def valid_credentials?
  return false unless api_key?
  !activities.all.nil?
rescue Rescuetime::Errors::InvalidCredentialsError
  false
end