Class: Rhosync::EndpointHelpers

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

Class Method Summary (collapse)

Class Method Details

+ (Object) authenticate(content_type, body)



5
6
7
8
9
10
11
# File 'lib/rhosync/endpoints.rb', line 5

def self.authenticate(content_type, body)
  code, params = 200, parse_params(content_type, body)
  if Rhosync.configuration.authenticate
    code = 401 unless Rhosync.configuration.authenticate.call(params)
  end  
  [code, {'Content-Type' => 'text/plain'}, [""]]
end

+ (Object) create(content_type, body)



46
47
48
# File 'lib/rhosync/endpoints.rb', line 46

def self.create(content_type, body)
  self.on_cud(:create, content_type, body)
end

+ (Object) delete(content_type, body)



54
55
56
# File 'lib/rhosync/endpoints.rb', line 54

def self.delete(content_type, body)
  self.on_cud(:delete, content_type, body)  
end

+ (Object) on_cud(action, content_type, body)



35
36
37
38
39
40
41
42
43
44
# File 'lib/rhosync/endpoints.rb', line 35

def self.on_cud(action, content_type, body)
  params = parse_params(content_type, body)
  object_id = ""
  code, error = get_resource(params['resource'], action) do |klass|
    object_id = klass.send("rhosync_receive_#{action}".to_sym,
      params['partition'], params['attributes'])
    object_id = object_id.to_s if object_id
  end
  [code, {'Content-Type' => "text/plain"}, [error || object_id]]
end

+ (Object) query(content_type, body)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rhosync/endpoints.rb', line 13

def self.query(content_type, body)
  params = parse_params(content_type, body)
  action, c_type, result, records = :rhosync_query, 'application/json', {}, []
  # Call resource rhosync_query class method
  code, error = get_resource(params['resource'], action) do |klass|
    records = klass.send(action, params['partition'])
  end
  if code == 200
    # Serialize records into hash of hashes
    records.each do |record|
      result[record.id.to_s] = record.normalized_attributes
    end
    result = result.to_json
  else
    result = error
    c_type = 'text/plain'
    # Log warning if something broke
    warn error
  end    
  [code, {'Content-Type' => c_type}, [result]]
end

+ (Object) update(content_type, body)



50
51
52
# File 'lib/rhosync/endpoints.rb', line 50

def self.update(content_type, body)
  self.on_cud(:update, content_type, body)  
end