module Bodhi::Resource::ClassMethods

Public Instance Methods

aggregate(context, pipeline) click to toggle source

Performs MongoDB aggregations using the given pipeline

Equivalent CURL command:

curl -u {username}:{password} https://{server}/{namespace}/resources/{resource}/aggregate?pipeline={pipeline}

@note Large aggregations can be very time and resource intensive! @param context [Bodhi::Context] @param pipeline [String] @return [Hash] the JSON response converted to a Ruby Hash @raise [ArgumentError] if the given pipeline is NOT a String @raise [Bodhi::ContextErrors] if the given context is invalid @raise [Bodhi::ApiErrors] if any response status is NOT 200 @example

Resource.aggregate(context, "[{ $match: { age: { $gte: 21 }}}]")
# File lib/bodhi-slam/resource.rb, line 238
def aggregate(context, pipeline)
  if context.invalid?
    raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
  end

  unless pipeline.is_a? String
    raise ArgumentError.new("Expected 'pipeline' to be a String. 'pipeline' #=> #{pipeline.class}")
  end

  result = context.connection.get do |request|
    request.url "/#{context.namespace}/resources/#{name}/aggregate?pipeline=#{pipeline}"
    request.headers[context.credentials_header] = context.credentials
  end

  if result.status != 200
    raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
  end

  result.body
end
all(context=nil)
Alias for: find_all
build_type() click to toggle source

Generates a new {Bodhi::Type} instance using the classes metadata

@return [Bodhi::Type] @example

class User
  include Bodhi::Resource

  field :first_name, type: "String", required: true, is_not_blank: true
  field :last_name, type: "String", required: true, is_not_blank: true
  field :email, type: "String", required: true, is_not_blank: true, is_email: true

  index ["last_name"]
end

User.build_type #=> #<Bodhi::Type:0x007fbff403e808 @name="User" @properties={...} @indexes=[...]>
# File lib/bodhi-slam/resource.rb, line 62
def build_type
  Bodhi::Type.new(name: self.name, properties: self.properties, indexes: self.indexes, embedded: self.is_embedded?)
end
count(context, query={}) click to toggle source

Counts all records that match the given query

Equivalent CURL command:

curl -u {username}:{password} https://{server}/{namespace}/resources/{resource}/count?where={query}

@param context [Bodhi::Context] @param query [Hash] MongoDB query operations @example

Resource.count(context) #=> # count all records
Resource.count(context, name: "Foo") #=> # count all records with name == "Foo"
# File lib/bodhi-slam/resource.rb, line 89
def count(context, query={})
  query_obj = Bodhi::Query.new(name)
  query_obj.where(query).from(context)
  query_obj.count
end
create!(context, properties) click to toggle source

Creates a new resource with the given properties and POSTs it to the IoT Platform

Equivalent CURL command:

curl -u {username}:{password} -X POST -H "Content-Type: application/json" \
https://{server}/{namespace}/resources/{resource} \
-d '{properties}'

@param context [Bodhi::Context] @param properties [Hash] @return [Bodhi::Resource] @raise [Bodhi::ContextErrors] if the given Bodhi::Context is invalid @raise [Bodhi::ApiErrors] if the record cannot be saved

Resource.create!(context, name: "Foo", age: 125, tags: ["green", "blue"])
# File lib/bodhi-slam/resource.rb, line 129
def create!(context, properties)
  if context.invalid?
    raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
  end

  # Build a new record and set the context
  record = self.new(properties)
  record.bodhi_context = context

  # POST to the IoT Platform
  record.save!
  return record
end
delete!(context, query={}) click to toggle source

Deletes all records that match the given query

Equivalent CURL command:

curl -u {username}:{password} -X DELETE https://{server}/{namespace}/resources/{resource}?where={query}

@note Beware: It's easy to delete an entire collection with this method! Use it wisely :) @param context [Bodhi::Context] @param query [Hash] MongoDB query operations @return [Hash] with key: count @todo add more query complex examples @example

# delete with a query
Resource.delete!(context, sys_created_at: { '$lte': 6.months.ago.iso8601 })

# delete all records
Resource.delete!(context)
# File lib/bodhi-slam/resource.rb, line 110
def delete!(context, query={})
  query_obj = Bodhi::Query.new(name)
  query_obj.where(query).from(context)
  query_obj.delete
end
embedded(status) click to toggle source

Sets the resources embedded status to either true/false.

@param status [Boolean] @return [Boolean]

# File lib/bodhi-slam/resource.rb, line 17
def embedded(status); @embedded = status; end
field(name, options) click to toggle source

