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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 140a15c68030d059d158ea174fe82b2f426241491b1f0e5d8f348716ca2ef2b9
4
- data.tar.gz: 58e18c80dfa100f432bb77119c1cffe88b95ca04d3a43467af27d9e2240b160f
3
+ metadata.gz: 1900ac35dde2dd785dbebf585f72044933f2b39cfefae51f05ef91b81b16e07b
4
+ data.tar.gz: d0acac948011df5b2a48203b75e50c99115f071794443e61a2ae96e61fd2ab91
5
5
  SHA512:
6
- metadata.gz: e4caa755f4c8802d59d19ad981031297944c54fc3725afe7a967eeafea65439c7fa682721d78df516166f1349b7a1aa2243697e1de1eb4eee4458f3bcf646c8e
7
- data.tar.gz: 43c6d7a6e62afd236a5834e18912449dca8aead0f35e0dd3ee411ee9f3165611ff7c32f38d95ab761e2cc92490722190a2a45f216ab7d7688ae9171dc891083d
6
+ metadata.gz: 0c7c225ac5f218caca1b2749ff6b50ddb7663482accf3b7118f0e4ef5eed3d2e36293915752dcf5a362727dcd87ccb897db194adcf81262e0a80076cdb3264d0
7
+ data.tar.gz: 855efb249f5e819ef0d1bbcfa4f52d0b5be048e9ab43247c2125b61bc5a2b8e57deb83dec937bc8c0570cd8087778ef45041596411f9458527ff7aef4ea7c8da
@@ -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 'open-uri'
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 = 5
48
+ MINOR = 6
49
49
  PATCH = 0
50
50
  NAME = 'rubyhexagon'
51
51
  VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}"
@@ -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
- @base = 'https://e621.net'
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
- login = Login.instance
63
- if login.setup?
64
- query.store(:login, login.login)
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 = uri.open(@user_agent).read
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
@@ -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.is_a?(Integer) && id.positive?
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.5.0
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-12 00:00:00.000000000 Z
11
+ date: 2018-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json