Class: WWW::Delicious::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/www/delicious/post.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values_or_rexml) {|_self| ... } ⇒ Post

Creates a new WWW::Delicious::Post with given values. If values_or_rexml is a REXML element, the element is parsed and all values assigned to this instance attributes.

Yields:

  • (_self)

Yield Parameters:



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/www/delicious/post.rb', line 32

def initialize(values_or_rexml, &block) #  :yields: post
  case values_or_rexml
    when Hash
      initialize_from_hash(values_or_rexml)
    when REXML::Element
      initialize_from_rexml(values_or_rexml)
    else
      raise ArgumentError, 'Expected `values_or_rexml` to be `Hash` or `REXML::Element`'
  end
  
  yield(self) if block_given?
  self
end

Instance Attribute Details

#notesObject

TODO: filter and validate



23
24
25
# File 'lib/www/delicious/post.rb', line 23

def notes
  @notes
end

#othersObject

TODO: filter and validate



23
24
25
# File 'lib/www/delicious/post.rb', line 23

def others
  @others
end

#replaceObject

Returns the value of attribute replace.



24
25
26
# File 'lib/www/delicious/post.rb', line 24

def replace
  @replace
end

#sharedObject

Returns the value of attribute shared.



24
25
26
# File 'lib/www/delicious/post.rb', line 24

def shared
  @shared
end

#tagsObject

TODO: filter and validate



23
24
25
# File 'lib/www/delicious/post.rb', line 23

def tags
  @tags
end

#timeObject

TODO: filter and validate



23
24
25
# File 'lib/www/delicious/post.rb', line 23

def time
  @time
end

#titleObject

TODO: filter and validate



23
24
25
# File 'lib/www/delicious/post.rb', line 23

def title
  @title
end

#uidObject

TODO: filter and validate



23
24
25
# File 'lib/www/delicious/post.rb', line 23

def uid
  @uid
end

#urlObject

TODO: filter and validate



23
24
25
# File 'lib/www/delicious/post.rb', line 23

def url
  @url
end

Instance Method Details

#api_valid?Boolean

Returns wheter this object is valid for an API request.

To be valid url and title must not be empty.

Examples

post = WWW::Delicious::Post.new(:url => 'http://localhost', :title => 'foo')
post.api_valid?
# => true

post = WWW::Delicious::Post.new(:url => 'http://localhost')
post.api_valid?
# => false

Returns:

  • (Boolean)


106
107
108
# File 'lib/www/delicious/post.rb', line 106

def api_valid?
  return !(url.nil? or url.empty? or title.nil? or title.empty?)
end

#initialize_from_hash(values) ⇒ Object

Initializes WWW::Delicious::Post from an Hash.



50
51
52
53
54
55
56
# File 'lib/www/delicious/post.rb', line 50

def initialize_from_hash(values)
  %w(url title notes others udi tags time shared replace).each do |v|
    self.instance_variable_set("@#{v}".to_sym(), values[v.to_sym()])
  end
  self.shared  = true    if self.shared.nil?
  self.replace = true    if self.replace.nil?
end

#initialize_from_rexml(element) ⇒ Object

Initializes WWW::Delicious::Post from a REXML fragment.



62
63
64
65
66
67
68
69
70
71
# File 'lib/www/delicious/post.rb', line 62

def initialize_from_rexml(element)
  self.url    = element.attribute_value(:href) { |v| URI.parse(v) }
  self.title  = element.attribute_value(:description)
  self.notes  = element.attribute_value(:extended)
  self.others = element.attribute_value(:others).to_i() # cast nil to 0
  self.uid    = element.attribute_value(:hash)
  self.tags   = element.attribute_value(:tag)  { |v| v.split(' ') }.to_a()
  self.time   = element.attribute_value(:time) { |v| Time.parse(v) }
  self.shared = element.attribute_value(:shared) { |v| v == 'no' ? false : true }
end

#to_paramsObject

Returns a params-style representation suitable for API calls.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/www/delicious/post.rb', line 77

def to_params()
  params = {}
  params[:url] = self.url
  params[:description] = self.title
  params[:extended] = self.notes if self.notes
  params[:shared] = self.shared
  params[:tags] = self.tags.join(' ') if self.tags
  params[:replace] = self.replace
  params[:dt] = WWW::Delicious::TIME_CONVERTER.call(self.time) if self.time
  return params
end