Class: Ixtlan::Remote::Rest

Inherits:
Object
  • Object
show all
Includes:
ModelHelpers
Defined in:
lib/ixtlan/remote/rest.rb

Instance Method Summary collapse

Methods included from ModelHelpers

#to_model, #to_model_class, #to_model_singular_underscore, #to_model_underscore

Instance Method Details

#[]=(clazz, server) ⇒ Object



47
48
49
# File 'lib/ixtlan/remote/rest.rb', line 47

def []=( clazz, server )
  model_to_servers[ to_model_singular_underscore( clazz ) ] = server
end

#create(model, *args, &block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/ixtlan/remote/rest.rb', line 66

def create( model, *args, &block )
  if model.respond_to?( :attributes )
    args << model.attributes
    model = model.class
  end
  new_resource( model ).create( *args ).send_it( &block )
end

#delete(model, *args) ⇒ Object



103
104
105
# File 'lib/ixtlan/remote/rest.rb', line 103

def delete( model, *args )
  update_or_delete( :delete, model, *args )
end

#new_resource(model) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/ixtlan/remote/rest.rb', line 57

def new_resource( model )
  begin 
    m = to_model_singular_underscore( model ).camelize.constantize 
    to_server( model ).new_rest_resource( m )
  rescue
    raise "unknown model #{model}"
  end
end

#retrieve(model, *args) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/ixtlan/remote/rest.rb', line 74

def retrieve( model, *args )
  resource = new_resource( model )
  if args.last.is_a? Hash
    params = args.delete_at( args.size - 1 )
    resource.retrieve( *args ).query_params( params )
  else
    resource.retrieve( *args )
  end
  resource.send_it
end

#server(server_name, &block) ⇒ Object



41
42
43
44
45
# File 'lib/ixtlan/remote/rest.rb', line 41

def server( server_name, &block )
  server = servers[ server_name.to_sym ] ||= Server.new( self )
  block.call server if block
  server
end

#to_server(model) ⇒ Object



51
52
53
54
55
# File 'lib/ixtlan/remote/rest.rb', line 51

def to_server( model )
  s = model_to_servers[ to_model_singular_underscore( model ) ]
  raise "model #{model} unknown as rest service" unless s
  s
end

#update(model, *args) ⇒ Object



99
100
101
# File 'lib/ixtlan/remote/rest.rb', line 99

def update( model, *args )
  update_or_delete( :update, model, *args )
end