Skip to content

Commit

Permalink
Formatting: define inner modules using ::
Browse files Browse the repository at this point in the history
  • Loading branch information
timcraft committed Dec 10, 2024
1 parent a559b59 commit 2ee573f
Show file tree
Hide file tree
Showing 17 changed files with 465 additions and 499 deletions.
98 changes: 48 additions & 50 deletions lib/noko/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,67 @@
require 'net/http'
require 'json'

module Noko
class Client
def initialize(options = {})
if options.key?(:access_token)
@auth_header, @auth_value = 'Authorization', "token #{options[:access_token]}"
elsif options.key?(:token)
@auth_header, @auth_value = 'X-NokoToken', options.fetch(:token)
elsif !(netrc = load_netrc_credentials).nil?
@auth_header, @auth_value = 'X-NokoToken', netrc.password
else
raise ArgumentError, 'access_token or token required for authentication'
end

@user_agent = options.fetch(:user_agent) { "noko/#{VERSION} ruby/#{RUBY_VERSION}" }

@host = 'api.nokotime.com'

@http = Net::HTTP.new(@host, Net::HTTP.https_default_port)

@http.use_ssl = true
class Noko::Client
def initialize(options = {})
if options.key?(:access_token)
@auth_header, @auth_value = 'Authorization', "token #{options[:access_token]}"
elsif options.key?(:token)
@auth_header, @auth_value = 'X-NokoToken', options.fetch(:token)
elsif !(netrc = load_netrc_credentials).nil?
@auth_header, @auth_value = 'X-NokoToken', netrc.password
else
raise ArgumentError, 'access_token or token required for authentication'
end

def get(path, params = nil)
request(Net::HTTP::Get.new(Params.join(path, params)))
end
@user_agent = options.fetch(:user_agent) { "noko/#{Noko::VERSION} ruby/#{RUBY_VERSION}" }

private
@host = 'api.nokotime.com'

def post(path, attributes)
request(Net::HTTP::Post.new(path), attributes)
end
@http = Net::HTTP.new(@host, Net::HTTP.https_default_port)

def put(path, attributes = nil)
request(Net::HTTP::Put.new(path), attributes)
end
@http.use_ssl = true
end

def delete(path)
request(Net::HTTP::Delete.new(path))
end
def get(path, params = nil)
request(Net::HTTP::Get.new(Noko::Params.join(path, params)))
end

private

def request(http_request, body_object = nil)
http_request['User-Agent'] = @user_agent
http_request[@auth_header] = @auth_value
def post(path, attributes)
request(Net::HTTP::Post.new(path), attributes)
end

def put(path, attributes = nil)
request(Net::HTTP::Put.new(path), attributes)
end

if body_object
http_request['Content-Type'] = 'application/json'
http_request.body = JSON.generate(body_object)
end
def delete(path)
request(Net::HTTP::Delete.new(path))
end

response = @http.request(http_request)
def request(http_request, body_object = nil)
http_request['User-Agent'] = @user_agent
http_request[@auth_header] = @auth_value

if response.is_a?(Net::HTTPSuccess)
Response.parse(response)
else
raise Response.error(response)
end
if body_object
http_request['Content-Type'] = 'application/json'
http_request.body = JSON.generate(body_object)
end

def load_netrc_credentials
require 'net/netrc'
response = @http.request(http_request)

Net::Netrc.locate('api.nokotime.com')
rescue LoadError
if response.is_a?(Net::HTTPSuccess)
Noko::Response.parse(response)
else
raise Noko::Response.error(response)
end
end

def load_netrc_credentials
require 'net/netrc'

Net::Netrc.locate('api.nokotime.com')
rescue LoadError
end
end
8 changes: 3 additions & 5 deletions lib/noko/client/account.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# frozen_string_literal: true

module Noko
class Client
def get_account
get('/v2/account')
end
class Noko::Client
def get_account
get('/v2/account')
end
end
26 changes: 12 additions & 14 deletions lib/noko/client/current_user.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# frozen_string_literal: true

module Noko
class Client
def get_current_user
get('/v2/current_user')
end
class Noko::Client
def get_current_user
get('/v2/current_user')
end

def get_current_user_entries(params = nil)
get('/v2/current_user/entries', params)
end
def get_current_user_entries(params = nil)
get('/v2/current_user/entries', params)
end

def get_current_user_expenses(params = nil)
get('/v2/current_user/expenses', params)
end
def get_current_user_expenses(params = nil)
get('/v2/current_user/expenses', params)
end

def update_current_user(attributes)
put('/v2/current_user', attributes)
end
def update_current_user(attributes)
put('/v2/current_user', attributes)
end
end
88 changes: 43 additions & 45 deletions lib/noko/client/entries.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
# frozen_string_literal: true

