Class: Ubiquitously::Reddit::Post

Inherits:
Service::Post show all
Defined in:
lib/ubiquitously/services/reddit.rb

Instance Attribute Summary

Attributes inherited from Service::Post

#captcha, #categories, #description, #downvotes, #extension, #format, #image, #kind, #must_be_unique, #privacy, #rating, #remote, #service_id, #service_url, #slug, #source, #source_url, #state, #status, #tags, #title, #token, #upvotes, #url, #user, #vote

Instance Method Summary collapse

Methods inherited from Service::Post

#access_token, #initialize

Methods included from Loggable::Post

included

Methods included from Ubiquitously::Restful::Post

included

Methods included from Postable::Post

included

Methods included from Ownable::Post

included

Methods inherited from Base

#apply, #debug?

Methods included from SubclassableCallbacks

included, override

Constructor Details

This class inherits a constructor from Ubiquitously::Service::Post

Instance Method Details

#createObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ubiquitously/services/reddit.rb', line 16

def create
  page = agent.get("http://www.reddit.com/submit?url=#{url}")
  key = page.body.match(/modhash\:\s+'([^']+)'/)[1]
  
  if page.parser.css(".infobar a").first.text =~ /submit it again/i
    post = page.parser.css(".thing").first
    id = post["class"].match(/id-(\w+)/)[1]
    vh = post.css(".arrow.up").first["onclick"].match(/\((?:\s+)?['"](\w+)['"]/)[1]
    post_site = page.body.match(/post_site:\s+['"]([^'"]+)['"]/)[1] rescue "javascript"
    params = {
      "id" => id,
      "vh" => vh,
      "dir" => 1,
      "uh" => key,
      "renderstyle" => "html",
      "r" => post_site
    }
    
    # dir = 1, 0, or -1. “direction” of vote.
    headers = {
      "X-Requested-With" => "XMLHttpRequest",
      "Accept" => "application/json, text/javascript, */*"
    }
    unless debug?
      page = agent.post("http://www.reddit.com/api/vote", params, headers)
    end
  else
    form = page.forms.detect {|form| form.form_node["id"] == "newlink"}
    form["title"] = title
    form["url"]   = url
    form["id"] = "#newlink"
    form["uh"] = key
    form["renderstyle"] = "html"
    form["kind"] = "link"
    
    form.action = "http://www.reddit.com/api/submit"
    headers = {
      "X-Requested-With" => "XMLHttpRequest"
    }
    unless debug?
      page = form.submit(nil, headers)
    end
  end
end