lib/rapidash/base.rb in rapidash-0.0.4 vs lib/rapidash/base.rb in rapidash-0.0.5

- old
+ new

@@ -1,12 +1,21 @@ 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 @@ -18,36 +27,44 @@ @url = "#{base_url}#{self.class.to_s.split("::")[-1].downcase}" @url += "/#{@id}" if @id end def create!(params) - self.options[:method] = :post - self.options[:body] = params.to_json + options[:method] = :post + set_body!(params) call! end def update!(params) - self.options[:method] = client.class.patch ? :patch : :put - self.options[:body] = params.to_json + options[:method] = client.class.patch ? :patch : :put + set_body!(params) call! end def delete! - self.options[:method] = :delete + options[:method] = :delete call! end def call! self.options ||= {} - self.options.delete(:previous_url) - self.options[:header] ||= {} - self.options[:header]["content-type"] = "application/json" - method = self.options.delete(:method) || :get - client.send(method, url, 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