Class: Arrest::RootResource

Inherits:
AbstractResource show all
Defined in:
lib/arrest/root_resource.rb

Direct Known Subclasses

SingleResource

Instance Attribute Summary

Attributes inherited from AbstractResource

#context, #id

Attributes included from HasAttributes

#attribute_values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractResource

#==, active_resource_classes, all_filters, body_root, build, #clone, #curl, custom_json_type, custom_resource_name, #delete, filters, inherited, #initialize, json_type_map, json_type_to_class, mk_proxy, #new_record?, parent, read_only_attributes, #reload, resource_name, #save, #save!, source, #to_json_type, to_json_type

Methods included from BelongsTo

included

Methods included from HasView

included

Methods included from HasAttributes

#attributes, #attributes=, included, #init_from_hash, #initialize, #initialize_has_attributes, #load_from_stub, #render_field_to_hash?, #reset_dirtiness, #stubbed?, #to_hash, #to_jhash, #update_attributes

Methods included from HasMany

included

Constructor Details

This class inherits a constructor from Arrest::AbstractResource

Class Method Details

.all(context, filter = {}) ⇒ Object



40
41
42
# File 'lib/arrest/root_resource.rb', line 40

def all(context, filter={})
  Arrest::OrderedCollection.new(context, self, self.resource_path, filter)
end

.by_url(context, url) ⇒ Object

Retrieves a collection of objects and returns them in a hash combined with metadata



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/arrest/root_resource.rb', line 12

def by_url(context, url)
  begin
    response = source().get(context, url)
    parsed_hash = JSON.parse(response)
    result_count = parsed_hash['result_count']
    body = body_root(response)
  rescue Arrest::Errors::DocumentNotFoundError
    Arrest::logger.info "DocumentNotFoundError for #{url} gracefully returning []"
    return []
  end
  body ||= []
  collection = body.map do |h|
    self.build(context, h)
  end
  {
    :result_count => result_count,
    :collection => collection
  }
end

.delete_all(context) ⇒ Object



117
118
119
# File 'lib/arrest/root_resource.rb', line 117

def delete_all(context)
  source().delete_all(context, self.resource_path)
end

.filter(name, &aproc) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/arrest/root_resource.rb', line 64

def filter name, &aproc
  if aproc != nil
    if @filters == nil
      @filters = []
    end
    @filters << Filter.new(name, &aproc)
    send :define_singleton_method, "FILTER_#{name}" do |args = nil|
      collection = args[0]
      call_args = args.drop(1)
      collection.select do |instance|
        instance.instance_exec(*call_args, &aproc)
      end
    end
    send :define_singleton_method, name do |context, args = nil|
      self.all(context).select do |instance|
        instance.instance_exec(args, &aproc)
      end
    end
  else
    raise "You must specify a block for a filter"
  end
end

.find(context, id) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/arrest/root_resource.rb', line 44

def find(context, id)
  context.cache.lookup(id) do
    begin
      raise "Document Id must not be blank" if id == nil || "" == id

      full_resource_path = "#{self.resource_path}/#{id}"
      r = source().get(context, full_resource_path)
      body = body_root(r)

      raise "Response body must not be empty for #{full_resource_path}" if body == nil || body.empty?

      resource = self.build(context, body.merge({:id => id}))
      resource
    rescue Exception => e
      Arrest::logger.info e.message if e.message
      raise Errors::SpecifiedDocumentNotFoundError.new(id, self.class)
    end
  end
end

.first(context, filter = {}) ⇒ Object



32
33
34
# File 'lib/arrest/root_resource.rb', line 32

def first(context, filter={})
  all(context,filter).first
end

.last(context, filter = {}) ⇒ Object



36
37
38
# File 'lib/arrest/root_resource.rb', line 36

def last(context, filter={})
  all(context,filter).last
end

.resource_pathObject



6
7
8
# File 'lib/arrest/root_resource.rb', line 6

def resource_path
  "#{self.resource_name}"
end

.scope(name, options = {}, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/arrest/root_resource.rb', line 87

def scope name, options = {}, &block
  super(name, options)
  if block_given?
    send :define_singleton_method, name do |context, filter = {}|
      self.all(context, filter).select(&block)
    end
  else
    send :define_singleton_method, name do |context, filter = {}|
      resource_name = options[:resource_name] || name
      Arrest::OrderedCollection.new(context, self, self.scoped_path(resource_name), filter)
    end
  end

end

.scoped_path(scope_name) ⇒ Object



102
103
104
105
# File 'lib/arrest/root_resource.rb', line 102

def scoped_path scope_name
  scope_name = scope_name.to_s
  resource_path + (scope_name.start_with?('?') ? '' : '/') + scope_name
end

.stub(context, stub_id) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/arrest/root_resource.rb', line 107

def stub(context, stub_id)
  n = self.new(context)
  n.initialize_has_attributes({:id => stub_id}) do
    r = n.class.source().get(@context, "#{self.resource_path}/#{stub_id}")
    body = n.class.body_root(r)
    n.init_from_hash(body, true)
  end
  n
end

Instance Method Details

#resource_locationObject



126
127
128
# File 'lib/arrest/root_resource.rb', line 126

def resource_location
  self.class.resource_path + '/' + self.id.to_s
end

#resource_pathObject



122
123
124
# File 'lib/arrest/root_resource.rb', line 122

def resource_path
  "#{self.class.resource_name}"
end