module Noko
class Client
def get_entries(params = nil)
get('/v2/entries', params)
end

def get_entry(id)
get("/v2/entries/#{id}")
end

def create_entry(attributes)
post('/v2/entries', attributes)
end

def update_entry(id, attributes)
put("/v2/entries/#{id}", attributes)
end

def delete_entry(id)
delete("/v2/entries/#{id}")
end

def mark_entry_invoiced(id, params)
put("/v2/entries/#{id}/marked_as_invoiced", params)
end

def mark_entries_invoiced(params)
put('/v2/entries/marked_as_invoiced', params)
end

def mark_entry_approved(id, params = nil)
put("/v2/entries/#{id}/approved", params)
end

def mark_entries_approved(params)
put('/v2/entries/approved', params)
end

def mark_entry_unapproved(id)
put("/v2/entries/#{id}/unapproved")
end

def mark_entries_unapproved(params)
put('/v2/entries/unapproved', params)
end
class Noko::Client
def get_entries(params = nil)
get('/v2/entries', params)
end

def get_entry(id)
get("/v2/entries/#{id}")
end

def create_entry(attributes)
post('/v2/entries', attributes)
end

def update_entry(id, attributes)
put("/v2/entries/#{id}", attributes)
end

def delete_entry(id)
delete("/v2/entries/#{id}")
end

def mark_entry_invoiced(id, params)
put("/v2/entries/#{id}/marked_as_invoiced", params)
end

def mark_entries_invoiced(params)
put('/v2/entries/marked_as_invoiced', params)
end

def mark_entry_approved(id, params = nil)
put("/v2/entries/#{id}/approved", params)
end

def mark_entries_approved(params)
put('/v2/entries/approved', params)
end

def mark_entry_unapproved(id)
put("/v2/entries/#{id}/unapproved")
end

def mark_entries_unapproved(params)
put('/v2/entries/unapproved', params)
end
end
32 changes: 15 additions & 17 deletions lib/noko/client/expenses.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
# frozen_string_literal: true

module Noko
class Client
def get_expenses(params = nil)
get('/v2/expenses', params)
end
class Noko::Client
def get_expenses(params = nil)
get('/v2/expenses', params)
end

def get_expense(id)
get("/v2/expenses/#{id}")
end
def get_expense(id)
get("/v2/expenses/#{id}")
end

def create_expense(attributes)
post('/v2/expenses', attributes)
end
def create_expense(attributes)
post('/v2/expenses', attributes)
end

def update_expense(id, attributes)
put("/v2/expenses/#{id}", attributes)
end
def update_expense(id, attributes)
put("/v2/expenses/#{id}", attributes)
end

def delete_expense(id)
delete("/v2/expenses/#{id}")
end
def delete_expense(id)
delete("/v2/expenses/#{id}")
end
end
72 changes: 35 additions & 37 deletions lib/noko/client/invoices.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
# frozen_string_literal: true

module Noko
class Client
def get_invoices(params = nil)
get('/v2/invoices', params)
end

def get_invoice(id)
get("/v2/invoices/#{id}")
end

def create_invoice(attributes)
post('/v2/invoices', attributes)
end

def update_invoice(id, attributes)
put("/v2/invoices/#{id}", attributes)
end

def mark_invoice_paid(id)
put("/v2/invoices/#{id}/paid")
end

def mark_invoice_unpaid(id)
put("/v2/invoices/#{id}/unpaid")
end

def get_invoice_entries(id, params = nil)
get("/v2/invoices/#{id}/entries", params)
end

def get_invoice_expenses(id, params = nil)
get("/v2/invoices/#{id}/expenses", params)
end

def delete_invoice(id)
delete("/v2/invoices/#{id}")
end
class Noko::Client
def get_invoices(params = nil)
get('/v2/invoices', params)
end

def get_invoice(id)
get("/v2/invoices/#{id}")
end

def create_invoice(attributes)
post('/v2/invoices', attributes)
end

def update_invoice(id, attributes)
put("/v2/invoices/#{id}", attributes)
end

def mark_invoice_paid(id)
put("/v2/invoices/#{id}/paid")
end

def mark_invoice_unpaid(id)
put("/v2/invoices/#{id}/unpaid")
end

def get_invoice_entries(id, params = nil)
get("/v2/invoices/#{id}/entries", params)
end

def get_invoice_expenses(id, params = nil)
get("/v2/invoices/#{id}/expenses", params)
end

def delete_invoice(id)
delete("/v2/invoices/#{id}")
end
end
Loading

0 comments on commit 2ee573f

Please sign in to comment.