Class: Caseblocks::Case

Inherits:
Object
  • Object
show all
Defined in:
lib/caseblocks/case.rb

Overview

Class for handling Case objects

Relationships are accessed using dynamic method names based on the related case type code, for example if you have an order case type relating to customer then kase.customers will return an array of related customer cases

Currently there is a very simplistic caching mechanism, so may cause issues if you need to refresh data

Author:

  • Stewart McKee

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(case_data, client) ⇒ Caseblocks::Case

Initializes a new case object

Parameters:

  • case_data (Hash)

    hash of data from the case

  • client (Caseblocks::Client)

    client to be used

Since:

  • 0.1.0



62
63
64
65
# File 'lib/caseblocks/case.rb', line 62

def initialize(case_data, client)
  @client = client
  @case_data = case_data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Since:

  • 0.1.0



119
120
121
122
123
124
125
126
# File 'lib/caseblocks/case.rb', line 119

def method_missing(m, *args, &block)
  matched_relationship = case_type.relationships.detect{|r| r.case_type.code == m.to_s.singularize}
  if matched_relationship
    return @client.request("/case_blocks/#{matched_relationship.case_type.code.pluralize}?related_cases%5Brelation_id%5D=#{matched_relationship.id}&related_cases%5Brelationship_type%5D=CaseBlocks%3A%3ACaseTypeDirectRelationship&related_cases%5Bcase_from_id%5D=#{id}&related_cases%5Bpage_size%5D=1000")[matched_relationship.case_type.code.pluralize].map{|kase| Caseblocks::Case.new(kase, @client) }
  else
    super
  end
end

Class Method Details

.create(data, case_type, client = Caseblocks::Client.default) ⇒ Caseblocks::Case

Creates a case within the supplied case type with the data requested. Will fail if validation fails with a Caseblocks::ValidationError error

Examples:

Create a simple case

client = Caseblocks::Client.new(:authentication_token => "qsodjgoasgdsjgsd")
case_type = Caseblocks::CaseType.find(204, client)
kase = Caseblocks::Case.create({:title => "I'm a teapot", :case_type => case_type}, client)
puts kase.id
puts kase.attributes["title"]

Parameters:

  • data (Hash)

    The case data to create

  • data (Caseblocks::Client)

    the client to connect to caseblocks with

Returns:

  • (Caseblocks::Case)

    The newly created case object with the data supplied from the server. This will include default values and fields created by the server.

Raises:

  • (Caseblocks::ValidationError)

    This occurs when a validation error occurrs with regards to the data supplied. This could be explicit validation failures or could be a conflict of document version numbers

  • (Caseblocks::Error)

    General error

Since:

  • 0.1.0



104
105
106
107
108
# File 'lib/caseblocks/case.rb', line 104

def self.create(data, case_type, client=Caseblocks::Client.default)
  data["case_type_id"] = case_type.id
  res = client.post("/case_blocks/#{case_type.code.pluralize}.json", {case_type.code => data})
  Case.new(res[case_type.code], client)
end

.find(id, client = Caseblocks::Client.default) ⇒ Caseblocks::Case

Finds an individual case from an id

Parameters:

  • id (String)

    the bson id of the case being loaded

  • client (Caseblocks::Client) (defaults to: Caseblocks::Client.default)

    client to be used

Returns:

  • (Caseblocks::Case)

    the case being searched for or nil if the case is not found

Since:

  • 0.1.0



19
20
21
22
# File 'lib/caseblocks/case.rb', line 19

def self.find(id, client=Caseblocks::Client.default)
  res = client.request("/case_blocks/cases/#{id}.json")
  return Case.new(res["case"], client)
end

.find_all_by(criteria, client = Caseblocks::Client.default, options = {}) ⇒ Array<Caseblocks::Case>

Finds all cases matching the supplied search criteria matched to the cases data

Parameters:

  • criteria (Hash)

    a hash where the keys are the field names and values are the search param for that field. Note wildcards are not supported

  • client (Caseblocks::Client) (defaults to: Caseblocks::Client.default)

    client to be used

