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.



3
4
5
# File 'lib/mirrored/post.rb', line 3

def code
  @code
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/mirrored/post.rb', line 3

def description
  @description
end

#extendedObject

Returns the value of attribute extended.



3
4
5
# File 'lib/mirrored/post.rb', line 3

def extended
  @extended
end

#hashObject

Returns the value of attribute hash.



3
4
5
# File 'lib/mirrored/post.rb', line 3

def hash
  @hash
end

#hrefObject Also known as: url

Returns the value of attribute href.



3
4
5
# File 'lib/mirrored/post.rb', line 3

def href
  @href
end

#othersObject

Returns the value of attribute others.



3
4
5
# File 'lib/mirrored/post.rb', line 3

def others
  @others
end

#shareObject

Defaults share to yes



111
112
113
# File 'lib/mirrored/post.rb', line 111

def share
  @share
end

#tagsObject

Returns the value of attribute tags.



3
4
5
# File 'lib/mirrored/post.rb', line 3

def tags
  @tags
end

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

Destroys a post by url.

Usage:

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


66
67
68
69
70
71
72
73
74
# File 'lib/mirrored/post.rb', line 66

def 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)


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.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.



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.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


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mirrored/post.rb', line 88

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:



100
101
102
103
104
105
106
107
108
# File 'lib/mirrored/post.rb', line 100

def to_h #:nodoc:
  {
    :url         => url, 
    :description => description, 
    :extended    => extended, 
    :tags        => (tags ? tags.map { |t| t.gsub(' ', '_') }.join(' ') : ''),
    :share       => share
  }
end