class Rapidash::Base

Attributes

root_element[RW]
client[RW]
options[RW]
url[RW]

Public Class Methods

new(*args) click to toggle source
# File lib/rapidash/base.rb, line 16
def initialize(*args)
  @client, @id, options = args

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

  @options ||= {}
  @options.merge!(options || {})
  @url = "#{base_url}#{resource_url}"
  @url += "/#{@id}" if @id
end
root(name) click to toggle source
# File lib/rapidash/base.rb, line 11
def root(name)
  @root_element = name.to_sym
end

Public Instance Methods

call!() click to toggle source
# File lib/rapidash/base.rb, line 48
def call!
  self.options ||= {}
  options.delete(:previous_url)
  options[:headers] ||= {}
  options[:headers]["content-type"] = "application/json"
  method = options.delete(:method) || :get
  client.send(method, url, options)
end
create!(params) click to toggle source
# File lib/rapidash/base.rb, line 30
def create!(params)
  options[:method] = :post
  set_body!(params)
  call!
end
delete!() click to toggle source
# File lib/rapidash/base.rb, line 42
def delete!
  options[:method] = :delete
  call!
end
update!(params) click to toggle source
# File lib/rapidash/base.rb, line 36
def update!(params)
  options[:method] = client.class.patch ? :patch : :put
  set_body!(params)
  call!
end

Private Instance Methods

base_url() click to toggle source
# File lib/rapidash/base.rb, line 68
def base_url
  old_url = self.options[:previous_url]
  old_url ? "#{old_url}/" : ""
end
resource_url() click to toggle source
# File lib/rapidash/base.rb, line 73
def resource_url
  self.options[:url] || self.class.to_s.split("::")[-1].downcase.pluralize
end
set_body!(params) click to toggle source
# File lib/rapidash/base.rb, line 60
def set_body!(params)
  if self.class.root_element
    options[:body] = {self.class.root_element => params}
  else
    options[:body] = params
  end
end