Add resource framework: Base, CRUD mixins, and shared drafts/attachments/payments concerns
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "faraday/multipart"
|
||||
|
||||
module Fiken
|
||||
module Resources
|
||||
module Concerns
|
||||
# /<parent>/attachments — list (where supported) and multipart upload.
|
||||
class Attachments < Resource::Base
|
||||
def initialize(client, company_slug, parent_path, listable: true)
|
||||
super(client, company_slug)
|
||||
@parent_path = parent_path
|
||||
@listable = listable
|
||||
end
|
||||
|
||||
def base_path
|
||||
"#{@parent_path}/attachments"
|
||||
end
|
||||
|
||||
def list
|
||||
raise Error, "this resource does not support listing attachments" unless @listable
|
||||
|
||||
get_collection(base_path, {})
|
||||
end
|
||||
|
||||
# Upload a file. Provide either path: (a filename on disk) or io: + filename:.
|
||||
# Extra Fiken fields (e.g. comment:, attachToPayment:) are passed as form fields.
|
||||
def add(path: nil, io: nil, filename: nil, content_type: "application/octet-stream", **fields)
|
||||
parts = { "file" => build_file_part(path, io, filename, content_type) }
|
||||
fields.each { |key, value| parts[key.to_s] = value.to_s unless value.nil? }
|
||||
build_created(connection.post_multipart(base_path, parts))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Fiken
|
||||
module Resources
|
||||
module Concerns
|
||||
# /<parent>/drafts — the draft lifecycle shared by invoices, credit notes,
|
||||
# offers, order confirmations, sales and purchases.
|
||||
class Drafts < Resource::Base
|
||||
include Resource::Listable
|
||||
include Resource::Findable
|
||||
include Resource::Creatable
|
||||
include Resource::Updatable # drafts update via PUT
|
||||
include Resource::Deletable
|
||||
|
||||
def initialize(client, company_slug, parent_path, create_action)
|
||||
super(client, company_slug)
|
||||
@parent_path = parent_path
|
||||
@create_action = create_action
|
||||
end
|
||||
|
||||
def base_path
|
||||
"#{@parent_path}/drafts"
|
||||
end
|
||||
|
||||
def attachments(draft_id)
|
||||
Attachments.new(client, company_slug, "#{base_path}/#{draft_id}")
|
||||
end
|
||||
|
||||
# Finalize a draft into its document (e.g. createInvoice, createSale).
|
||||
def create_document(draft_id, attributes = nil)
|
||||
post_create("#{base_path}/#{draft_id}/#{@create_action}", attributes)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Fiken
|
||||
module Resources
|
||||
module Concerns
|
||||
# /<parent>/payments — shared by sales and purchases.
|
||||
class Payments < Resource::Base
|
||||
include Resource::Listable
|
||||
include Resource::Findable
|
||||
include Resource::Creatable
|
||||
|
||||
def initialize(client, company_slug, parent_path)
|
||||
super(client, company_slug)
|
||||
@parent_path = parent_path
|
||||
end
|
||||
|
||||
def base_path
|
||||
"#{@parent_path}/payments"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user