Sha256: a24420b96f91ec46a72023dfac189afc1d960175426f877afaf5455183aa13f3

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

require_relative 'base'
require 'json'

module Manifestly
  module Entity
    class Endpoint < Base
      @client = nil

      def path
        self.class.path
      end

      def self.path
        raise 'Must override method'
      end

      def client
        self.class.client
      end

      def self.client
        @client ||= Manifestly::Client.new
      end

      def create
        response = client.post(path, params: to_h)
        json_entity = JSON.parse(response[:body], symbolize_names: true)[path.chomp('s').to_sym]
        self.attributes = json_entity
        self
      end

      def self.list(**params)
        response = client.get(path, params: params)
        json_entities = JSON.parse(response[:body], symbolize_names: true)[path.to_sym]
        json_entities.map { |it| new(it) }
      end

      def self.get(id)
        response = client.get("#{path}/#{id}")
        json_entity = JSON.parse(response[:body], symbolize_names: true)[path.chomp('s').to_sym]
        new(json_entity)
      end

      def update
        client.post("#{path}/#{id}", params: to_h)
        self
      end

      def save
        if id
          update
        else
          create
        end
      end

      def delete
        client.delete(path, params: {external_id: external_id})
        nil
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
manifestly-client-0.0.1 lib/manifestly/entity/endpoint.rb
manifestly-client-0.0.0 lib/manifestly/entity/endpoint.rb