Class: Ixtlan::Remote::Resource
- Inherits:
-
Object
- Object
- Ixtlan::Remote::Resource
show all
- Includes:
- ModelHelpers
- Defined in:
- lib/ixtlan/remote/resource.rb
Instance Method Summary
collapse
#to_model, #to_model_class, #to_model_singular_underscore, #to_model_underscore
Constructor Details
#initialize(resource, model, new_method = nil) ⇒ Resource
Returns a new instance of Resource.
28
29
30
31
32
|
# File 'lib/ixtlan/remote/resource.rb', line 28
def initialize(resource, model, new_method = nil)
@base = resource
@model = model
@new_method = new_method || @model.method(:new)
end
|
Instance Method Details
#create(*obj_or_hash) ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/ixtlan/remote/resource.rb', line 89
def create(*obj_or_hash)
@method = :post
@payload = last(obj_or_hash)
@params = nil
@resource = @base[path(*obj_or_hash)]
self
end
|
#delete(*obj_or_hash) ⇒ Object
105
106
107
108
109
110
111
|
# File 'lib/ixtlan/remote/resource.rb', line 105
def delete(*obj_or_hash)
@method = :delete
@payload = last(obj_or_hash)
@params = nil
@resource = @base[path(*obj_or_hash)]
self
end
|
#last(args) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/ixtlan/remote/resource.rb', line 72
def last(args)
if args.last.is_a?(Hash)
args.reverse!
l = args.shift
args.reverse!
l
end
end
|
#model ⇒ Object
113
114
115
|
# File 'lib/ixtlan/remote/resource.rb', line 113
def model
@model
end
|
#query_params(params) ⇒ Object
67
68
69
70
|
# File 'lib/ixtlan/remote/resource.rb', line 67
def query_params(params)
@params = params
self
end
|
#retrieve(*args) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/ixtlan/remote/resource.rb', line 81
def retrieve(*args)
@method = :get
@params = nil
@resource = @base[path(*args)]
@payload = nil
self
end
|
#send_it(&block) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/ixtlan/remote/resource.rb', line 50
def send_it(&block)
raise "no method given - call any CRUD method first" unless @method
= {:content_type => :json, :accept => :json}
[:params] = @params if @params
result =
if @method != :get
@resource.send( @method, @payload ? @payload.to_json : '', , &block )
else
@resource.send( @method, , &block )
end
if result && result.size > 0
create_result_objects( JSON.load( result ) )
else
nil
end
end
|
#update(*obj_or_hash) ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/ixtlan/remote/resource.rb', line 97
def update(*obj_or_hash)
@method = :put
@payload = last(obj_or_hash)
@params = nil
@resource = @base[path(*obj_or_hash)]
self
end
|