Class: Mirrored::Post
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#description ⇒ Object
Returns the value of attribute description.
-
#extended ⇒ Object
Returns the value of attribute extended.
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#href ⇒ Object
(also: #url)
Returns the value of attribute href.
-
#others ⇒ Object
Returns the value of attribute others.
-
#share ⇒ Object
Defaults share to yes.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#time ⇒ Object
(also: #dt)
Returns the value of attribute time.
Class Method Summary collapse
-
.destroy(url) ⇒ Object
(also: delete)
Destroys a post by url.
-
.find(*args) ⇒ Object
Does all the hard work finding your posts by various filters and such.
-
.new_from_xml(xml) ⇒ Object
Creates a new post from an hpricot post instance.
Instance Method Summary collapse
-
#save(replace = false) ⇒ Object
Saves a post to delicious or magnolia.
-
#to_h ⇒ Object
:nodoc:.
Methods inherited from Base
api_url, connection, establish_connection, remove_connection, service, valid_service?
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
3 4 5 |
# File 'lib/mirrored/post.rb', line 3 def code @code end |
#description ⇒ Object
Returns the value of attribute description.
3 4 5 |
# File 'lib/mirrored/post.rb', line 3 def description @description end |
#extended ⇒ Object
Returns the value of attribute extended.
3 4 5 |
# File 'lib/mirrored/post.rb', line 3 def extended @extended end |
#hash ⇒ Object
Returns the value of attribute hash.
3 4 5 |
# File 'lib/mirrored/post.rb', line 3 def hash @hash end |
#href ⇒ Object Also known as: url
Returns the value of attribute href.
3 4 5 |
# File 'lib/mirrored/post.rb', line 3 def href @href end |
#others ⇒ Object
Returns the value of attribute others.
3 4 5 |
# File 'lib/mirrored/post.rb', line 3 def others @others end |
#share ⇒ Object
Defaults share to yes
103 104 105 |
# File 'lib/mirrored/post.rb', line 103 def share @share end |
#tags ⇒ Object
Returns the value of attribute tags.
3 4 5 |
# File 'lib/mirrored/post.rb', line 3 def @tags end |
#time ⇒ Object Also known as: dt
Returns the value of attribute time.
3 4 5 |
# File 'lib/mirrored/post.rb', line 3 def time @time end |
Class Method Details
.destroy(url) ⇒ Object Also known as: delete
66 67 68 69 70 |
# File 'lib/mirrored/post.rb', line 66 def destroy(url) doc = Hpricot::XML(connection.get('posts/delete', :url => url)) code = doc.at('result')['code'] code == 'done' ? true : false end |
.find(*args) ⇒ Object
Does all the hard work finding your posts by various filters and such.
Usage:
Valid hash options for :get are :tag, :dt and :url. All are optional and can be used in any combination.
Mirrored::Post.find(:get) # => posts
Mirrored::Post.find(:get, :tag => 'ruby') # => posts tagged ruby
Mirrored::Post.find(:get, :tag => 'ruby', :dt => '2007-10-05')# => posts tagged ruby on oct. 5, 2007
Mirrored::Post.find(:get, :url => 'http://addictedtonew') # => posts with url http://addictedtonew
Valid hash option for :all is :tag. All are optional and can be used in any combination. Use sparingly according to magnolia and delicious.
Mirrored::Post.find(:all) # => all posts
Mirrored::Post.find(:all, :tag => 'ruby') # => all posts for tag ruby
Valid hash options for :recent are :tag and :count. All are optional and can be used in any combination.
Mirrored::Post.find(:recent) # most recent posts
Mirrored::Post.find(:recent, :tag => 'ruby') # => most recent posts for tag ruby
Mirrored::Post.find(:recent, :tag => 'ruby', :count => '5') # => 5 most recent posts for tag ruby
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mirrored/post.rb', line 43 def find(*args) raise ArgumentError, "First argument must be symbol (:all or :recent)" unless args.first.kind_of?(Symbol) params = args. params = params == {} ? nil : params filter = case args.first when :recent 'recent' when :all 'all' else 'get' end doc = Hpricot::XML(connection.get("posts/#{filter}", params)) (doc/:post).inject([]) { |elements, el| elements << Post.new_from_xml(el); elements } end |
.new_from_xml(xml) ⇒ Object
Creates a new post from an hpricot post instance.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mirrored/post.rb', line 12 def new_from_xml(xml) #:nodoc: p = Post.new p.href = (xml)['href'] p.description = (xml)['description'] p.extended = (xml)['extended'] p.hash = (xml)['hash'] p.others = (xml)['others'] p. = (xml)['tag'] p.time = Time.parse((xml)['time']) p end |
Instance Method Details
#save(replace = false) ⇒ Object
Saves a post to delicious or magnolia.
Usage:
p = Mirrored::Post.new
p.url = 'http://addictedtonew.com'
p.description = 'Addicted to New by John Nuneamker'
p.extended = 'Really cool dude'
p. = %w(cool dude ruby)
p.save # => do not replace if post already exists
p.save(true) # => replace post if it already exists
84 85 86 87 88 89 90 |
# File 'lib/mirrored/post.rb', line 84 def save(replace=false) attrs = to_h.merge((replace) ? {:replace => 'yes'} : {:replace => 'no'}) puts attrs.inspect doc = Hpricot::XML(self.class.connection.get('posts/add', attrs)) @code = doc.at('result')['code'] (@code == 'done') ? true : false end |
#to_h ⇒ Object
:nodoc:
92 93 94 95 96 97 98 99 100 |
# File 'lib/mirrored/post.rb', line 92 def to_h #:nodoc: { :url => url, :description => description, :extended => extended, :tags => ( ? .map { |t| t.gsub(' ', '_') }.join(' ') : ''), :share => share } end |