rubyhexagon 0.4.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ # Copyright 2014-2018 Maxine Michalski <maxine@furfind.net>
2
+ #
3
+ # This file is part of rubyhexagon.
4
+ #
5
+ # rubyhexagon is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # rubyhexagon is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ module E621
18
+ # Class to hold type information.
19
+ #
20
+ # @api private
21
+ # @author Maxine Michalski
22
+ # @since 1.0.0
23
+ class Type
24
+ # @return [Integer] id of type
25
+ attr_reader :id
26
+
27
+ # @return [String] name of type
28
+ attr_reader :name
29
+
30
+ # @author Maxine Michalski
31
+ #
32
+ # Initializer for Type.
33
+ #
34
+ # @raise ArgumentError if type ID is not valid
35
+ #
36
+ # @param type [Hash] type data, fetched from e621
37
+ # @option type [Integer] :id Type ID (can be one of 0, 1, 3, 4 or 5
38
+ # @option type [
39
+ #
40
+ # @return the object
41
+ def initialize(id, locked)
42
+ unless [0, 1, 3, 4, 5].include?(id)
43
+ raise ArgumentError, "Unkown type id: #{id}"
44
+ end
45
+ @id = id.to_i
46
+ @name = [:general, :artist, nil, :copyright, :character, :species][@id]
47
+ @locked = locked ? true : false
48
+ end
49
+
50
+ # @author Maxine Michalski
51
+ #
52
+ # Comparison method for Types, to give a more meaningful comparison.
53
+ #
54
+ # @return [TrueClass, FalseClass]
55
+ def ==(other)
56
+ other.is_a?(Type) && @id == other.id && @name == other.name &&
57
+ @locked == other.locked?
58
+ end
59
+
60
+ # @author Maxine Michalski
61
+ #
62
+ # Check if this type is locked for the tag who has it assigned.
63
+ #
64
+ # @return [TrueClass] tag type is locked
65
+ # @return [FalseClass] tag type is not locked
66
+ def locked?
67
+ @locked
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,78 @@
1
+ # Copyright 2014-2018 Maxine Michalski <maxine@furfind.net>
2
+ #
3
+ # This file is part of rubyhexagon.
4
+ #
5
+ # rubyhexagon is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # rubyhexagon is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ require 'time'
18
+
19
+ module E621
20
+ # Class to hold user information for users on e621.
21
+ #
22
+ # @author Maxine Michalski
23
+ # @since 1.0.0
24
+ class User
25
+ # @return [Integer] id of user
26
+ attr_reader :id
27
+
28
+ # @return [String] name of user
29
+ attr_reader :name
30
+
31
+ # @return [Level] level of access, this user holds
32
+ attr_reader :level
33
+
34
+ # @return [Time] registration time of this user
35
+ attr_reader :created_at
36
+
37
+ # @author Maxine Michalski
38
+ #
39
+ # Initializer for User.
40
+ #
41
+ # @param user [Hash] user data, fetched from e621
42
+ #
43
+ # @return the object
44
+ def initialize(user)
45
+ @id = user[:id].to_i
46
+ @name = user[:name]
47
+ end
48
+
49
+ # @author Maxine Michalski
50
+ #
51
+ # Fill this object with user data.
52
+ def show!
53
+ user = API.new.fetch('user', 'show', id: @id)
54
+ @level = Level.new(user[:level].to_i)
55
+ @created_at = Time.parse(user[:created_at])
56
+ end
57
+
58
+ # @author Maxine Michalski
59
+ #
60
+ # Returns a filled User object
61
+ #
62
+ # @return [User] filled out user object
63
+ def show
64
+ user = User.new(id: @id, name: @name)
65
+ user.show!
66
+ user
67
+ end
68
+
69
+ # @author Maxine Michalski
70
+ #
71
+ # Comparison method for User objects
72
+ #
73
+ # @return [TrueClass, FalseClass]
74
+ def ==(other)
75
+ other.is_a?(User) && @id == other.id && @name == other.name
76
+ end
77
+ end
78
+ end
metadata CHANGED
@@ -1,34 +1,47 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyhexagon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - Maxine Red
7
+ - Maxine Michalski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-26 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Rubyhexagon provides Ruby bindings for the e621 [dot] net API.
11
+ date: 2018-03-25 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: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Rubyhexagon provides Ruby bindings for the e621 API.
14
28
  email: maxine@furfind.net
