Class: Tenk::Client
- Inherits:
-
Object
- Object
- Tenk::Client
- Defined in:
- lib/client.rb
Overview
One client of the 10k API
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#approvals ⇒ Tenk::Approvals
Access the Approvals resource using this client.
-
#bill_rates ⇒ Tenk::BillRates
Access the BillRates resource using this client.
-
#connection ⇒ Object
The Faraday connection used by this client.
-
#delete(url, url_opts = {}) ⇒ Hashie::Mash
Make a DELETE request to the API.
-
#get(url, url_opts = {}) ⇒ Hashie::Mash
Make a GET request to the API.
-
#hashify_response(resp) ⇒ Hashie::Mash
Take a Farady response and turn it into a Hashie::Mash.
-
#initialize(options = {}, &block) ⇒ Client
constructor
Initialize a new Tenk client.
-
#placeholder_resources ⇒ Tenk::PlaceholderResources
Access the PlaceholderResources resource using this client.
-
#post(url, url_opts = {}) ⇒ Hashie::Mash
Make a POST request to the API.
-
#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.
-
#projects ⇒ Tenk::Projects
Access the Project resource using this client.
-
#put(url, url_opts = {}) ⇒ Hashie::Mash
Make a PUT request to the API.
-
#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.
-
#time_entries ⇒ Tenk::TimeEntries
Access the TimeEntry resource using this client.
-
#users ⇒ Tenk::Users
Access the User resource using this client.
-
#yield_or_eval(&block) ⇒ Object
If a block is given, invoke it with this client.
Constructor Details
#initialize(options = {}, &block) ⇒ Client
Initialize a new Tenk client
24 25 26 27 28 29 |
# File 'lib/client.rb', line 24 def initialize( = {}, &block) self.configuration = Configuration.new() self.logger = configuration.logger yield_or_eval(&block) if block_given? end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
17 18 19 |
# File 'lib/client.rb', line 17 def configuration @configuration end |
#logger ⇒ Object
Returns the value of attribute logger.
18 19 20 |
# File 'lib/client.rb', line 18 def logger @logger end |
Instance Method Details
#approvals ⇒ Tenk::Approvals
Access the Approvals resource using this client
141 142 143 |
# File 'lib/client.rb', line 141 def approvals @_approvals ||= Tenk::Approvals.new(self) end |
#bill_rates ⇒ Tenk::BillRates
Access the BillRates resource using this client
135 136 137 |
# File 'lib/client.rb', line 135 def bill_rates @_bill_rates ||= Tenk::BillRates.new(self) end |
#connection ⇒ Object
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
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
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
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_resources ⇒ Tenk::PlaceholderResources
Access the PlaceholderResources resource using this client
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
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
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 |
#projects ⇒ Tenk::Projects
Access the Project resource using this client
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
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
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_entries ⇒ Tenk::TimeEntries
Access the TimeEntry resource using this client
129 130 131 |
# File 'lib/client.rb', line 129 def time_entries @_time_entries ||= Tenk::TimeEntries.new(self) end |
#users ⇒ Tenk::Users
Access the User resource using this client
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 |