Class: Ubiquitously::Dzone::Post

Inherits:
Base::Post show all
Defined in:
lib/ubiquitously/dzone.rb

Instance Attribute Summary

Attributes inherited from Base::Post

#categories, #description, #service_url, #tags, #title, #url, #user

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base::Post

#agent, create

Methods included from Resourceful

included

Class Method Details

.find(url) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ubiquitously/dzone.rb', line 40

def find(url)
  query = CGI.escape(url.gsub(/http(s)?\:\/\//, ""))
  search = "http://www.dzone.com/links/search.html?query=#{query}"
  html = Nokogiri::HTML(open(search).read)
  records = []
  
  html.css(".linkblock").each do |node|
    link = node.css(".thumb a").first["href"]
    header = node.css(".details h3 a").first
    title = header.text
    service_url = header["href"]
    records << Ubiquitously::Dzone::Post.new(
      :url => link,
      :title => title,
      :service_url => service_url
    )
  end
  
  records
end

.first(url) ⇒ Object



32
33
34
# File 'lib/ubiquitously/dzone.rb', line 32

def first(url)
  find(url).first
end

.last(url) ⇒ Object



36
37
38
# File 'lib/ubiquitously/dzone.rb', line 36

def last(url)
  find(url).last
end

.new_record?(url, throw_error = false) ⇒ Boolean

www.dzone.com/links/feed/user/<user_id>/rss.xml check to see if dzone already has the url

Returns:

  • (Boolean)

Raises:



63
64
65
66
67
# File 'lib/ubiquitously/dzone.rb', line 63

def new_record?(url, throw_error = false)
  exists = !find(url).blank?
  raise DuplicateError.new("DZone already links to #{url}") if throw_error && exists
  exists
end

Instance Method Details

#new_record?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/ubiquitously/dzone.rb', line 106

def new_record?
  self.class.new_record?(url)
end

#save(options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ubiquitously/dzone.rb', line 70

def save(options = {})
  return false unless valid? && new_record?
  
  user.
  
  # url
  page        = agent.get("http://www.dzone.com/links/add.html")
  form        = page.form_with(:action => "/links/add.html")
  
  accepted_tags = []
  
  form.checkboxes_with(:name => "tags").each do |tag|
    accepted_tags << tag.value.to_s
  end
  
  unaccepted_tags = tags.select { |tag| !accepted_tags.include?(tag) }
  
  if unaccepted_tags.length > 0
    raise ArgumentError.new("DZone doesn't accept these tags: #{unaccepted_tags.inspect}, they want these:\n#{accepted_tags.inspect}")
  end
  
  form["title"] = title
  form["url"] = url
  form["description"] = description
  
  form.checkboxes_with(:name => "tags").each do |checkbox|
    checkbox.check if tags.include?(checkbox.value)
  end
  
  unless options[:debug] == true
    page = form.submit
  end
  
  true
end