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

#==, all_filters, body_root, build, children, #clear_dirtiness, create_has_many_attribute, #curl, custom_resource_name, #delete, filters, has_many, inherited, #initialize, mk_proxy, #new_record?, parent, read_only_attributes, #reload, resource_name, #save, #save!, source

Methods included from BelongsTo

included

Methods included from HasAttributes

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

Constructor Details

This class inherits a constructor from Arrest::AbstractResource

Class Method Details

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



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/arrest/root_resource.rb', line 40

def all(context, filter={})
  begin
    body = body_root(source().get_many(context, self.resource_path, filter))
  rescue Arrest::Errors::DocumentNotFoundError
    Arrest::logger.info "DocumentNotFoundError for #{self.resource_path} gracefully returning []"
    return []
  end
  body ||= []
  body.map do |h|
    self.build(context, h)
  end
end

.by_url(context, url) ⇒ Object

Rertrieves 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_many(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

.current_page_countObject

:nodoc:



75
76
77
# File 'lib/arrest/root_resource.rb', line 75

def current_page_count #:nodoc:
  count
end

.delete_all(context) ⇒ Object



147
148
149
# File 'lib/arrest/root_resource.rb', line 147

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

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



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

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

.filter(name, &aproc) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/arrest/root_resource.rb', line 95

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/arrest/root_resource.rb', line 80

def find(context, id)
  if id == nil || "" == id
    Arrest::logger.info "DocumentNotFoundError: no id given"
    raise Errors::DocumentNotFoundError.new
  end
  r = source().get_one(context, "#{self.resource_path}/#{id}")
  body = body_root(r)
  if body == nil || body.empty?
    Arrest::logger.info "DocumentNotFoundError for #{self.resource_path}/#{id}"
    raise Errors::DocumentNotFoundError.new
  end
  resource = self.build(context, body.merge({:id => id}))
  resource
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

.limit_valueObject

Methods for pagination



55
56
57
# File 'lib/arrest/root_resource.rb', line 55

def limit_value #:nodoc:
  query.options[:limit] || 0
end

.offset_valueObject

:nodoc:



59
60
61
# File 'lib/arrest/root_resource.rb', line 59

def offset_value #:nodoc:
  query.options[:offset] || 0
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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/arrest/root_resource.rb', line 118

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

end

.scoped_path(scope_name) ⇒ Object



133
134
135
# File 'lib/arrest/root_resource.rb', line 133

def scoped_path scope_name
  resource_path + '/' + scope_name.to_s
end

.stub(context, stub_id) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/arrest/root_resource.rb', line 137

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

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

:nodoc:



63
64
65
66
67
68
69
70
71
72
# File 'lib/arrest/root_resource.rb', line 63

def total_count(context, filter = {}) #:nodoc:
  begin
    response = source().get_many(context, self.resource_path, filter)
    all = JSON.parse(response)
    all['result_count'].to_i
  rescue Arrest::Errors::DocumentNotFoundError
    Arrest::logger.info "DocumentNotFoundError for #{self.resource_path} gracefully returning []"
    return 0
  end
end

Instance Method Details

#resource_locationObject



156
157
158
# File 'lib/arrest/root_resource.rb', line 156

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

#resource_pathObject



152
153
154
# File 'lib/arrest/root_resource.rb', line 152

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