Sha256: 67d9032cd8e0c4b62b2b599505151972300158964e0525e7a1fce31ed9ddd3f5
Contents?: true
Size: 1.9 KB
Versions: 2
Compression:
Stored size: 1.9 KB
Contents
module Contactually class Base attr_reader :url, :interface def self.implements(*method_list) @implements = method_list end def self.implement_list @implements end def initialize(url:, interface:) @url = url @interface = interface end def list(params = {}) check_implementation(:list) Response.new(interface.get(url, params), self) end def list_each(params = {}, &block) check_implementation(:list) if block_given? list_in_batches(params) do |items| items.each do |item| block.call(item) end end else enum_for :list_each, params end end def create(body) check_implementation(:create) Response.new(interface.post(build_fetch_url, body), self) end def fetch(id, params = {}) check_implementation(:fetch) Response.new(interface.get(build_fetch_url(id), params), self) end def update(id, body) check_implementation(:update) Response.new(interface.patch(build_fetch_url(id), body), self) end def destroy(id) check_implementation(:destroy) interface.destroy(build_fetch_url(id)) end def check_implementation(method) raise 'Error' unless self.class.implement_list.include?(method) end def model OpenStruct end private def build_fetch_url(id = nil) if id url + "/" + id.to_s else url end end def list_in_batches(params) collection = Response.new(interface.get(url, params), self).data return unless collection.total > 0 while collection.items.any? yield collection break unless collection.has_next_page? url = collection.next_page _old_page = params.delete(:page) collection = Response.new(interface.get(url, params), self).data end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
contactually-rb-0.1.3 | lib/contactually/base.rb |
contactually-rb-0.1.2 | lib/contactually/base.rb |