class Bixby::Model::Base

Public Class Methods

destroy(id) click to toggle source
# File lib/bixby-client/model/base.rb, line 25
def destroy(id)
  raise NotImplementedError
end
find(id) click to toggle source
# File lib/bixby-client/model/base.rb, line 17
def find(id)
  raise NotImplementedError
end
list(*args) click to toggle source

Public API methods

# File lib/bixby-client/model/base.rb, line 13
def list(*args)
  raise NotImplementedError
end
new(hash=nil) click to toggle source
# File lib/bixby-client/model/base.rb, line 113
      def initialize(hash=nil)
        return if hash.nil?

        hash.each do |k,v|
          instance_variable_set("@#{k}", v)
          next if self.respond_to?(k.to_sym)
          code = <<-EOF
          def #{k}()
            @#{k}
          end
          EOF
          eval(code)
        end
      end
update(id, data) click to toggle source
# File lib/bixby-client/model/base.rb, line 21
def update(id, data)
  raise NotImplementedError
end

Private Class Methods

api_uri(*args) click to toggle source

Create a fully qualified API URL

@param [String] path or a list of paths

@return [String] API URL

# File lib/bixby-client/model/base.rb, line 92
def api_uri(*args)
  if args.first !~ %r{^/rest/} then
    args.unshift File.join("/rest", args.shift) # prepend /rest
  end
  URI.join(Bixby.client.manager_uri, *args).to_s
end
create_req(url) click to toggle source

Create a REST request

@param [String] url

@return [HTTPI::Request]

# File lib/bixby-client/model/base.rb, line 104
def create_req(url)
  req = HTTPI::Request.new(api_uri(url))
  req.headers["Content-Type"] = "application/json"
  req.headers["Accept"] = "application/json" # temp workaround, manager should always return JSON on REST urls
  req
end
delete(url) click to toggle source

Delete the resource at the given URL

@param [String] url

@return [Boolean]

# File lib/bixby-client/model/base.rb, line 77
def delete(url)
  req = create_req(url)
  Bixby.client.sign_http_request(req)
  res = HTTPI.delete(req)
  if res.error? then
    raise "error" # TODO
  end
  true
end
get(url) click to toggle source

Get the object at the given URL

@param [String] url

@return [Object] single object or an array of objects

# File lib/bixby-client/model/base.rb, line 38
def get(url)
  req = create_req(url)
  Bixby.client.sign_http_request(req)
  res = HTTPI.get(req)
  if res.error? then
    raise "error" # TODO
  end

  data = MultiJson.load(res.body)
  if data.kind_of? Array then
    return data.map{ |d| self.new(d) }
  else
    return self.new(data)
  end
end
patch(url, data)
Alias for: put
put(url, data) click to toggle source

Put some data at the given URL (update an object)

@param [String] url @param [Hash] data to update

@return [Boolean]

# File lib/bixby-client/model/base.rb, line 60
def put(url, data)
  req = create_req(url)
  req.body = MultiJson.dump(data)
  Bixby.client.sign_http_request(req)
  res = HTTPI.put(req)
  if res.error? then
    raise "error" # TODO
  end
  true
end
Also aliased as: patch

Public Instance Methods

[](key) click to toggle source
# File lib/bixby-client/model/base.rb, line 128
def [](key)
  if self.respond_to?(key.to_sym) then
    return self.send(key.to_sym)
  end
  return nil
end