Class: Mirrored::Post

Inherits:
Base
  • Object
show all
Defined in:
lib/mirrored/post.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_url, connection, establish_connection, remove_connection, service, valid_service?

Instance Attribute Details

#codeObject

Returns the value of attribute code.



69
70
71
# File 'lib/mirrored/post.rb', line 69

def code
  @code
end

#descriptionObject

Returns the value of attribute description.



69
70
71
# File 'lib/mirrored/post.rb', line 69

def description
  @description
end

#extendedObject

Returns the value of attribute extended.



69
70
71
# File 'lib/mirrored/post.rb', line 69

def extended
  @extended
end

#hashObject

Returns the value of attribute hash.



69
70
71
# File 'lib/mirrored/post.rb', line 69

def hash
  @hash
end

#hrefObject Also known as: url

Returns the value of attribute href.



69
70
71
# File 'lib/mirrored/post.rb', line 69

def href
  @href
end

#othersObject

Returns the value of attribute others.



69
70
71
# File 'lib/mirrored/post.rb', line 69

def others
  @others
end

#shareObject

Defaults share to yes



109
110
111
# File 'lib/mirrored/post.rb', line 109

def share
  @share
end

#tagsObject

Returns the value of attribute tags.



69
70
71
# File 'lib/mirrored/post.rb', line 69

def tags
  @tags
end

#timeObject 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

Destroys a post by url.

Usage:

Mirrored::Post.destroy('http://microsoft.com')  
Mirrored::Post.delete('http://microsoft.com')   # => aliased version


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

Raises:

  • (ArgumentError)


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.extract_options!
  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.tags        = (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.tags        = %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_hObject

: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        => (tags ? tags.map { |t| t.gsub(' ', '_') }.join(' ') : ''),
    :share       => share
  }
end