Returns:

  • (Array<Caseblocks::Case>)

    returns an array of Caseblocks::Case objects that match the requested criteria

Since:

  • 0.1.0



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/caseblocks/case.rb', line 36

def self.find_all_by(criteria, client=Caseblocks::Client.default, options={})
  options[:page_size] = 10 unless options.has_key?(:page_size)
  options[:page] = 0 unless options.has_key?(:page)

  current_page = 0
  page_size = options[:page_size]
  total = -1
  results = []
  while (total == -1 || ((current_page * page_size) + page_size) < total) do
    options[:query_type] = "AND" unless options.has_key?(:query_type)
    query = {:query => criteria.map{|k,v| "#{k}:\"#{Caseblocks::Helpers::Elastic.encode_query(v).gsub("&", "%26")}\""}.join(" #{options[:query_type]} ")}
    query.merge!(options)
    res = client.request("/case_blocks/search.json", query)
    total = res.inject(0){|total, ct| total += ct["total"].to_i }
    cases = res.map{|ct| ct["cases"] }.flatten
    results += cases.map{|kase| Case.new(kase, client) }
    current_page += 1
    options[:page] = current_page
  end
  results
end

.find_by(criteria, client = Caseblocks::Client.default) ⇒ Caseblocks::Case

Finds an individual case based on search criteria matched to the cases data

Parameters:

  • criteria (Hash)

    a hash where the keys are the field names and values are the search param for that field. Note wildcards are not supported

  • client (Caseblocks::Client) (defaults to: Caseblocks::Client.default)

    client to be used

Returns:

  • (Caseblocks::Case)

    the case being searched for or nil if the case is not found

Since:

  • 0.1.0



28
29
30
# File 'lib/caseblocks/case.rb', line 28

def self.find_by(criteria, client=Caseblocks::Client.default)
  Caseblocks::Case.find_all_by(criteria, client).first
end

Instance Method Details

#attributesHash

The case data for the current case. This can be treated as the hash of data within the case as the save operation will use this data to update the case

Returns:

  • (Hash)

    Case data object

Since:

  • 0.1.0



87
88
89
# File 'lib/caseblocks/case.rb', line 87

def attributes
  @case_data
end

#case_typeCaseblocks::CaseType

Case type for the current case

Returns:

Since:

  • 0.1.0



75
76
77
# File 'lib/caseblocks/case.rb', line 75

def case_type
  @case_type ||= Caseblocks::CaseType.find(case_type_id, @client)
end

#case_type_idInteger

Case type id for the current case

Returns:

  • (Integer)

    The id for the current case, taking into account work/people/organization types

Since:

  • 0.1.0



81
82
83
# File 'lib/caseblocks/case.rb', line 81

def case_type_id
  @case_data["case_type_id"] || @case_data["work_type_id"] || @case_data["people_type_id"] || @case_data["organization_type_id"]
end

#idString

Returns the bson id for the case

Returns:

  • (String)

    BSON id for the current case

Since:

  • 0.1.0



69
70
71
# File 'lib/caseblocks/case.rb', line 69

def id
  @case_data["_id"]
end

#saveBoolean

Saves the current data against the case and updates attributes with latest from server

Returns:

  • (Boolean)

    Returns true if the result is a success

Since:

  • 0.1.0



112
113
114
115
116
# File 'lib/caseblocks/case.rb', line 112

def save
  res = @client.put("/case_blocks/cases/#{id}.json", @case_data)
  @case_data.merge(res["case"])
  true
end

#tasklistsObject

TODO:

Implement me

Since:

  • 0.1.0



129
130
131
# File 'lib/caseblocks/case.rb', line 129

def tasklists
  []
end

#tasksArray<Caseblocks::Task>

Returns an array of Caseblocks::Task that are on this case. This is a full list of all tasks, not split by tasklist.

Returns:

Since:

  • 0.1.0



135
136
137
# File 'lib/caseblocks/case.rb', line 135

def tasks
  @tasks ||= Caseblocks::Task.find_all(@case_data["tasks"], @client)
end