Module: Chawk

Defined in:
lib/chawk.rb,
lib/node.rb,
lib/range.rb,
lib/models.rb,
lib/quantizer.rb,
lib/chawk/version.rb

Overview

Chawk is a gem for storing and retrieving time seris data.

Defined Under Namespace

Modules: Models, Quantizer

Constant Summary collapse

VERSION =

The current version of Chawk

"0.3.0"

Class Method Summary collapse

Class Method Details

.bulk_add_points(agent, data) ⇒ Object

Insert data for multiple addresses at once. Format should be a hash of valid data sets keyed by address example: href="1,2,3,4,5">key1’=>,’key2’=>

Parameters:

  • agent (Chawk::Agent)

    the agent whose permission will be used for this request

  • data (Hash)

    the bulk data to be inserted



89
90
91
92
93
94
95
# File 'lib/chawk.rb', line 89

def self.bulk_add_points(agent, data)
  data.keys.each do |key|
    dset = data[key]
    dnode = node(agent,key)
    dnode.add_points dset
  end
end

.check_node_public_security(node, access) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/chawk.rb', line 23

def self.check_node_public_security(node, access)
  case access
  when :read
    node.public_read == true
  when :write
    node.public_write == true
  end
end

.check_node_relations_security(rel, access) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/chawk.rb', line 8

def self.check_node_relations_security(rel, access)
  if (rel && (rel.read || rel.admin))
    case access
    when :read
      (rel.read or rel.admin)
    when :write
      (rel.write or rel.write)
    when :admin
      rel.admin
    when :full
      (rel.read && rel.write && rel.admin)
    end
  end
end

.check_node_security(agent, node, access = :full) ⇒ Object

Raises:

  • (SecurityError)


32
33
34
35
36
37
38
39
# File 'lib/chawk.rb', line 32

def self.check_node_security(agent,node,access=:full)

  rel = node.relations.where(agent_id:agent.id).first

  return node if check_node_relations_security(rel,access) || check_node_public_security(node,access)

  raise SecurityError,"You do not have permission to access this node. #{agent} #{rel} #{access}"
end

.clear_all_data!Object

Deletes all data in the database. Very dangerous. Backup often!



99
100
101
102
103
104
105
# File 'lib/chawk.rb', line 99

def self.clear_all_data!
  Chawk::Models::Agent.destroy_all
  Chawk::Models::Relation.destroy_all
  Chawk::Models::Node.destroy_all
  Chawk::Models::Point.destroy_all
  Chawk::Models::Value.destroy_all
end

.find_or_create_node(agent, key, access = :full) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chawk.rb', line 41

def self.find_or_create_node(agent,key,access=:full)
  #TODO also accept regex-tested string
  raise(ArgumentError,"Key must be a string.") unless key.is_a?(String)

  node = Chawk::Models::Node.where(key:key).first
  if node
    node = check_node_security(agent,node,access)
  else
    node = Chawk::Models::Node.create(key:key) if node.nil?
    node.set_permissions(agent,true,true,true)
  end
  node.access = access
  return node
end

.node(agent, key, access = :full) ⇒ Chawk::Node

The primary method for retrieving an Node. If a key does not exist, it will be created and the current agent will be set as an admin for it.

Parameters:

  • agent (Chawk::Agent)

    the agent whose permission will be used for this request

  • key (String)

    the string address this node can be found in the database.

Returns:

  • (Chawk::Node)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chawk.rb', line 61

def self.node(agent,key,access=:full)

  unless key =~ /^[\w\:\$\!\@\*\[\]\~\(\)]+$/
    raise ArgumentError, "Key can only contain [A-Za-z0-9_:$!@*[]~()] (#{key})"
  end

  unless agent.is_a?(Chawk::Models::Agent) 
    raise ArgumentError, 'Agent must be a Chawk::Models::Agent instance'
  end

  unless key.is_a?(String)
    raise ArgumentError, 'key must be a string.'
  end

  node = find_or_create_node(agent,key,access)

  unless node
    raise ArgumentError,"No node was returned."
  end

  node.agent = agent
  return node
end