Class: Rhoconnect::Model::DynamicAdapterModel

Inherits:
Base
  • Object
show all
Defined in:
lib/rhoconnect/model/dynamic_adapter_model.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#result, #session, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#after_query, #ask, #before_query, create, #current_user, #do_query, enable, #expire_bulk_data, #get_data, load, load_source_model, #login, #logoff, partition_name, #push_deletes, #push_objects, #run_validators, #search, #stash_result, #sync, #validate

Methods included from Helpers

extended, #validators

Methods included from Handler::Helpers::Binding

#bind_handler

Constructor Details

#initialize(source, partition = nil, uri = nil) ⇒ DynamicAdapterModel

Returns a new instance of DynamicAdapterModel.

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 10

def initialize(source, partition=nil, uri=nil)
  @source = source
  @uri = uri || Rhoconnect.appserver
  @partition = partition || @source.user_id
  
  if @uri
    @uri = URI.parse(@uri)
    user = @uri.user
    @uri.user = nil
    @uri = @uri.to_s 
  end
  
  @token = Rhoconnect.api_token || user   
  raise Exception.new("Please provide a :token or set it in uri") unless @token
  super(source)
end

Instance Attribute Details

#partitionObject

Returns the value of attribute partition.



8
9
10
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 8

def partition
  @partition
end

#uriObject

Returns the value of attribute uri.



8
9
10
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 8

def uri
  @uri
end

Class Method Details

.authenticate(login, password) ⇒ Object



27
28
29
30
31
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 27

def self.authenticate(,password)
  hsh = {:login => , :password => password, :api_token => Rhoconnect.api_token}.to_json
  headers = {:content_type => :json, :accept => :json}
  RestClient.post "#{Rhoconnect.appserver}/rhoconnect/authenticate", hsh, headers
end

Instance Method Details

#api_headersObject

:nodoc:



82
83
84
85
86
87
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 82

def api_headers   # :nodoc:
  {
    :content_type => :json, 
    :accept => :json
  }
end

#create(create_hash) ⇒ Object



38
39
40
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 38

def create(create_hash)
  send_objects('create',@source.name, @partition, create_hash)
end

#delete(delete_hash) ⇒ Object



46
47
48
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 46

def delete(delete_hash)
  send_objects('delete',@source.name, @partition, delete_hash)
end

#process(method, path, payload = nil) ⇒ Object

:nodoc:



74
75
76
77
78
79
80
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 74

def process(method, path, payload = nil) # :nodoc:
  headers = api_headers
  payload  = payload.merge!(:api_token => @token).to_json
  args     = [method, payload, headers].compact
  response = resource(path).send(*args)
  response
end

#query(params = nil) ⇒ Object



33
34
35
36
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 33

def query(params=nil)
  @result={}
  @result = JSON.parse(send_objects('query',@source.name, @partition, params))
end

#resource(path) ⇒ Object

:nodoc:



70
71
72
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 70

def resource(path) # :nodoc:
  RestClient::Resource.new(@uri)[path]
end

#send_objects(action, source_name, partition, obj = {}) ⇒ Object

:nodoc:



59
60
61
62
63
64
65
66
67
68
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 59

def send_objects(action, source_name, partition, obj = {}) # :nodoc:
  validate_args(action, source_name, partition, obj)
  process(:post, "/rhoconnect/#{action}", 
    {
      :resource => source_name,
      :partition => partition,
      :attributes => obj 
    }
  )
end

#update(update_hash) ⇒ Object



42
43
44
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 42

def update(update_hash)
  send_objects('update',@source.name, @partition, update_hash)
end

#validate_args(action, source_name, partition, obj = {}) ⇒ Object

protected

Raises:



52
53
54
55
56
57
# File 'lib/rhoconnect/model/dynamic_adapter_model.rb', line 52

def validate_args(action, source_name, partition, obj = {}) # :nodoc:
  raise Exception.new("Please set uri in your settings or through console") unless @uri
  raise ArgumentError.new("Missing object id for #{obj.inspect}") if ['update','delete'].include? action and not obj.has_key?('id')
  raise ArgumentError.new("Missing source_name.") if source_name.empty?
  #raise ArgumentError.new("Missing partition for #{model}.") unless partition or partition.blank?
end