15
29
  executables: []
16
30
  extensions: []
17
31
  extra_rdoc_files: []
18
32
  files:
19
- - lib/api.rb
20
- - lib/container.rb
21
- - lib/pool.rb
22
- - lib/post.rb
23
33
  - lib/rubyhexagon.rb
24
- - lib/search.rb
25
- - lib/set.rb
26
- - lib/standard/error.rb
27
- - lib/standard/hash.rb
28
- - lib/standard/http.rb
29
- - lib/standard/int.rb
30
- - lib/standard/string.rb
31
- - lib/standard/time.rb
34
+ - lib/rubyhexagon/artist.rb
35
+ - lib/rubyhexagon/error.rb
36
+ - lib/rubyhexagon/helper/api.rb
37
+ - lib/rubyhexagon/image.rb
38
+ - lib/rubyhexagon/level.rb
39
+ - lib/rubyhexagon/post.rb
40
+ - lib/rubyhexagon/search/posts.rb
41
+ - lib/rubyhexagon/search/tags.rb
42
+ - lib/rubyhexagon/tag.rb
43
+ - lib/rubyhexagon/type.rb
44
+ - lib/rubyhexagon/user.rb
32
45
  homepage: https://github.com/maxine-red/rubyhexagon
33
46
  licenses:
34
47
  - GPL-3.0
@@ -39,9 +52,9 @@ require_paths:
39
52
  - lib
40
53
  required_ruby_version: !ruby/object:Gem::Requirement
41
54
  requirements:
42
- - - ">="
55
+ - - "~>"
43
56
  - !ruby/object:Gem::Version
44
- version: 1.9.2
57
+ version: '2.1'
45
58
  required_rubygems_version: !ruby/object:Gem::Requirement
46
59
  requirements:
47
60
  - - ">="
@@ -49,8 +62,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
62
  version: '0'
50
63
  requirements: []
51
64
  rubyforge_project:
52
- rubygems_version: 2.2.2
65
+ rubygems_version: 2.7.6
53
66
  signing_key:
54
67
  specification_version: 4
55
- summary: Rubyhexagon, Ruby bindings for e621[dot]net.
68
+ summary: Rubyhexagon, Ruby bindings for e621.
56
69
  test_files: []
