Module: Voorhees::Resource::ClassMethods

Defined in:
lib/voorhees/resource.rb

Instance Method Summary collapse

Instance Method Details

#json_request(options = {}) {|request| ... } ⇒ Object

Yields:

  • (request)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/voorhees/resource.rb', line 38

def json_request(options={})
  request = Voorhees::Request.new(options[:class] || self)
  yield request
  response = request.perform
  
  case options[:returning]
  when :raw
    response.body
  when :json
    response.json
  when :response
    response
  else
    response.to_objects
  end
end

#json_service(name, request_options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/voorhees/resource.rb', line 23

def json_service(name, request_options={})
  klass = request_options.delete(:class) || self
  (class << self; self; end).instance_eval do
    define_method name do |*args|
      params = args[0]
      json_request(:class => klass) do |r|
        r.parameters = params if params.is_a?(Hash)
        request_options.each do |option, value|
          r.send("#{option}=", value)
        end
      end
    end
  end
end

#new_from_json(json, hierarchy = nil) ⇒ Object



16
17
18
19
20
21
# File 'lib/voorhees/resource.rb', line 16

def new_from_json(json, hierarchy=nil)
  obj = self.new
  obj.raw_json       = json
  obj.json_hierarchy = hierarchy
  obj
end