Skip to content

Instantly share code, notes, and snippets.

@srpouyet
Last active March 16, 2018 10:21
Show Gist options
  • Save srpouyet/48714adc0833149cd077216d25259e84 to your computer and use it in GitHub Desktop.
Save srpouyet/48714adc0833149cd077216d25259e84 to your computer and use it in GitHub Desktop.
ROM Exact Online adapter
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
require 'rom'
require 'rom/exact_online/dataset'
require 'rom/exact_online/gateway'
require 'rom/exact_online/relation'
ROM.register_adapter(:exact_online, ROM::ExactOnline)
require "rom/http"
module ROM
module ExactOnline
class Gateway < ROM::HTTP::Gateway
end
end
end
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
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