Sha256: 5a87e562f3816a1b5ae9060a83c6cf8f419c7d8a490898e1d220e5364c5b48c5

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

module Rapidash
  class Base

    include Urlable
    include Resourceable

    attr_accessor :url, :options, :client

    class << self
      attr_accessor :root_element

      def root(name)
        @root_element = name.to_sym
      end
    end

    def initialize(*args)
      @client, @id, options = args

      if @id.is_a?(Hash)
        options = @id
        @id = nil
      end

      @options ||= {}
      @options.merge!(options || {})
      @url = "#{base_url}#{self.class.to_s.split("::")[-1].downcase}"
      @url += "/#{@id}" if @id
    end

    def create!(params)
      options[:method] = :post
      set_body!(params)
      call!
    end

    def update!(params)
      options[:method] = client.class.patch ? :patch : :put
      set_body!(params)
      call!
    end

    def delete!
      options[:method] = :delete
      call!
    end


    def call!
      self.options ||= {}
      options.delete(:previous_url)
      options[:header] ||= {}
      options[:header]["content-type"] = "application/json"
      method = options.delete(:method) || :get
      client.send(method, url, options)
    end

    private

    def set_body!(params)
      if self.class.root_element
        options[:body] = {self.class.root_element => params}.to_json
      else
        options[:body] = params.to_json
      end
    end

    def base_url
      if old_url = self.options[:previous_url]
        return "#{old_url}/"
      end
      ""
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rapidash-0.1.1 lib/rapidash/base.rb
rapidash-0.1.0 lib/rapidash/base.rb
rapidash-0.0.6 lib/rapidash/base.rb
rapidash-0.0.5 lib/rapidash/base.rb