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.
69 70 71 |
# File 'lib/mirrored/post.rb', line 69 def code @code end |
#description ⇒ Object
Returns the value of attribute description.
69 70 71 |
# File 'lib/mirrored/post.rb', line 69 def description @description end |
#extended ⇒ Object
Returns the value of attribute extended.
69 70 71 |
# File 'lib/mirrored/post.rb', line 69 def extended @extended end |
#hash ⇒ Object
Returns the value of attribute hash.
69 70 71 |
# File 'lib/mirrored/post.rb', line 69 def hash @hash end |
#href ⇒ Object Also known as: url
Returns the value of attribute href.
69 70 71 |
# File 'lib/mirrored/post.rb', line 69 def href @href end |
#others ⇒ Object
Returns the value of attribute others.
69 70 71 |
# File 'lib/mirrored/post.rb', line 69 def others @others end |
#share ⇒ Object
Defaults share to yes
109 110 111 |
# File 'lib/mirrored/post.rb', line 109 def share @share end |
#tags ⇒ Object
Returns the value of attribute tags.
69 70 71 |
# File 'lib/mirrored/post.rb', line 69 def @tags end |
#time ⇒ Object Also known as: dt
Returns the value of attribute time.
69 70 71 |
# File 'lib/mirrored/post.rb', line 69 def time @time end |
Class Method Details
.destroy(url) ⇒ Object Also known as: delete
58 59 60 61 62 63 64 65 66 |
# File 'lib/mirrored/post.rb', line 58 def self.destroy(url) doc = Hpricot::XML(connection.get('posts/delete', :url => url)) if doc && doc.at('result') code = doc.at('result')['code'] code == 'done' ? true : false else false end 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mirrored/post.rb', line 35 def self.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.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/mirrored/post.rb', line 4 def self.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
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/mirrored/post.rb', line 86 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)) if doc && doc.at('result') @code = doc.at('result')['code'] (@code == 'done') ? true : false else false end end |
#to_h ⇒ Object
:nodoc:
98 99 100 101 102 103 104 105 106 |
# File 'lib/mirrored/post.rb', line 98 def to_h #:nodoc: { :url => url, :description => description, :extended => extended, :tags => ( ? .map { |t| t.gsub(' ', '_') }.join(' ') : ''), :share => share } end |