class SafetyRazor::Slice::Base
Common behavior for Razor’s model slices.
@author Fletcher Nichol <fnichol@nichol.ca>
Attributes
client[R]
Public Class Methods
new(client)
click to toggle source
# File lib/safety_razor/slice/base.rb, line 15 def initialize(client) @client = client end
Public Instance Methods
all()
click to toggle source
# File lib/safety_razor/slice/base.rb, line 30 def all response = connection.get(slice_path) parse(response) end
create(params)
click to toggle source
# File lib/safety_razor/slice/base.rb, line 19 def create(params) payload = JSON.generate(params) response = connection.post(slice_path, 'json_hash' => payload) parse(response).first end
destroy(uuid)
click to toggle source
# File lib/safety_razor/slice/base.rb, line 42 def destroy(uuid) connection.delete(slice_path(uuid)) end
get(uuid)
click to toggle source
# File lib/safety_razor/slice/base.rb, line 25 def get(uuid) response = connection.get(slice_path(uuid)) parse(response).first end
update(params)
click to toggle source
# File lib/safety_razor/slice/base.rb, line 35 def update(params) payload = JSON.generate(params) uuid = params[:uuid] response = connection.put(slice_path(uuid), 'json_hash' => payload) parse(response).first end
Protected Instance Methods
connection()
click to toggle source
# File lib/safety_razor/slice/base.rb, line 50 def connection client.connection end
new_mash(obj)
click to toggle source
# File lib/safety_razor/slice/base.rb, line 67 def new_mash(obj) Hashie::Mash.new(obj) end
parse(response)
click to toggle source
# File lib/safety_razor/slice/base.rb, line 60 def parse(response) collection = JSON.parse(response.body)["response"] collection = [collection] if collection.is_a?(Hash) Array(collection).map { |obj| new_mash(strip_ivars(obj)) } end
slice_path(uuid = nil)
click to toggle source
# File lib/safety_razor/slice/base.rb, line 54 def slice_path(uuid = nil) path = "/razor/api/#{slice_name}" path += "/#{uuid}" if uuid path end
strip_ivars(obj)
click to toggle source
# File lib/safety_razor/slice/base.rb, line 71 def strip_ivars(obj) case obj when Hash stripped = Hash.new obj.each_pair do |key, value| new_key = key.is_a?(String) ? key.sub(/^@/, '') : key stripped[new_key] = strip_ivars(value) end stripped when Array obj.map { |value| strip_ivars(value) } else obj end end