mojang 1.0.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +25 -0
  3. data/lib/errors.rb +29 -0
  4. data/lib/mojang.rb +87 -0
  5. metadata +47 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2a08c579322cc5c37c14c5e5b0835ac60138c15b
4
+ data.tar.gz: d6b5e673a294456325780acb81a4e522e323e67f
5
+ SHA512:
6
+ metadata.gz: c8550b755c11fe989842a9c3b88c7459581e59b2b15dfd405489a7116da91d37c5b9ceaea6c46ff3768263d121dac7885ea9e2882700eeec2b8709d4bc8c140b
7
+ data.tar.gz: 5fdfc222c79dd752090d368d4038c000371a13e663b4eb0c52eee534da8f584aa95524c957c75214dc39e07daf012787062356bb1f4b44175cd4c5dd867b1232
@@ -0,0 +1,25 @@
1
+ # The MIT License (MIT)
2
+
3
+ Copyright © 2016 Eli Foster
4
+
5
+ Permission is hereby granted, free of charge, to any person
6
+ obtaining a copy of this software and associated documentation
7
+ files (the “Software”), to deal in the Software without
8
+ restriction, including without limitation the rights to use,
9
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the
11
+ Software is furnished to do so, subject to the following
12
+ conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ OTHER DEALINGS IN THE SOFTWARE.
25
+
@@ -0,0 +1,29 @@
1
+ module Mojang
2
+ module Errors
3
+ class NoSuchUserError < StandardError
4
+ attr_reader :user
5
+
6
+ def initialize(user)
7
+ @user = user
8
+ end
9
+
10
+ def message
11
+ "No such user '#{@user}'."
12
+ end
13
+ end
14
+
15
+ class MojangError < StandardError
16
+ attr_reader :error
17
+ attr_reader :msg
18
+
19
+ def initialize(error, msg)
20
+ @error = error
21
+ @msg = msg
22
+ end
23
+
24
+ def message
25
+ "#{@msg} (#{@error}"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,87 @@
1
+ require 'curb'
2
+ require 'oj'
3
+ require_relative 'errors'
4
+
5
+ # Wrapper module around the Mojang and Minecraft web APIs.
6
+ module Mojang
7
+ module_function
8
+
9
+ # Gets the status for the various Mojang and Minecraft servers and web services.
10
+ # @return [Hash<String, String>] A hash containing keys of the sites, and values of the statuses (green, yellow, red).
11
+ def status
12
+ response = Curl.get('https://status.mojang.com/check'.freeze).body_str
13
+ json = Oj.load(response)
14
+ ret = {}
15
+ # Reformatting the returned data because it is super annoying to work with.
16
+ json.each do |hash|
17
+ hash.each do |site, status|
18
+ ret[site] = status
19
+ end
20
+ end
21
+
22
+ ret
23
+ end
24
+
25
+ # Gets the User ID (UUID) for the given username at the given time.
26
+ # @param username [String] The username.
27
+ # @param date [Date] The date to get the ID at.
28
+ # @return [String] The username's user ID.
29
+ def userid(username, date = nil)
30
+ self.profile(username, date, 'id')
31
+ end
32
+
33
+ # Gets the username for the given UUID at the given time.
34
+ # @param uuid [String] The user's UUID (see #{userid})
35
+ # @param date [Date] The date to get the name at.
36
+ # @return [String] The user's username at the given time.
37
+ def username(uuid, date = nil)
38
+ self.profile(uuid, date, 'name')
39
+ end
40
+
41
+ # Gets whether the given username has paid for Minecraft.
42
+ # @param username [String] The username to check.
43
+ # @return [Boolean] Whether they have paid or not.
44
+ def has_paid?(username)
45
+ params = { user: username }
46
+ response = Curl.get('https://minecraft.net/haspaid.jsp', params).body_str
47
+
48
+ response == 'true'
49
+ end
50
+
51
+ # Gets a user's name history from their UUID.
52
+ # @param uuid [String] The user's ID (see #{userid}).
53
+ # @return [Hash<Symbol/Time, String>] A hash of all the names. Key is either :original, or the Time object of when
54
+ # the name was changed. Value is always the name at that point in time.
55
+ def name_history(uuid)
56
+ response = Curl.get("https://api.mojang.com/user/profiles/#{uuid}/names").body_str
57
+ json = Oj.load(response)
58
+ ret = {}
59
+ json.each do |hash|
60
+ if hash.key?('changedToAt')
61
+ ret[Time.at(hash['changedToAt'] / 1000)] = hash['name']
62
+ else
63
+ ret[:original] = hash['name']
64
+ end
65
+ end
66
+
67
+ ret
68
+ end
69
+
70
+ private
71
+
72
+ # Gets the profile data.
73
+ # @param query [String] Either a username or an ID; they are handled in the same way.
74
+ # @param date [Date] The date to get at.
75
+ # @param return_val [String] The key in the returned hash to return.
76
+ # @return [String] The returned value for the return_val key.
77
+ def self.profile(query, date, return_val)
78
+ params = { at: date.to_i }
79
+ response = Curl.get("https://api.mojang.com/users/profiles/minecraft/#{query}", params).body_str
80
+ fail NoSuchUserError.new(query) if response.empty?
81
+ json = Oj.load(response)
82
+ if json.key?('error')
83
+ fail MojangError.new(json['error'], json['errorMessage'])
84
+ end
85
+ return json[return_val]
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mojang
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eli Foster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby library accessing the Mojang and Minecraft web APIs
14
+ email: elifosterwy@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE.md
20
+ - lib/errors.rb
21
+ - lib/mojang.rb
22
+ homepage: https://github.com/elifoster/mojang-rb
23
+ licenses:
24
+ - MIT
25
+ metadata:
26
+ issue_tracker: https://github.com/elifoster/mojang-rb/issues
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.5.1
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Mojang and Minecraft web APIs in Ruby
47
+ test_files: []