module Fakecrm::Fetch

Public Instance Methods

build_continuation() click to toggle source
# File lib/fakecrm/fetch.rb, line 11
def build_continuation
  Base64.urlsafe_encode64(params.merge({:offset => offset+limit, :limit => limit, :include_deleted => include_deleted?.to_s}).to_json)
end
consider_deleted(resource) click to toggle source
# File lib/fakecrm/fetch.rb, line 28
def consider_deleted(resource)
  if include_deleted?
    resource.with_deleted
  else
    resource
  end
end
continuation() click to toggle source
# File lib/fakecrm/fetch.rb, line 5
def continuation
  JSON.parse(Base64.decode64(params.fetch("continuation_handle", "e30=")))
rescue => exception
  {}
end
fetch_many(resource) click to toggle source
# File lib/fakecrm/fetch.rb, line 111
def fetch_many(resource)
  query = consider_deleted(resource)
  remaining_count = query.count - offset
  result = {:results => ResourceView.decorate(query.all(:limit => limit, :offset => offset)) }

  if remaining_count > limit
    result["continuation_handle"] = build_continuation
  end

  status 200
  body result.to_json
end
fetch_one(resource, primary_key) click to toggle source
# File lib/fakecrm/fetch.rb, line 124
def fetch_one(resource, primary_key)
  begin
    status 200
    if resource.respond_to? :with_deleted
      body ResourceView.decorate(resource.with_deleted.get!(primary_key)).to_json
    else
      body ResourceView.decorate(resource.get!(primary_key)).to_json
    end
  rescue ::DataMapper::ObjectNotFoundError => e
    status 404
  end
end
include_deleted?() click to toggle source
# File lib/fakecrm/fetch.rb, line 24
def include_deleted?
  "true" == continuation.fetch("include_deleted", params.fetch("include_deleted", "false")).to_s
end
limit() click to toggle source
# File lib/fakecrm/fetch.rb, line 15
def limit
  limit = params.fetch("limit", continuation.fetch("limit", "10")).to_i
  (1..1000).include?(limit) ? limit : 10
end
offset() click to toggle source
# File lib/fakecrm/fetch.rb, line 20
def offset
  continuation.fetch("offset", 0).to_i
end