Class: Oos4ruby::Collection
- Inherits:
-
Array
- Object
- Array
- Oos4ruby::Collection
- Defined in:
- lib/oos4ruby/collection.rb
Overview
This class wraps feed response. You must use its subclass, Oos4ruby::Sites, Oos4ruby::Contacts, Oos4ruby::Medias
API feed response is paginated, so, this class provides some methods to explore the feed.
Instance Method Summary collapse
-
#all ⇒ Object
return all entries into the feed.
-
#contains?(elem, value) ⇒ Boolean
return true if the text of the element is equals to the given value.
-
#create!(opts = {}) ⇒ Object
method to create a resource into a collection.
-
#initialize(feed, auth, slug = nil) ⇒ Collection
constructor
A new instance of Collection.
-
#next_page ⇒ Object
returns the next feed as a new Collection subclass.
-
#next_page! ⇒ Object
updats the current Collection with the next feed response.
-
#next_page? ⇒ Boolean
returns true if next page exists into the feed.
-
#previous! ⇒ Object
updates the current Collection with the precious feed response.
-
#previous_page ⇒ Object
returns the previous feed as a new Collection subclass.
-
#previous_page? ⇒ Boolean
returns true if previous page exists into the feed.
-
#refresh! ⇒ Object
reload the current collection.
-
#total_size ⇒ Object
returns the total size of the feed.
Constructor Details
#initialize(feed, auth, slug = nil) ⇒ Collection
Returns a new instance of Collection.
10 11 12 13 14 15 16 |
# File 'lib/oos4ruby/collection.rb', line 10 def initialize(feed, auth, slug = nil) @feed = feed @auth = auth @slug = slug evalued_class = Oos4ruby.const_get(self.class.name.gsub(/Oos4ruby::/, '').gsub(/s$/, '')) @feed.entries.each {|entry| self << evalued_class.new(entry, @auth, @slug) } end |
Instance Method Details
#all ⇒ Object
return all entries into the feed.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/oos4ruby/collection.rb', line 70 def all all = self.to_a aux = self while aux.next_page? next_page = aux.next_page next_page.each {|entry| all << entry} aux = next_page end return all end |
#contains?(elem, value) ⇒ Boolean
return true if the text of the element is equals to the given value
i.e:
user.contacts.contains? 'oos:slug', 'calavera'
user.sites.contains? 'oos:locality', 'Madrid'
137 138 139 140 141 142 143 144 |
# File 'lib/oos4ruby/collection.rb', line 137 def contains?(elem, value) self.each do |obj| if obj.contains?(elem, value) return true end end return false end |
#create!(opts = {}) ⇒ Object
method to create a resource into a collection.
opts available keys:
* if you want to create a Site:
:id => 11870 service/place id if it already exists in 11780
if the service/place doesn't exit in 11870 then
:title => service/place name (required)
:user_address (required)
:url (optional)
:telephone (optional)
:locality => a hash with :name and :slug (required)
:country => a hash with :name and :slug (required)
:summary => review title
:content or :review_content => review_content
:privacy => a REXML::Element from 11870 privacy scheme or one of the next strings => "public", "trustedContacts", "contacts", "private"
:tags => an array of tags
:lists => an array of lists
* if you want to create a Contact:
:contact or :slug or :title => the new contact slug
:trusted => a REXML::Element from 11870 trusted scheme or a string with "true" or "false"
* if you want to create a Media:
:file => the new media file system path or a File object
:content_length => file size
:content_type => file content type
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/oos4ruby/collection.rb', line 113 def create!(opts = {}) evalued_class = Oos4ruby.const_get(self.class.name.gsub(/Oos4ruby::/, '').gsub(/s$/, '')) body = evalued_class.dump! opts, @slug unless self.instance_of?Medias body = "<?xml version='1.0' ?>\n" + body.to_s end poster = HTTPInvoker.new( @feed.self_link, @auth) content_type = opts.delete(:content_type) || AtomEntryContentType opts.each {|k, v| poster.set_header(k, v)} worked = poster.post content_type, body raise poster.response. unless worked return get_page(@feed.self_link).first end |
#next_page ⇒ Object
returns the next feed as a new Collection subclass.
35 36 37 |
# File 'lib/oos4ruby/collection.rb', line 35 def next_page return get_page(@feed.next) if next_page? end |
#next_page! ⇒ Object
updats the current Collection with the next feed response.
49 50 51 |
# File 'lib/oos4ruby/collection.rb', line 49 def next_page! return get_page!(@feed.next) end |
#next_page? ⇒ Boolean
returns true if next page exists into the feed
21 22 23 |
# File 'lib/oos4ruby/collection.rb', line 21 def next_page? @feed.next? end |
#previous! ⇒ Object
updates the current Collection with the precious feed response.
56 57 58 |
# File 'lib/oos4ruby/collection.rb', line 56 def previous! return get_page!(@feed.previous) end |
#previous_page ⇒ Object
returns the previous feed as a new Collection subclass.
42 43 44 |
# File 'lib/oos4ruby/collection.rb', line 42 def previous_page return get_page(@feed.previous) if previous_page? end |
#previous_page? ⇒ Boolean
returns true if previous page exists into the feed
28 29 30 |
# File 'lib/oos4ruby/collection.rb', line 28 def previous_page? @feed.previous? end |
#refresh! ⇒ Object
reload the current collection
84 85 86 |
# File 'lib/oos4ruby/collection.rb', line 84 def refresh! return get_page!(@feed.self_link) end |
#total_size ⇒ Object
returns the total size of the feed.
63 64 65 |
# File 'lib/oos4ruby/collection.rb', line 63 def total_size return @feed.total_size end |