Last active
March 16, 2018 10:21
-
-
Save srpouyet/48714adc0833149cd077216d25259e84 to your computer and use it in GitHub Desktop.
ROM Exact Online adapter
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
require "rom/http" | |
require "json" | |
require "uri" | |
require "faraday" | |
require "typhoeus" | |
require "typhoeus/adapters/faraday" | |
module ROM | |
module ExactOnline | |
class Dataset < ROM::HTTP::Dataset | |
# option :base_path, type: Types::String, default: 'api/v1' | |
# option :division, type: Types::Int | |
configure do |config| | |
# config.param_encoder = | |
config.default_request_handler = ->(dataset) do | |
uri = dataset.uri | |
conn = Faraday.new(url: "#{uri.scheme}://#{uri.host}") do |faraday| | |
faraday.adapter :typhoeus | |
faraday.headers = dataset.headers | |
end | |
conn.send(dataset.request_method, dataset.path) | |
end | |
config.default_response_handler = ->(response, dataset) do | |
if %i(post put patch).include?(dataset.request_method) | |
JSON.parse(response.body, symbolize_names: false) | |
else | |
Array([JSON.parse(response.body, symbolize_names: false)]).flatten | |
end | |
end | |
end | |
def headers | |
super.merge({ 'Accept' => 'application/json', 'Content-Type' => 'application/json' }) | |
end | |
end | |
end | |
end |
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
require 'rom' | |
require 'rom/exact_online/dataset' | |
require 'rom/exact_online/gateway' | |
require 'rom/exact_online/relation' | |
ROM.register_adapter(:exact_online, ROM::ExactOnline) |
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
require "rom/http" | |
module ROM | |
module ExactOnline | |
class Gateway < ROM::HTTP::Gateway | |
end | |
end | |
end |
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
require "pipes/import" | |
module ExactOnline | |
module Relations | |
class Receivables < ROM::Relation[:exact_online] | |
# include Pipes::Import.args["setting_repo"] | |
# option :division, type: Types::Int | |
schema(:receivables) do | |
attribute 'ID', ROM::Types::String | |
primary_key 'ID' | |
end | |
# How do I inject a setting object in this Relation? | |
# https://start.exactonline.nl/api/v1/123456/cashflow/Receivables | |
dataset { with_base_path("api/v1/#{setting.division_id}").with_path('cashflow/Receivables') } | |
def all | |
to_a | |
end | |
end | |
end | |
end |
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
require "rom/http" | |
module ROM | |
module ExactOnline | |
class Relation < ROM::HTTP::Relation | |
adapter :exact_online | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment