Class: UserscriptsOrg
- Inherits:
-
Object
show all
- Defined in:
- lib/userscripts_org.rb
Defined Under Namespace
Classes: NotLoggedinError, Script, UserscriptsOrgError
Constant Summary
collapse
- VERSION =
"0.0.3"
- BASE =
URI("http://userscripts.org/")
Instance Method Summary
collapse
Constructor Details
Returns a new instance of UserscriptsOrg.
18
19
20
21
22
|
# File 'lib/userscripts_org.rb', line 18
def initialize(config)
@config = OpenStruct.new(config)
@agent = WWW::Mechanize.new
@uid = nil
end
|
Instance Method Details
#create(content) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/userscripts_org.rb', line 84
def create(content)
@agent.get BASE + "/scripts/new"
form = @agent.page.forms.find {|i| i.action == "/scripts/create" }
form["which_source"] = "textarea"
form["script[src]"] = content
@agent.submit(form, form.buttons.find {|i| i.name == "commit" })
p @agent.page
p @agent.page.uri
get(@agent.page.uri.to_s[%r|/scripts/show/(\d+)|, 1])
end
|
#get(script) ⇒ Object
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/userscripts_org.rb', line 73
def get(script)
uid = script.respond_to?(:uid) ? script.uid : script
@agent.get BASE + "/scripts/source/#{uid}.user.js"
content = @agent.page.content
Script.new({
:uid => uid,
:name => content[/@name\s+(.+)/, 1].chomp,
:content => content,
})
end
|
#list ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/userscripts_org.rb', line 41
def list
check
@agent.get(BASE + "/users/#{@uid}/scripts")
@agent.page.links.map {|i|
uid = i.href[%r|/scripts/show/(\d+)|, 1]
uid ? Script.new(
:uid => uid.to_i,
:name => i.text
) : nil
}.compact
end
|
#login ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/userscripts_org.rb', line 28
def login
@agent.get(BASE + "/login")
form = @agent.page.forms.find {|i| i.action == "/sessions" }
form.login = @config.username
form.password = @config.password
@agent.submit(form, form.buttons.find {|i| i.name == "commit"})
@uid = @agent.page.links.find {|i| i.text == "public profile" }.href[%r|/users/(\d+)|, 1].to_i
self
end
|
#logined? ⇒ Boolean
24
25
26
|
# File 'lib/userscripts_org.rb', line 24
def logined?
!!@uid
end
|
#update(name, content) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/userscripts_org.rb', line 53
def update(name, content)
script = list.find {|i|
i == name || i.name == name || i.uid == name
}
raise UserscriptsOrgError, "Script named #{name} is not found." if script.nil?
@agent.get BASE + "/scripts/edit_src/#{script.uid}"
form = @agent.page.forms.find {|i| i.action =~ %r|^/scripts/update_src/| }
form["which_source"] = "textarea"
form["script[src]"] = content
@agent.submit(form, form.buttons.find {|i| i.name == "commit"})
date = (@agent.page.root/"//span[@class = 'date']").text.gsub(/^\s+|\s+$/, "")
unless date == "Last update a few seconds ago"
raise UserscriptsOrgError, "Update may failed"
end
script
end
|