rubyhexagon 1.5.0 → 1.6.0
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.
- checksums.yaml +4 -4
- data/lib/rubyhexagon.rb +2 -2
- data/lib/rubyhexagon/error.rb +14 -0
- data/lib/rubyhexagon/helper/api.rb +12 -11
- data/lib/rubyhexagon/post.rb +60 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1900ac35dde2dd785dbebf585f72044933f2b39cfefae51f05ef91b81b16e07b
|
4
|
+
data.tar.gz: d0acac948011df5b2a48203b75e50c99115f071794443e61a2ae96e61fd2ab91
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c7c225ac5f218caca1b2749ff6b50ddb7663482accf3b7118f0e4ef5eed3d2e36293915752dcf5a362727dcd87ccb897db194adcf81262e0a80076cdb3264d0
|
7
|
+
data.tar.gz: 855efb249f5e819ef0d1bbcfa4f52d0b5be048e9ab43247c2125b61bc5a2b8e57deb83dec937bc8c0570cd8087778ef45041596411f9458527ff7aef4ea7c8da
|
data/lib/rubyhexagon.rb
CHANGED
@@ -17,7 +17,7 @@
|
|
17
17
|
# You should have received a copy of the GNU General Public License
|
18
18
|
# along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
|
19
19
|
|
20
|
-
require '
|
20
|
+
require 'net/http'
|
21
21
|
require 'uri'
|
22
22
|
require 'json'
|
23
23
|
require 'digest/md5'
|
@@ -45,7 +45,7 @@ require_relative 'rubyhexagon/search/tag_history'
|
|
45
45
|
# @since 0.4.3
|
46
46
|
module Rubyhexagon
|
47
47
|
MAJOR = 1
|
48
|
-
MINOR =
|
48
|
+
MINOR = 6
|
49
49
|
PATCH = 0
|
50
50
|
NAME = 'rubyhexagon'
|
51
51
|
VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}"
|
data/lib/rubyhexagon/error.rb
CHANGED
@@ -27,3 +27,17 @@ class InvalidIDError < StandardError; end
|
|
27
27
|
# @author Maxine Michalski
|
28
28
|
# @since 1.0.0
|
29
29
|
class ObjectNotFilledError < StandardError; end
|
30
|
+
|
31
|
+
module Rubyhexagon
|
32
|
+
# Error class to denote that API access was not permitted.
|
33
|
+
#
|
34
|
+
# @author Maxine Michalski
|
35
|
+
# @since 1.6.0
|
36
|
+
class AccessDenied < StandardError; end
|
37
|
+
|
38
|
+
# Error class to denote that a Post was already favorited.
|
39
|
+
#
|
40
|
+
# @author Maxine Michalski
|
41
|
+
# @since 1.6.0
|
42
|
+
class AlreadyFavorited < StandardError; end
|
43
|
+
end
|
@@ -42,9 +42,10 @@ module Rubyhexagon
|
|
42
42
|
@user_agent = { 'User-Agent' =>
|
43
43
|
"#{Rubyhexagon::NAME}/#{Rubyhexagon::VERSION} "\
|
44
44
|
'(by maxine_red on e621' }
|
45
|
-
@
|
45
|
+
@http = Net::HTTP.new('e621.net', 443)
|
46
|
+
@http.use_ssl = true
|
46
47
|
lock_path = '/tmp/rubyhexagon.lock'
|
47
|
-
@lock_file = File.open(lock_path, 'a')
|
48
|
+
@lock_file = File.open(lock_path, 'a', 0o666)
|
48
49
|
end
|
49
50
|
|
50
51
|
# @author Maxine Michalski
|
@@ -58,18 +59,18 @@ module Rubyhexagon
|
|
58
59
|
# into account rate limiting.
|
59
60
|
#
|
60
61
|
# @return [Hash] JSON parsed response.
|
61
|
-
def fetch(noun, action, query)
|
62
|
-
|
63
|
-
|
64
|
-
query.store(:
|
65
|
-
query.store(:password_hash, login.password_hash)
|
62
|
+
def fetch(noun, action, query, method = 'GET')
|
63
|
+
if Login.instance.setup?
|
64
|
+
query.store(:login, Login.instance.login)
|
65
|
+
query.store(:password_hash, Login.instance.password_hash)
|
66
66
|
end
|
67
67
|
query = query.map { |k, v| "#{k}=#{v}" }.join('&')
|
68
|
-
query = "?#{query}" unless query == ''
|
69
|
-
uri = "#{@base}/#{noun}/#{action}.json#{query}"
|
70
|
-
uri = URI.parse(uri)
|
71
68
|
lock do
|
72
|
-
data =
|
69
|
+
data = if method == 'GET'
|
70
|
+
@http.get("/#{noun}/#{action}.json?#{query}", @user_agent).body
|
71
|
+
else
|
72
|
+
@http.post("/#{noun}/#{action}.json", query, @user_agent).body
|
73
|
+
end
|
73
74
|
JSON.parse(data, symbolize_names: true)
|
74
75
|
end
|
75
76
|
end
|
data/lib/rubyhexagon/post.rb
CHANGED
@@ -88,7 +88,7 @@ module Rubyhexagon
|
|
88
88
|
# @return the object
|
89
89
|
def initialize(id)
|
90
90
|
id = id.to_i
|
91
|
-
unless id.
|
91
|
+
unless id.positive?
|
92
92
|
raise InvalidIDError, "ID out of range: #{id}"
|
93
93
|
end
|
94
94
|
@id = id
|
@@ -120,6 +120,65 @@ module Rubyhexagon
|
|
120
120
|
tmp
|
121
121
|
end
|
122
122
|
|
123
|
+
# @author Maxine Michalski
|
124
|
+
#
|
125
|
+
# Send a vote to e621
|
126
|
+
#
|
127
|
+
# @param score [Integer] direction of vote, can be positive or negative
|
128
|
+
def vote!(score)
|
129
|
+
unless score.is_a?(Integer)
|
130
|
+
raise ArgumentError, 'Voting requires a positive/negative number'
|
131
|
+
end
|
132
|
+
raise ArgumentError, "Vote can't be zero" if score.zero?
|
133
|
+
score = score.positive? ? 1 : -1
|
134
|
+
res = API.new.fetch('post', 'vote', { id: @id, score: score }, 'POST')
|
135
|
+
if res[:reason] == 'access denied' && !res[:success]
|
136
|
+
raise Rubyhexagon::AccessDenied, res[:message]
|
137
|
+
end
|
138
|
+
nil
|
139
|
+
end
|
140
|
+
|
141
|
+
# @author Maxine Michalski
|
142
|
+
#
|
143
|
+
# Upvotes this post
|
144
|
+
def voteup!
|
145
|
+
vote(1)
|
146
|
+
end
|
147
|
+
|
148
|
+
# @author Maxine Michalski
|
149
|
+
#
|
150
|
+
# Downvotes this post
|
151
|
+
def votedown!
|
152
|
+
vote(-1)
|
153
|
+
end
|
154
|
+
|
155
|
+
# @author Maxine Michalski
|
156
|
+
#
|
157
|
+
# Favorite this post
|
158
|
+
def favorite!
|
159
|
+
res = API.new.fetch('favorite', 'create', { id: @id }, 'POST')
|
160
|
+
p res
|
161
|
+
if res[:reason] == 'access denied' && !res[:success]
|
162
|
+
raise Rubyhexagon::AccessDenied, res[:message]
|
163
|
+
elsif !res[:success] &&
|
164
|
+
res[:reason] == 'You\'ve already favorited this post'
|
165
|
+
raise Rubyhexagon::AlreadyFavorited
|
166
|
+
end
|
167
|
+
nil
|
168
|
+
end
|
169
|
+
|
170
|
+
# @author Maxine Michalski
|
171
|
+
#
|
172
|
+
# Unfavorite this post
|
173
|
+
def unfavorite!
|
174
|
+
res = API.new.fetch('favorite', 'destroy', { id: @id }, 'POST')
|
175
|
+
p res
|
176
|
+
if res[:reason] == 'access denied' && !res[:success]
|
177
|
+
raise Rubyhexagon::AccessDenied, res[:message]
|
178
|
+
end
|
179
|
+
nil
|
180
|
+
end
|
181
|
+
|
123
182
|
# @author Maxine Michalski
|
124
183
|
#
|
125
184
|
# Retrieve an Array of this post's tags
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyhexagon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maxine Michalski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|