data/lib/api.rb DELETED
@@ -1,79 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- require "standard/http"
21
-
22
- module E621
23
- class API
24
- def initialize(user=nil,pass=nil)
25
- @http = HTTP.new
26
- login(user,pass) if !defined? @@login
27
- end
28
- # A wrapper function to fit into the general theme better.
29
- def post_list(*args)
30
- post_index(*args)
31
- end
32
- # Send commands to API and parse all answers, even errors.
33
- # Also make it work with POST and GET requests.
34
- def method_missing(method,*args)
35
- request = request2body(args.first)
36
- method = method.to_s.split("_")
37
- m = case method[0]
38
- when "post" then
39
- case method[1]
40
- when "show" then :get
41
- else :post
42
- end
43
- else :post
44
- end
45
- @http.__send__(m,"/#{method.join("/")}.json",request)
46
- end
47
- private
48
- # This helper function provides the functionality of translating Ruby Hashes
49
- # into HTTP POST body strings.
50
- def request2body(request)
51
- query = @@login ? "#@@login&" : String.new
52
- request = request.map do |k,v|
53
- if v.is_a?(Hash) then
54
- raise Argumenterror, "Hashes as request values aren't allowed."
55
- elsif v.is_a?(Array) then
56
- v = v.join(",")
57
- end
58
- "#{k}=#{v}"
59
- end
60
- query += request.join("&")
61
- return query
62
- end
63
- # Perform an API and site login. This function needs to be called once
64
- # before any actual API call can be made.
65
- def login(user,pass)
66
- if user == nil && pass == nil then
67
- @@login = nil
68
- else
69
- request = {name:name,password:pass}
70
- body = @http.get("/user/login.json",request)
71
- if body.has_key?("success") && (!body["success"] || body["success"] == "failed") then
72
- raise AuthentificationError, "Username or password wrong!"
73
- else
74
- @@login = "login=#{body["name"]}&password_hash=#{body["password_hash"]}"
75
- end
76
- end
77
- end
78
- end
79
- end
@@ -1,45 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- module E621
21
- class Container
22
- def method_missing(method)
23
- if instance_variables.include?("@#{method}".to_sym) && method != :api then
24
- return instance_variable_get("@#{method}")
25
- else
26
- raise NoMethodError, "undefined method #{method} for #{self.class}"
27
- end
28
- end
29
- private
30
- def set_variables(container)
31
- container.each do |k,v|
32
- if (k == "created_at" || k == "updated_at") && v.is_a?(Hash) then
33
- v = Time.at(v["s"])
34
- elsif (k == "created_at" || k == "updated_at") && !v.is_a?(Hash) then
35
- v = Time.at(v)
36
- elsif k == "cached_tags" then
37
- k = "tags"
38
- elsif k == "posts" then
39
- v.map!{|post|Post.new(post)}
40
- end
41
- instance_variable_set("@#{k}",v)
42
- end
43
- end
44
- end
45
- end
@@ -1,71 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- require "digest/md5"
21
-
22
- module E621
23
- class Pool < Container
24
- def initialize(post)
25
- @api = API.new("pool")
26
- if post.is_a?(Hash) then
27
- set_variables(post)
28
- elsif post.is_a?(Fixnum) then
29
- set_variables(@api.get("show",{"id"=>post}))
30
- else
31
- raise ArgumentError, "Parameter must be Hash or Number."
32
- end
33
- end
34
-
35
- def keys
36
- return instance_variables.map{|i|i.to_s.sub("@","")}
37
- end
38
-
39
- def updated_at=(time)
40
- @updated_at = time
41
- end
42
-
43
- def name=(name)
44
- @name = name
45
- end
46
-
47
- def posts=(posts)
48
- @posts = posts
49
- end
50
-
51
- def to_json(n=nil)
52
- json_hash = Hash.new
53
- instance_variables.each do |i|
54
- v = instance_variable_get(i)
55
- v = v.is_a?(Time) ? v.to_i : v
56
- json_hash.store(i.to_s.sub("@",""),v)
57
- end
58
- return json_hash.to_json
59
- end
60
-
61
- def each_post
62
- (@post_count/24.0).ceil.times do |page|
63
- @posts.each do |post|
64
- yield post
65
- end
66
- post = api.get("show",{"id"=>@id,"page"=>page+2})
67
- set_variables(post)
68
- end
69
- end
70
- end
71
- end
@@ -1,50 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- require "digest/md5"
21
-
22
- module E621
23
- class Post < Container
24
- def initialize(post)
25
- @api = API.new
26
- if post.is_a?(Fixnum) || (post.is_a?(String) && post.match(/^[0-9,a-f]+$/i)) then
27
- post = if post.is_a?(String) && post.length == 32 then {md5:post}
28
- elsif post.is_a?(Fixnum) || (post.is_a?(String) && post.match(/^\d+$/)) then
29
- {id:post.to_i}
30
- else
31
- raise ArgumentError, "Neither an ID nor a MD5 Hash."
32
- end
33
- post = @api.post_show(post)
34
- elsif post.is_a?(Hash) then # Variable post is ready to use. Do nothing.
35
- else
36
- raise ArgumentError, "Post only accepts an id, a MD5 hashsum or a Ruby hash."
37
- end
38
- set_variables(post)
39
- end
40
- alias show initialize
41
- # Cast a vote! Any value above 0 votes up, every value below 0 votes down.
42
- # Zero itself causes an error.
43
- def vote(direction)
44
- if direction > 0 then @api.post_vote(1)
45
- elsif direction < 0 then @api.post_vote(-1)
46
- else raise ArgumentError, "Zero isn't allowed for voting results."
47
- end
48
- end
49
- end
50
- end