Class: Tenk::Client

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

Overview

One client of the 10k API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Client

Initialize a new Tenk client

Parameters:

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

    configuration options for the client

  • block (Block)

    a block which will receive the new client as an arg

See Also:



24
25
26
27
28
29
# File 'lib/client.rb', line 24

def initialize(options = {}, &block)
  self.configuration = Configuration.new(options)
  self.logger = configuration.logger

  yield_or_eval(&block) if block_given?
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



17
18
19
# File 'lib/client.rb', line 17

def configuration
  @configuration
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#approvalsTenk::Approvals

Access the Approvals resource using this client

Returns:



141
142
143
# File 'lib/client.rb', line 141

def approvals
  @_approvals ||= Tenk::Approvals.new(self)
end

#bill_ratesTenk::BillRates

Access the BillRates resource using this client

Returns:



135
136
137
# File 'lib/client.rb', line 135

def bill_rates
  @_bill_rates ||= Tenk::BillRates.new(self)
end

#connectionObject

The Faraday connection used by this client



39
40
41
42
43
# File 'lib/client.rb', line 39

def connection
  Faraday.new(url: configuration.api_base) do |faraday|
    faraday.adapter Faraday.default_adapter
  end
end

#delete(url, url_opts = {}) ⇒ Hashie::Mash

Make a DELETE request to the API

Parameters:

  • url (String)

    the URL where to send the request

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

    the request payload, which will become query params

Returns:

  • (Hashie::Mash)

    the API response wrapped in a Hashie::Mash



105
106
107
# File 'lib/client.rb', line 105

def delete(url, url_opts = {})
  request(url, :delete, url_opts)
end

#get(url, url_opts = {}) ⇒ Hashie::Mash

Make a GET request to the API

Parameters:

  • url (String)

    the URL where to send the request

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

    the request payload, which will become query params

Returns:

  • (Hashie::Mash)

    the API response wrapped in a Hashie::Mash



65
66
67
# File 'lib/client.rb', line 65

def get(url, url_opts = {})
  request(url, :get, url_opts)
end

#hashify_response(resp) ⇒ Hashie::Mash

Take a Farady response and turn it into a Hashie::Mash

Parameters:

  • resp (Faraday::Response)

    a response provided by Faraday

Returns:

  • (Hashie::Mash)

    a Hashie::Mash version of the response body



148
149
150
151
# File 'lib/client.rb', line 148

def hashify_response(resp)
  return true if resp.body.empty?
  Hashie::Mash.new(JSON.parse(resp.body))
end

#placeholder_resourcesTenk::PlaceholderResources

Access the PlaceholderResources resource using this client

Returns:



117
118
119
# File 'lib/client.rb', line 117

def placeholder_resources
  @_placeholder_resources ||= Tenk::PlaceholderResources.new(self)
end

#post(url, url_opts = {}) ⇒ Hashie::Mash

Make a POST request to the API

Parameters:

  • url (String)

    the URL where to send the request

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

    the request payload, which will become query params

Returns:

  • (Hashie::Mash)

    the API response wrapped in a Hashie::Mash



73
74
75
# File 'lib/client.rb', line 73

def post(url, url_opts = {})
  request(url, :post, url_opts)
end

#post_with_body(url, url_opts = {}) ⇒ Hashie::Mash

Make a POST request to the API and use url_opts as a body payload instead of query params

Parameters:

  • url (String)

    the URL where to send the request

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

    the request payload, which will become the POST body

Returns:

  • (Hashie::Mash)

    the API response wrapped in a Hashie::Mash



82
83
84
85
86
87
88
89
90
91
# File 'lib/client.rb', line 82

def post_with_body(url, url_opts = {})
  hashify_response(
    get_connection.post do |req|
      req.url configuration.api_base + url
      req.body = JSON.generate(url_opts)
      req.headers['Content-Type'] = 'application/json'
      req.headers['auth'] = configuration.token
    end,
  )
end

#projectsTenk::Projects

Access the Project resource using this client

Returns:



123
124
125
# File 'lib/client.rb', line 123

def projects
  @_projects ||= Tenk::Projects.new(self)
end

#put(url, url_opts = {}) ⇒ Hashie::Mash

Make a PUT request to the API

Parameters:

  • url (String)

    the URL where to send the request

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

    the request payload, which will become query params

Returns:

  • (Hashie::Mash)

    the API response wrapped in a Hashie::Mash



97
98
99
# File 'lib/client.rb', line 97

def put(url, url_opts = {})
  request(url, :put, url_opts)
end

#request(url, method, url_opts = {}) ⇒ Hashie::Mash

Make a request to url, with a given method and options, and wrap the response in a Hashie::Mash

Parameters:

  • url (String)

    the URL where to send the request

  • method (Symbol)

    the HTTP method to use to send the request

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

    the request payload, which will become query params

Returns:

  • (Hashie::Mash)

    the API response wrapped in a Hashie::Mash



51
52
53
54
55
56
57
58
59
# File 'lib/client.rb', line 51

def request(url, method, url_opts = {})
  hashify_response(
    connection.send(method) do |req|
      req.url configuration.api_base + url, url_opts
      req.headers['Content-Type'] = 'application/json'
      req.headers['auth'] = configuration.token
    end,
  )
end

#time_entriesTenk::TimeEntries

Access the TimeEntry resource using this client

Returns:



129
130
131
# File 'lib/client.rb', line 129

def time_entries
  @_time_entries ||= Tenk::TimeEntries.new(self)
end

#usersTenk::Users

Access the User resource using this client

Returns:



111
112
113
# File 'lib/client.rb', line 111

def users
  @_users ||= Tenk::Users.new(self)
end

#yield_or_eval(&block) ⇒ Object

If a block is given, invoke it with this client. If the block takes no args, then just execute it as an instance of the client



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

def yield_or_eval(&block)
  return unless block
  block.arity > 0 ? yield(self) : instance_eval(&block)
end