Class: Ixtlan::Remote::Resource

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

Instance Method Summary collapse

Methods included from ModelHelpers

#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

#modelObject



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
  headers = {:content_type => :json, :accept => :json}
  headers[:params] = @params if @params
  result =
    if @method != :get
      @resource.send( @method, @payload ? @payload.to_json : '', headers, &block )
    else
      @resource.send( @method, headers, &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