-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - abstract some logic into resourceable class
- Loading branch information
Showing
3 changed files
with
49 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blnk | ||
# Resoureable module that bring some tweaks for basic REST api integration | ||
class Resourceable < OpenStruct | ||
include Client | ||
|
||
def self.resource_name = raise NotImplementedError | ||
|
||
def self.find(id) | ||
response = new.get_request(path: "/#{resource_name}/#{id}") | ||
return response unless response.status.success? | ||
|
||
new response.parse | ||
end | ||
|
||
def self.all | ||
response = new.get_request(path: "/#{resource_name}") | ||
return response unless response.status.success? | ||
|
||
response.parse.map do |r| | ||
new r | ||
end | ||
end | ||
|
||
def self.create(**create_args) | ||
new(**create_args).save | ||
end | ||
|
||
def save | ||
return self if persisted? | ||
|
||
response = post_request(path: "/#{self.class.resource_name}", body: body_data) | ||
return response unless response.status.success? | ||
|
||
response.parse.each_pair { |k, v| self[k] = v } | ||
self | ||
end | ||
|
||
def create_args = {} | ||
def persisted? = raise NotImplementedError | ||
def body_data = raise NotImplementedError | ||
end | ||
end |