Class: GistKV::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gist_id, auth = nil) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
# File 'lib/gistkv.rb', line 17

def initialize(gist_id, auth = nil)
  @gist_id = gist_id
  @auth = auth
  @read_only = @auth.nil?
  headers = GistKV::Client.headers(@auth)
  @conn = GistKV::Client.conn(headers)
end

Instance Attribute Details

#read_onlyObject (readonly)

Returns the value of attribute read_only.



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

def read_only
  @read_only
end

Class Method Details

.create_database(auth) ⇒ Object



25
26
27
28
29
30
# File 'lib/gistkv.rb', line 25

def self.create_database(auth)
  return if auth.nil?
  headers = GistKV::Client.headers(auth)
  res = GistKV::Client.conn(headers).post(nil, GIST_BASE)
  res.body["id"] if res.body
end

Instance Method Details

#get(key) ⇒ Object Also known as: []



36
37
38
# File 'lib/gistkv.rb', line 36

def get(key)
  get_gist[key.to_s]
end

#keysObject



32
33
34
# File 'lib/gistkv.rb', line 32

def keys
  get_gist.keys
end

#set(key, value) ⇒ Object Also known as: []=



42
43
44
45
46
47
48
# File 'lib/gistkv.rb', line 42

def set(key, value)
  return if @auth.nil?
  gist = get_gist
  gist[key.to_s] = value
  patch_gist(gist)
  value
end

#update(changes) ⇒ Object



52
53
54
55
56
57
# File 'lib/gistkv.rb', line 52

def update(changes)
  gist = get_gist
  changes.each {|key,val| gist[key.to_s] = val }
  patch_gist(gist)
  changes
end