class Rsbe::Client::Base

Public Class Methods

all() click to toggle source

returns an array of Partner objects

# File lib/rsbe/client/base.rb, line 13
def self.all
  conn = Rsbe::Client::Connection.new
  local_response = conn.get base_path
  raise "Error retrieving partners" unless local_response.status == 200
  JSON.parse(local_response.body).collect { |json_hash| new(json_hash) }
end
base_path() click to toggle source
# File lib/rsbe/client/base.rb, line 34
def self.base_path
  '/api/v0'
end
find(id) click to toggle source
# File lib/rsbe/client/base.rb, line 6
def self.find(id)
  o = find_and_instantiate(id)
  raise Rsbe::Client::RecordNotFound.new("#{self.name} with #{id} not found") if o.nil?
  o
end
new() click to toggle source
# File lib/rsbe/client/base.rb, line 31
def initialize
  @conn = Rsbe::Client::Connection.new
end

Private Class Methods

all_attrs() click to toggle source
# File lib/rsbe/client/base.rb, line 135
def self.all_attrs
  self.rw_attrs + self.ro_attrs
end
find_and_instantiate(id) click to toggle source
# File lib/rsbe/client/base.rb, line 20
def self.find_and_instantiate(id)
  p = self.new(id: id)
  if p.send(:get)
    p.send(:update_hash_from_response)
  else
    p = nil
  end
  p
end
ro_attrs() click to toggle source
# File lib/rsbe/client/base.rb, line 131
def self.ro_attrs
  []
end
rw_attrs() click to toggle source
# File lib/rsbe/client/base.rb, line 127
def self.rw_attrs
  []
end

Public Instance Methods

save() click to toggle source
# File lib/rsbe/client/base.rb, line 37
def save
  (has_id? && exists?) ? update : create
end

Private Instance Methods

coll_path() click to toggle source
# File lib/rsbe/client/base.rb, line 119
def coll_path
  self.class.base_path
end
create() click to toggle source
# File lib/rsbe/client/base.rb, line 54
def create
  @response = @conn.post do |req|
    req.url coll_path
    req.headers['Content-Type'] = 'application/json'
    req.body = @hash.to_json
  end
  return false unless @response.status == 201

  update_hash_from_response
  true
end
exists?() click to toggle source
# File lib/rsbe/client/base.rb, line 115
def exists?
  get
end
get(id = @hash[:id]) click to toggle source

N.B. intentionally not updating hash here

# File lib/rsbe/client/base.rb, line 43
def get(id = @hash[:id])
  @response = @conn.get item_path(id)
  @response.status == 200
end
get_children(path_segment) click to toggle source
# File lib/rsbe/client/base.rb, line 48
def get_children(path_segment)
  path = item_path(id) + '/' + path_segment
  @response = @conn.get path
  @response.status == 200
end
has_id?() click to toggle source
# File lib/rsbe/client/base.rb, line 111
def has_id?
  !@hash[:id].nil?
end
item_path(id) click to toggle source
# File lib/rsbe/client/base.rb, line 123
def item_path(id)
  coll_path + "/#{id}"
end
update() click to toggle source
# File lib/rsbe/client/base.rb, line 96
def update
  @response = @conn.put do |req|
    req.url item_path(@hash[:id])
    req.headers['Content-Type'] = 'application/json'
    req.body = @hash.to_json
  end

  success = @response.status == 204
  if success
    get
    update_hash_from_response
  end
  success
end
update_hash(arg) click to toggle source
# File lib/rsbe/client/base.rb, line 84
def update_hash(arg)
  # update object state
  self.class.all_attrs.each { |k| (@hash[k] = arg[k.to_s]) if arg.has_key?(k.to_s) }
end
update_hash_from_response() click to toggle source
# File lib/rsbe/client/base.rb, line 66
def update_hash_from_response
  raise "@response not initialized" if @response.nil?

  # update attributes with those from server
  response_hash = JSON.parse(@response.body)
  raise "unable to parse response to hash" unless response_hash.is_a?(Hash)
  update_hash(response_hash)
end
update_hash_nils(arg) click to toggle source
# File lib/rsbe/client/base.rb, line 89
def update_hash_nils(arg)
  # update object state
  self.class.all_attrs.each do |k|
    (@hash[k] = arg[k.to_s]) if @hash[k].nil?
  end
end
update_hash_nils_from_response() click to toggle source
# File lib/rsbe/client/base.rb, line 75
def update_hash_nils_from_response
  raise "@response not initialized" if @response.nil?

  # update attributes with those from server
  response_hash = JSON.parse(@response.body)
  raise "unable to parse response to hash" unless response_hash.is_a?(Hash)
  update_hash_nils(response_hash)
end