diff --git a/lib/noko/client.rb b/lib/noko/client.rb index 65c7eef..c86ac56 100644 --- a/lib/noko/client.rb +++ b/lib/noko/client.rb @@ -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 diff --git a/lib/noko/client/account.rb b/lib/noko/client/account.rb index 2e02f91..bcbf3d5 100644 --- a/lib/noko/client/account.rb +++ b/lib/noko/client/account.rb @@ -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 diff --git a/lib/noko/client/current_user.rb b/lib/noko/client/current_user.rb index 28103c4..e392ab6 100644 --- a/lib/noko/client/current_user.rb +++ b/lib/noko/client/current_user.rb @@ -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 diff --git a/lib/noko/client/entries.rb b/lib/noko/client/entries.rb index fd89204..de5aadc 100644 --- a/lib/noko/client/entries.rb +++ b/lib/noko/client/entries.rb @@ -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 diff --git a/lib/noko/client/expenses.rb b/lib/noko/client/expenses.rb index 945ea60..828e210 100644 --- a/lib/noko/client/expenses.rb +++ b/lib/noko/client/expenses.rb @@ -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 diff --git a/lib/noko/client/invoices.rb b/lib/noko/client/invoices.rb index e807823..e00787a 100644 --- a/lib/noko/client/invoices.rb +++ b/lib/noko/client/invoices.rb @@ -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 diff --git a/lib/noko/client/project_groups.rb b/lib/noko/client/project_groups.rb index de09b09..85efc75 100644 --- a/lib/noko/client/project_groups.rb +++ b/lib/noko/client/project_groups.rb @@ -1,33 +1,31 @@ # frozen_string_literal: true -module Noko - class Client - def get_project_groups(params = nil) - get('/v2/project_groups', params) - end +class Noko::Client + def get_project_groups(params = nil) + get('/v2/project_groups', params) + end - def get_project_group(id) - get("/v2/project_groups/#{id}") - end + def get_project_group(id) + get("/v2/project_groups/#{id}") + end - def create_project_group(attributes) - post('/v2/project_groups', attributes) - end + def create_project_group(attributes) + post('/v2/project_groups', attributes) + end - def get_project_group_entries(id, params = nil) - get("/v2/project_groups/#{id}/entries", params) - end + def get_project_group_entries(id, params = nil) + get("/v2/project_groups/#{id}/entries", params) + end - def get_project_group_projects(id, params = nil) - get("/v2/project_groups/#{id}/projects", params) - end + def get_project_group_projects(id, params = nil) + get("/v2/project_groups/#{id}/projects", params) + end - def update_project_group(id, attributes) - put("/v2/project_groups/#{id}", attributes) - end + def update_project_group(id, attributes) + put("/v2/project_groups/#{id}", attributes) + end - def delete_project_group(id) - delete("/v2/project_groups/#{id}") - end + def delete_project_group(id) + delete("/v2/project_groups/#{id}") end end diff --git a/lib/noko/client/projects.rb b/lib/noko/client/projects.rb index e486c71..506342d 100644 --- a/lib/noko/client/projects.rb +++ b/lib/noko/client/projects.rb @@ -1,57 +1,55 @@ # frozen_string_literal: true -module Noko - class Client - def get_projects(params = nil) - get('/v2/projects', params) - end - - def get_project(id) - get("/v2/projects/#{id}") - end - - def create_project(attributes) - post('/v2/projects', attributes) - end - - def get_project_entries(id, params = nil) - get("/v2/projects/#{id}/entries", params) - end - - def get_project_expenses(id, params = nil) - get("/v2/projects/#{id}/expenses", params) - end - - def update_project(id, attributes) - put("/v2/projects/#{id}", attributes) - end - - def merge_projects(id, project_id) - put("/v2/projects/#{id}/merge", project_id: project_id) - end - - def delete_project(id) - delete("/v2/projects/#{id}") - end - - def archive_project(id) - put("/v2/projects/#{id}/archive") - end - - def unarchive_project(id) - put("/v2/projects/#{id}/unarchive") - end - - def archive_projects(project_ids) - put('/v2/projects/archive', project_ids: project_ids) - end - - def unarchive_projects(project_ids) - put('/v2/projects/unarchive', project_ids: project_ids) - end - - def delete_projects(project_ids) - put('/v2/projects/delete', project_ids: project_ids) - end +class Noko::Client + def get_projects(params = nil) + get('/v2/projects', params) + end + + def get_project(id) + get("/v2/projects/#{id}") + end + + def create_project(attributes) + post('/v2/projects', attributes) + end + + def get_project_entries(id, params = nil) + get("/v2/projects/#{id}/entries", params) + end + + def get_project_expenses(id, params = nil) + get("/v2/projects/#{id}/expenses", params) + end + + def update_project(id, attributes) + put("/v2/projects/#{id}", attributes) + end + + def merge_projects(id, project_id) + put("/v2/projects/#{id}/merge", project_id: project_id) + end + + def delete_project(id) + delete("/v2/projects/#{id}") + end + + def archive_project(id) + put("/v2/projects/#{id}/archive") + end + + def unarchive_project(id) + put("/v2/projects/#{id}/unarchive") + end + + def archive_projects(project_ids) + put('/v2/projects/archive', project_ids: project_ids) + end + + def unarchive_projects(project_ids) + put('/v2/projects/unarchive', project_ids: project_ids) + end + + def delete_projects(project_ids) + put('/v2/projects/delete', project_ids: project_ids) end end diff --git a/lib/noko/client/tags.rb b/lib/noko/client/tags.rb index e4ac1e0..a79c0f6 100644 --- a/lib/noko/client/tags.rb +++ b/lib/noko/client/tags.rb @@ -1,37 +1,35 @@ # frozen_string_literal: true -module Noko - class Client - def get_tags(params = nil) - get('/v2/tags', params) - end - - def create_tags(names) - post('/v2/tags', names: names) - end - - def get_tag(id) - get("/v2/tags/#{id}") - end - - def get_tag_entries(id, params = nil) - get("/v2/tags/#{id}/entries", params) - end - - def update_tag(id, attributes) - put("/v2/tags/#{id}", attributes) - end - - def merge_tags(id, tag_id) - put("/v2/tags/#{id}/merge", tag_id: tag_id) - end - - def delete_tag(id) - delete("/v2/tags/#{id}") - end - - def delete_tags(tag_ids) - put('/v2/tags/delete', tag_ids: tag_ids) - end +class Noko::Client + def get_tags(params = nil) + get('/v2/tags', params) + end + + def create_tags(names) + post('/v2/tags', names: names) + end + + def get_tag(id) + get("/v2/tags/#{id}") + end + + def get_tag_entries(id, params = nil) + get("/v2/tags/#{id}/entries", params) + end + + def update_tag(id, attributes) + put("/v2/tags/#{id}", attributes) + end + + def merge_tags(id, tag_id) + put("/v2/tags/#{id}/merge", tag_id: tag_id) + end + + def delete_tag(id) + delete("/v2/tags/#{id}") + end + + def delete_tags(tag_ids) + put('/v2/tags/delete', tag_ids: tag_ids) end end diff --git a/lib/noko/client/teams.rb b/lib/noko/client/teams.rb index 69ce347..c0cc1b8 100644 --- a/lib/noko/client/teams.rb +++ b/lib/noko/client/teams.rb @@ -1,45 +1,43 @@ # frozen_string_literal: true -module Noko - class Client - def get_teams(params = nil) - get('/v2/teams', params) - end - - def get_team(id) - get("/v2/teams/#{id}") - end - - def create_team(attributes) - post('/v2/teams', attributes) - end - - def update_team(id, attributes) - put("/v2/teams/#{id}", attributes) - end - - def get_team_entries(id, params = nil) - get("/v2/teams/#{id}/entries", params) - end - - def get_team_users(id, params = nil) - get("/v2/teams/#{id}/users", params) - end - - def add_team_users(id, params) - post("/v2/teams/#{id}/add_users", params) - end - - def remove_team_users(id, params) - put("/v2/teams/#{id}/remove_users", params) - end - - def remove_all_team_users(id) - put("/v2/teams/#{id}/remove_all_users") - end - - def delete_team(id) - delete("/v2/teams/#{id}") - end +class Noko::Client + def get_teams(params = nil) + get('/v2/teams', params) + end + + def get_team(id) + get("/v2/teams/#{id}") + end + + def create_team(attributes) + post('/v2/teams', attributes) + end + + def update_team(id, attributes) + put("/v2/teams/#{id}", attributes) + end + + def get_team_entries(id, params = nil) + get("/v2/teams/#{id}/entries", params) + end + + def get_team_users(id, params = nil) + get("/v2/teams/#{id}/users", params) + end + + def add_team_users(id, params) + post("/v2/teams/#{id}/add_users", params) + end + + def remove_team_users(id, params) + put("/v2/teams/#{id}/remove_users", params) + end + + def remove_all_team_users(id) + put("/v2/teams/#{id}/remove_all_users") + end + + def delete_team(id) + delete("/v2/teams/#{id}") end end diff --git a/lib/noko/client/timers.rb b/lib/noko/client/timers.rb index 9cd0451..d18f83a 100644 --- a/lib/noko/client/timers.rb +++ b/lib/noko/client/timers.rb @@ -1,33 +1,31 @@ # frozen_string_literal: true -module Noko - class Client - def get_timers(params = nil) - get('/v2/timers', params) - end +class Noko::Client + def get_timers(params = nil) + get('/v2/timers', params) + end - def get_timer(project_id) - get("/v2/projects/#{project_id}/timer") - end + def get_timer(project_id) + get("/v2/projects/#{project_id}/timer") + end - def update_timer(project_id, attributes) - put("/v2/projects/#{project_id}/timer", attributes) - end + def update_timer(project_id, attributes) + put("/v2/projects/#{project_id}/timer", attributes) + end - def start_timer(project_id) - put("/v2/projects/#{project_id}/timer/start") - end + def start_timer(project_id) + put("/v2/projects/#{project_id}/timer/start") + end - def pause_timer(project_id) - put("/v2/projects/#{project_id}/timer/pause") - end + def pause_timer(project_id) + put("/v2/projects/#{project_id}/timer/pause") + end - def log_timer(project_id, attributes = {}) - put("/v2/projects/#{project_id}/timer/log", attributes) - end + def log_timer(project_id, attributes = {}) + put("/v2/projects/#{project_id}/timer/log", attributes) + end - def discard_timer(project_id) - delete("/v2/projects/#{project_id}/timer") - end + def discard_timer(project_id) + delete("/v2/projects/#{project_id}/timer") end end diff --git a/lib/noko/client/users.rb b/lib/noko/client/users.rb index 4235a9e..11c6100 100644 --- a/lib/noko/client/users.rb +++ b/lib/noko/client/users.rb @@ -1,41 +1,39 @@ # frozen_string_literal: true -module Noko - class Client - def get_users(params = nil) - get('/v2/users', params) - end - - def get_user(id) - get("/v2/users/#{id}") - end - - def get_user_entries(id, params = nil) - get("/v2/users/#{id}/entries", params) - end - - def get_user_expenses(id, params = nil) - get("/v2/users/#{id}/expenses", params) - end - - def create_user(attributes) - post('/v2/users', attributes) - end - - def update_user(id, attributes) - put("/v2/users/#{id}", attributes) - end - - def delete_user(id) - delete("/v2/users/#{id}") - end - - def reactivate_user(id) - put("/v2/users/#{id}/activate") - end - - def deactivate_user(id) - put("/v2/users/#{id}/deactivate") - end +class Noko::Client + def get_users(params = nil) + get('/v2/users', params) + end + + def get_user(id) + get("/v2/users/#{id}") + end + + def get_user_entries(id, params = nil) + get("/v2/users/#{id}/entries", params) + end + + def get_user_expenses(id, params = nil) + get("/v2/users/#{id}/expenses", params) + end + + def create_user(attributes) + post('/v2/users', attributes) + end + + def update_user(id, attributes) + put("/v2/users/#{id}", attributes) + end + + def delete_user(id) + delete("/v2/users/#{id}") + end + + def reactivate_user(id) + put("/v2/users/#{id}/activate") + end + + def deactivate_user(id) + put("/v2/users/#{id}/deactivate") end end diff --git a/lib/noko/client/webhooks.rb b/lib/noko/client/webhooks.rb index 886e31e..7e1d16b 100644 --- a/lib/noko/client/webhooks.rb +++ b/lib/noko/client/webhooks.rb @@ -1,43 +1,41 @@ -module Noko - class Client - def get_webhooks(params = nil) - get('/v2/webhooks', params) - end - - def get_webhook(id) - get("/v2/webhooks/#{id}") - end - - def create_webhook(attributes) - post('/v2/webhooks', attributes) - end - - def update_webhook(id, attributes) - put("/v2/webhooks/#{id}", attributes) - end - - def add_webhook_events(id, attributes) - put("/v2/webhooks/#{id}/add_events", attributes) - end - - def remove_webhook_events(id, attributes) - put("/v2/webhooks/#{id}/remove_events", attributes) - end - - def reroll_webhook_secret(id) - put("/v2/webhooks/#{id}/reroll_secret") - end - - def disable_webhook(id) - put("/v2/webhooks/#{id}/disable") - end - - def enable_webhook(id) - put("/v2/webhooks/#{id}/enable") - end - - def delete_webhook(id) - delete("/v2/webhooks/#{id}") - end +class Noko::Client + def get_webhooks(params = nil) + get('/v2/webhooks', params) + end + + def get_webhook(id) + get("/v2/webhooks/#{id}") + end + + def create_webhook(attributes) + post('/v2/webhooks', attributes) + end + + def update_webhook(id, attributes) + put("/v2/webhooks/#{id}", attributes) + end + + def add_webhook_events(id, attributes) + put("/v2/webhooks/#{id}/add_events", attributes) + end + + def remove_webhook_events(id, attributes) + put("/v2/webhooks/#{id}/remove_events", attributes) + end + + def reroll_webhook_secret(id) + put("/v2/webhooks/#{id}/reroll_secret") + end + + def disable_webhook(id) + put("/v2/webhooks/#{id}/disable") + end + + def enable_webhook(id) + put("/v2/webhooks/#{id}/enable") + end + + def delete_webhook(id) + delete("/v2/webhooks/#{id}") end end diff --git a/lib/noko/link_header.rb b/lib/noko/link_header.rb index 2a3c4e3..a72f990 100644 --- a/lib/noko/link_header.rb +++ b/lib/noko/link_header.rb @@ -1,16 +1,14 @@ require 'noko/record' require 'uri' -module Noko - module LinkHeader - extend self +module Noko::LinkHeader + extend self - REGEXP = /<([^>]+)>; rel="(\w+)"/ + REGEXP = /<([^>]+)>; rel="(\w+)"/ - def parse(string) - string.scan(REGEXP).each_with_object(Record.new) do |(uri, rel), record| - record[rel.to_sym] = URI.parse(uri).request_uri - end + def parse(string) + string.scan(REGEXP).each_with_object(Noko::Record.new) do |(uri, rel), record| + record[rel.to_sym] = URI.parse(uri).request_uri end end end diff --git a/lib/noko/params.rb b/lib/noko/params.rb index 0b253e6..b4abc7f 100644 --- a/lib/noko/params.rb +++ b/lib/noko/params.rb @@ -1,22 +1,20 @@ # frozen_string_literal: true require 'uri' -module Noko - module Params - extend self +module Noko::Params + extend self - def join(path, params = nil) - return path if params.nil? || params.empty? + def join(path, params = nil) + return path if params.nil? || params.empty? - path + '?' + encode(params) - end + path + '?' + encode(params) + end - def encode(params) - params.map { |k, v| escape(k) + '=' + Array(v).map { escape(_1) }.join(',') }.join('&') - end + def encode(params) + params.map { |k, v| escape(k) + '=' + Array(v).map { escape(_1) }.join(',') }.join('&') + end - def escape(value) - URI.encode_uri_component(value) - end + def escape(value) + URI.encode_uri_component(value) end end diff --git a/lib/noko/record.rb b/lib/noko/record.rb index 4eac1dc..ad1f223 100644 --- a/lib/noko/record.rb +++ b/lib/noko/record.rb @@ -1,37 +1,35 @@ # frozen_string_literal: true -module Noko - class Record - def initialize(attributes = {}) - @attributes = attributes - end +class Noko::Record + def initialize(attributes = {}) + @attributes = attributes + end - def [](name) - @attributes[name] - end + def [](name) + @attributes[name] + end - def []=(name, value) - @attributes[name] = value - end + def []=(name, value) + @attributes[name] = value + end - def inspect - '#<' + self.class.name + ' ' + @attributes.map { "#{_1}=#{_2.inspect}" }.join(', ') + '>' - end + def inspect + '#<' + self.class.name + ' ' + @attributes.map { "#{_1}=#{_2.inspect}" }.join(', ') + '>' + end - def method_missing(name, *args, &block) - if @attributes.key?(name) && args.empty? && block.nil? - return @attributes[name] - else - super name, *args, &block - end + def method_missing(name, *args, &block) + if @attributes.key?(name) && args.empty? && block.nil? + return @attributes[name] + else + super name, *args, &block end + end - def respond_to_missing?(name, include_private = false) - @attributes.key?(name) - end + def respond_to_missing?(name, include_private = false) + @attributes.key?(name) + end - def to_h - @attributes - end + def to_h + @attributes end end diff --git a/lib/noko/response.rb b/lib/noko/response.rb index c19b3ef..33d92ce 100644 --- a/lib/noko/response.rb +++ b/lib/noko/response.rb @@ -2,49 +2,47 @@ require 'net/http' require 'json' -module Noko - module Response - extend self +module Noko::Response + extend self - def parse(response) - if response.is_a?(Net::HTTPNoContent) - return :no_content - end - - if response.content_type == 'application/json' - object = JSON.parse(response.body, symbolize_names: true, object_class: Record) + def parse(response) + if response.is_a?(Net::HTTPNoContent) + return :no_content + end - if response['Link'] - object.singleton_class.module_eval { attr_accessor :link } - object.link = LinkHeader.parse(response['Link']) - end + if response.content_type == 'application/json' + object = JSON.parse(response.body, symbolize_names: true, object_class: Noko::Record) - return object + if response['Link'] + object.singleton_class.module_eval { attr_accessor :link } + object.link = Noko::LinkHeader.parse(response['Link']) end - response.body + return object end - def error(response) - if response.content_type == 'application/json' - body = JSON.parse(response.body) + response.body + end + + def error(response) + if response.content_type == 'application/json' + body = JSON.parse(response.body) - error_class(response).new(body['message']) - else - error_class(response) - end + error_class(response).new(body['message']) + else + error_class(response) end + end - def error_class(object) - case object - when Net::HTTPBadRequest then Noko::ClientError - when Net::HTTPUnauthorized then Noko::AuthenticationError - when Net::HTTPNotFound then Noko::NotFound - when Net::HTTPClientError then Noko::ClientError - when Net::HTTPServerError then Noko::ServerError - else - Noko::Error - end + def error_class(object) + case object + when Net::HTTPBadRequest then Noko::ClientError + when Net::HTTPUnauthorized then Noko::AuthenticationError + when Net::HTTPNotFound then Noko::NotFound + when Net::HTTPClientError then Noko::ClientError + when Net::HTTPServerError then Noko::ServerError + else + Noko::Error end end end