Defines the given name and options as a form attribute for the class. The name is set as a property, and validations & factory generators are added based on the supplied options

@param name [String] @param options [Hash] @example

class User
  include Bodhi::Resource

  field :first_name, type: "String", required: true, is_not_blank: true
  field :last_name, type: "String", required: true, is_not_blank: true
  field :email, type: "String", required: true, is_not_blank: true, is_email: true
end

object = User.new(first_name: "John", last_name: "Smith", email: "jsmith@email.com")
object.to_json #=> { "first_name": "John", "last_name": "Smith", "email": "jsmith@email.com" }
# File lib/bodhi-slam/resource.rb, line 41
def field(name, options)
  property(name.to_sym, options)
  validates(name.to_sym, options)
  generates(name.to_sym, options)
end
find(id, context=nil) click to toggle source

Search for a record with the given id

Equivalent CURL command:

curl -u {username}:{password} https://{server}/{namespace}/resources/{resource}/{id}

@param id [String] the {Bodhi::Properties#sys_id} of the record @param context [Bodhi::Context] @return [Bodhi::Resource] @raise [Bodhi::ContextErrors] if the given Bodhi::Context is invalid @raise [Bodhi::ApiErrors] if response status is NOT 200 @raise [ArgumentError] if the given id is NOT a String @example

context = Bodhi::Context.new
id = Resource.factory.create(context).sys_id
obj = Resource.find(context, id)
# File lib/bodhi-slam/resource.rb, line 157
def find(id, context=nil)
  if context.nil?
    context = Bodhi::Context.global_context
  end

  if context.invalid?
    raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
  end

  unless id.is_a? String
    raise ArgumentError.new("Expected 'id' to be a String. 'id' #=> #{id.class}")
  end

  result = context.connection.get do |request|
    request.url "/#{context.namespace}/resources/#{name}/#{id}"
    request.headers[context.credentials_header] = context.credentials
  end

  if result.status != 200
    raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
  end

  record = Object.const_get(name).new(result.body)
  record.bodhi_context = context
  record
end
find_all(context=nil) click to toggle source

Returns all records in the given context

All records returned by this method will have their {#bodhi_context} attribute set to context @note This method will return ALL records!! Don't use on large collections! YE BE WARNED!! @param context [Bodhi::Context] @return [Array<Bodhi::Resource>] @raise [Bodhi::ContextErrors] if the given context is invalid @raise [Bodhi::ApiErrors] if any response status is NOT 200 @example

Resource.find_all(context) # => [#<Resource:0x007fbff403e808>, #<Resource:0x007fbff403e808>, ...]
# File lib/bodhi-slam/resource.rb, line 195
def find_all(context=nil)
  if context.nil?
    context = Bodhi::Context.global_context
  end

  if context.invalid?
    raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
  end

  page = 1
  records = []

  begin
    result = context.connection.get do |request|
      request.url "/#{context.namespace}/resources/#{name}?paging=page:#{page}"
      request.headers[context.credentials_header] = context.credentials
    end

    if result.status != 200
      raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
    end

    page += 1
    records << result.body
  end until result.body.size != 100

  records.flatten.collect{ |record| Object.const_get(name).new(record.merge(bodhi_context: context)) }
end
Also aliased as: all
is_embedded?() click to toggle source

Checks if the resource is embedded

@return [Boolean]

# File lib/bodhi-slam/resource.rb, line 22
def is_embedded?; @embedded; end
save_batch(context, objects) click to toggle source

Saves a batch of resources to the Bodhi Cloud in the given context Returns an array of JSON objects describing the results for each record in the batch

@deprecated This uses the old bulk upload process and will be removed in version 1.0.0 DO NOT USE! @example

context = Bodhi::Context.new
list = Resource.factory.build_list(10)
Resource.save_batch(context, list)
# File lib/bodhi-slam/resource.rb, line 74
def save_batch(context, objects)
  batch = Bodhi::ResourceBatch.new(name, objects)
  batch.save!(context)
  batch
end
where(query) click to toggle source

Returns a {Bodhi::Query} object with the given query

@param query [Hash] the MongoDB query operations @return [Bodhi::Query<Bodhi::Resource>] a {Bodhi::Query} object, bound to the {Bodhi::Resource} with the given query @example

context = Bodhi::Context.new
Resource.where({conditions}).from(context).all
Resource.where({conditions}).and({more conditions}).limit(10).from(context).all
# File lib/bodhi-slam/resource.rb, line 267
def where(query)
  query_obj = Bodhi::Query.new(name)
  query_obj.where(query)
  query_obj
end