dangeru 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/dangeru.rb +146 -0
  3. metadata +78 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2eb2f25fbc2f2f6dd5c1f6676779637e1c7ec94c
4
+ data.tar.gz: f977d472e0bde4854965aa6d84a56405743d3345
5
+ SHA512:
6
+ metadata.gz: d4b10b431b11df8f7fd0c432977c4fe21b5352104f90e2ba9bf05d9a4ab40895acc2933b55ae7b11464b237eb2db1e59be0bc034d842e5f48c82a0eae7853519
7
+ data.tar.gz: eecc3118544a2628b743c01e88f30f3b1c46c71371adda49033318e65233d8bdb74b2e055e6d539e30194b57956e3937573a6f0733f8a7000cda89aa0efe4a93
@@ -0,0 +1,146 @@
1
+ # Awoo API wrapper for ruby
2
+ require 'json'
3
+ require 'uri'
4
+ require 'net/http'
5
+ require 'http-cookie'
6
+
7
+ class Dangeru
8
+ API = "/api/v2".freeze
9
+ Dummy_uri = "http://dummy/".freeze
10
+ @hostname = ""
11
+ @ssl = false
12
+
13
+ def initialize(url="dangeru.us", https=false)
14
+ @hostname = url
15
+ @ssl = https
16
+ end
17
+
18
+ ###############
19
+ # Used to get entries from a board
20
+ # Returns a formatted JSON entry for the board
21
+ def get_board(board, cookie = nil)
22
+ JSON.parse(get(API + "/board/" + board, cookie).body)
23
+ end
24
+
25
+ ###############
26
+ # Used to get replies from a thread
27
+ # Returns a formatted JSON entry for the replies
28
+ def get_thread_replies(id, cookie = nil)
29
+ JSON.parse(get(API + "/thread/" + id.to_s + "/replies", cookie).body)
30
+ end
31
+
32
+ ###############
33
+ # Used to get metadata from a thread
34
+ # Returns a formatted JSON entry for the metadata
35
+ def get_thread_metadata(id, cookie = nil)
36
+ JSON.parse(get(API + "/thread/" + id.to_s + "/metadata", cookie).body)
37
+ end
38
+
39
+ ###############
40
+ # Used to find a post by title
41
+ # Returns the post on success, and false on failure
42
+ def find_post(board, title, cookie = nil)
43
+ get_board(board, cookie).each do |post|
44
+ if post["title"] == title
45
+ return post
46
+ end
47
+ end
48
+ return false
49
+ end
50
+
51
+ ###############
52
+ # Used to authenticate as janitor
53
+ # Returns an auth cookie
54
+ def auth(name, pass)
55
+ res = post("/mod", nil, {"username": name, "password": pass})
56
+ jar = HTTP::CookieJar.new
57
+ res.get_fields("Set-Cookie").each do |value|
58
+ jar.parse(value, Dummy_uri)
59
+ end
60
+ jar
61
+ end
62
+
63
+ ###############
64
+ # Used to post an OP to a board
65
+ # Returns nothing
66
+ def post_op(board, title, comment, cookie=nil, capcode=false)
67
+ unless capcode then
68
+ post("/post", cookie, {"board" => board, "title" => title, "comment" => comment})
69
+ else
70
+ post("/post", cookie, {"board" => board, "title" => title, "comment" => comment, "capcode" => "true"})
71
+ end
72
+ end
73
+
74
+ ###############
75
+ # Used to reply to an OP
76
+ # Returns nothing
77
+ def reply(parent_id, board, comment, cookie=nil, capcode=false)
78
+ unless capcode then
79
+ post("/reply", cookie, {"board" => board, "parent" => parent_id.to_s, "content" => comment})
80
+ else
81
+ post("/reply", cookie, {"board" => board, "parent" => parent_id.to_s, "content" => comment, "capcode" => "true"})
82
+ end
83
+ end
84
+
85
+ # Moderation tools
86
+
87
+ ###############
88
+ # Used to sticky a post
89
+ # Returns nothing
90
+ def sticky(id, cookie, stickyness=1)
91
+ post("/sticky/" + id.to_s, cookie, {"stickyness" => stickyness})
92
+ end
93
+
94
+ ###############
95
+ # Used to unsticky a post
96
+ # Returns nothing
97
+ def unsticky(id, cookie)
98
+ get("/unsticky/" + id.to_s, cookie)
99
+ end
100
+
101
+ ###############
102
+ # Used to lock a post
103
+ # Returns nothing
104
+ def lock(id, cookie)
105
+ get("/lock/" + id.to_s, cookie)
106
+ end
107
+
108
+ ###############
109
+ # Used to unlock a post
110
+ # Returns nothing
111
+ def unlock(id, cookie)
112
+ get("/unlock/" + id.to_s, cookie)
113
+ end
114
+
115
+ ###############
116
+ # Used to move a post
117
+ # Returns nothing
118
+ def move(id, to, cookie)
119
+ post("/move/" + id.to_s, cookie, {"board" => to})
120
+ end
121
+
122
+ # Helper functions
123
+
124
+ def get(route, cookie = nil, params = nil)
125
+ if @ssl then
126
+ uri = URI("https://#{@hostname}/#{route}")
127
+ else
128
+ uri = URI("http://#{@hostname}/#{route}")
129
+ end
130
+ uri.query = URI.www_encode_form(params) if params
131
+ Net::HTTP.start(@hostname, :use_ssl => @ssl) do |http|
132
+ request = Net::HTTP::Get.new uri
133
+ request["Cookie"] = HTTP::Cookie.cookie_value(cookie.cookies) if cookie
134
+ http.request request
135
+ end
136
+ end
137
+
138
+ def post(route, cookie = nil, params = nil)
139
+ Net::HTTP.start(@hostname, :use_ssl => @ssl) do |http|
140
+ request = Net::HTTP::Post.new route
141
+ request.set_form_data(params) if params
142
+ request["Cookie"] = HTTP::Cookie.cookie_value(cookie.cookies) if cookie
143
+ http.request request
144
+ end
145
+ end
146
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dangeru
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - prefetcher
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http-cookie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.0.0
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '1.0'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.0.0
47
+ description: Awoo / danger/u/ API wrapper for ruby
48
+ email: 0xlunaric@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/dangeru.rb
54
+ homepage: http://rubygems.org/gems/dangeru
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.6.11
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: dangeru.rb
78
+ test_files: []