Class: Reflect::Keyspace

Inherits:
Object
  • Object
show all
Defined in:
lib/reflect/keyspace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attrs = {}) ⇒ Keyspace

Returns a new instance of Keyspace.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/reflect/keyspace.rb', line 17

def initialize(client, attrs={})
  @client = client

  # If we have fields, we'll need to populate them individually.
  fields = attrs.delete("fields")

  attrs['updated_at'] = Time.parse(attrs['updated_at']) if attrs['updated_at']
  attrs['created_at'] = Time.parse(attrs['created_at']) if attrs['created_at']

  attrs.each do |k, v|
    self.send("#{k}=".to_s, v)
  end

  if fields
    self.fields = fields.map { |f| Field.new(f) }
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/reflect/keyspace.rb', line 6

def client
  @client
end

#created_atObject

Returns the value of attribute created_at.



14
15
16
# File 'lib/reflect/keyspace.rb', line 14

def created_at
  @created_at
end

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/reflect/keyspace.rb', line 11

def description
  @description
end

#fieldsObject

Returns the value of attribute fields.



12
13
14
# File 'lib/reflect/keyspace.rb', line 12

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/reflect/keyspace.rb', line 8

def name
  @name
end

#slugObject

Returns the value of attribute slug.



9
10
11
# File 'lib/reflect/keyspace.rb', line 9

def slug
  @slug
end

#statistics_keyObject

Returns the value of attribute statistics_key.



10
11
12
# File 'lib/reflect/keyspace.rb', line 10

def statistics_key
  @statistics_key
end

#statusObject

Returns the value of attribute status.



13
14
15
# File 'lib/reflect/keyspace.rb', line 13

def status
  @status
end

#updated_atObject

Returns the value of attribute updated_at.



15
16
17
# File 'lib/reflect/keyspace.rb', line 15

def updated_at
  @updated_at
end

Instance Method Details

#append(key, records) ⇒ Object

Appends records to a tablet. If the tablet doesn’t exist it will be created. records can be either a single object or an array of objects. A single object represents a single row.

Parameters:

  • String

    key the key to create

  • Array|Hash

    records the records to create



58
59
60
61
62
63
64
# File 'lib/reflect/keyspace.rb', line 58

def append(key, records)
  resp = client.put(path(slug, key), records)

  if resp.response.code != "202"
    raise Reflect::RequestError, Reflect._format_error_message(resp)
  end
end

#delete(key) ⇒ Object

Deletes a key within a keyspace.

Parameters:

  • String

    key the key to delete



124
125
126
127
128
129
130
# File 'lib/reflect/keyspace.rb', line 124

def delete(key)
  resp = client.delete(path(slug, key))

  if resp.response.code != "202"
    raise Reflect::RequestError, Reflect._format_error_message(resp)
  end
end

#keys(continuation = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/reflect/keyspace.rb', line 35

def keys(continuation=nil)
  resp = client.get(keys_path(slug, continuation))

  if resp.response.code != "200"
    raise Reflect::RequestError, Reflect._format_error_message(resp)
  end

  json = JSON.parse(resp.body)

  if json["keys"].nil? || json["keys"].empty?
    nil
  else
    KeyList.new(json["keys"], json["next"])
  end
end

#patch(key, records, criteria) ⇒ Object

Patches the existing records in a tablet with a net set of records. The criteria parameter indicates which records to match existing records on. In the Reflect API, if no existing records match the supplied records then those records are dropped.

Parameters:

  • String

    key the key to create

  • Array|Hash

    records the records to create

  • Array

    criteria an array of field names within a record to match



90
91
92
93
94
95
96
# File 'lib/reflect/keyspace.rb', line 90

def patch(key, records, criteria)
  resp = client.patch(path(slug, key), records, "X-Criteria" => criteria.join(", "))

  if resp.response.code != "202"
    raise Reflect::RequestError, Reflect._format_error_message(resp)
  end
end

#replace(key, records) ⇒ Object

Replaces the existing records in a tablet with a net set of records. records can be either a single object or an array of objects. A single object represents a single row.

Parameters:

  • String

    key the key to create

  • Array|Hash

    records the records to create



73
74
75
76
77
78
79
# File 'lib/reflect/keyspace.rb', line 73

def replace(key, records)
  resp = client.post(path(slug, key), records)

  if resp.response.code != "202"
    raise Reflect::RequestError, Reflect._format_error_message(resp)
  end
end

#upsert(key, records, criteria) ⇒ Object

Patch the existing records in a tablet with a new set of records and insert any that aren’t matched. The criteria parameter indicates which records to match existing records on.

Parameters:

  • String

    key the key to create

  • Array|Hash

    records the records to create

  • Array

    criteria an array of field names within a record to match



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/reflect/keyspace.rb', line 106

def upsert(key, records, criteria)
  headers = {
    "X-Criteria" => criteria.join(", "),
    "X-Insert-Missing" => true
  }

  resp = client.patch(path(slug, key), records, headers)

  if resp.response.code != "202"
    raise Reflect::RequestError, Reflect._format_error_message(resp)
  end
end