minecraft-jsonapi 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +9 -0
- data/lib/minecraft-jsonapi.rb +56 -0
- data/lib/minecraft-jsonapi/jsonapi.rb +51 -0
- data/lib/minecraft-jsonapi/namespace.rb +22 -0
- data/lib/minecraft-jsonapi/response.rb +32 -0
- data/lib/minecraft-jsonapi/version.rb +5 -0
- data/minecraft-jsonapi.gemspec +19 -0
- data/test/test_minecraft-jsonapi.rb +35 -0
- metadata +60 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Steven Hoffman
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Minecraft::JSONAPI
|
2
|
+
|
3
|
+
A simple Ruby gem to interact with a Minecraft server running [the JSONAPI mod](https://github.com/alecgorge/jsonapi)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'minecraft-jsonapi'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install minecraft-jsonapi
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'minecraft-jsonapi'
|
23
|
+
|
24
|
+
api = Minecraft::JSONAPI.new(host: "123.45.6.78", port: 20059, username: "admin", password: "12345", salt: "mmm")
|
25
|
+
|
26
|
+
api.sendMessage "Dinnerbone", "Hello, Dinnerbone!"
|
27
|
+
|
28
|
+
# Namespaced commands can be sent via blocks...
|
29
|
+
api.permissions do |perms|
|
30
|
+
perms.addPlayerToGroup "Fustrate", "Jolly Good Fellows"
|
31
|
+
groups = perms.getGroups
|
32
|
+
end
|
33
|
+
|
34
|
+
# or Ruby's built-in "send" method
|
35
|
+
files = api.send("fs.listDirectory", ".")
|
36
|
+
|
37
|
+
# which is equivalent to
|
38
|
+
# files = api.call(:"fs.listDirectory" => ".")[0]
|
39
|
+
# and
|
40
|
+
# files = api.fs { |fs| fs.listDirectory "." }
|
41
|
+
|
42
|
+
percent_disk_usage = api.system do |system|
|
43
|
+
system.getDiskUsage / system.getDiskSize
|
44
|
+
end
|
45
|
+
|
46
|
+
players_online = api.getPlayerCount
|
47
|
+
players_limit = api.getPlayerLimit
|
48
|
+
api.banWithReason "Nardageddon", "For excessive use of jungle planks."
|
49
|
+
api.reloadServer
|
50
|
+
```
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'minecraft-jsonapi/version'
|
2
|
+
require 'minecraft-jsonapi/namespace'
|
3
|
+
require 'minecraft-jsonapi/jsonapi'
|
4
|
+
require 'minecraft-jsonapi/response'
|
5
|
+
|
6
|
+
require 'json'
|
7
|
+
require 'digest/sha2'
|
8
|
+
require 'net/http'
|
9
|
+
require 'cgi'
|
10
|
+
|
11
|
+
module Minecraft
|
12
|
+
module JSONAPI
|
13
|
+
CALL_URL = 'http://%{host}:%{port}/api/call?method=%{method}&args=%{args}&key=%{key}'
|
14
|
+
CALL_MULTIPLE_URL = 'http://%{host}:%{port}/api/call-multiple?method=%{method}&args=%{args}&key=%{key}'
|
15
|
+
|
16
|
+
# Shortcut to Minecraft::JSONAPI::JSONAPI.new
|
17
|
+
def self.new(options = {})
|
18
|
+
Minecraft::JSONAPI::JSONAPI.new options
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.send_request(url)
|
22
|
+
get_raw_response(url).response
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.get_raw_response(url)
|
26
|
+
uri = URI.parse(url)
|
27
|
+
|
28
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
29
|
+
http.open_timeout = 10
|
30
|
+
http.read_timeout = 10
|
31
|
+
response = http.request(Net::HTTP::Get.new(uri.request_uri))
|
32
|
+
|
33
|
+
Minecraft::JSONAPI::Response.new(response.body)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Wrap arguments in an array if they're not already an array or hash
|
37
|
+
# Converts nil to [], "" to [""], "Username" to ["Username"], etc.
|
38
|
+
def self.map_to_array(arguments)
|
39
|
+
if arguments.nil?
|
40
|
+
[]
|
41
|
+
elsif arguments.is_a?(Array) || arguments.is_a?(Hash)
|
42
|
+
arguments
|
43
|
+
else
|
44
|
+
[arguments]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.url_encoded_json(data)
|
49
|
+
begin
|
50
|
+
CGI.escape JSON.generate data
|
51
|
+
rescue JSON::GeneratorError
|
52
|
+
CGI.escape data
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Minecraft
|
2
|
+
module JSONAPI
|
3
|
+
class JSONAPI
|
4
|
+
def initialize(options = {})
|
5
|
+
raise "A username must be provided." if options[:username].nil?
|
6
|
+
raise "A password must be provided." if options[:password].nil?
|
7
|
+
raise "Please use a salt." if options[:salt].nil?
|
8
|
+
|
9
|
+
@host = options[:host] || 'localhost'
|
10
|
+
@port = options[:port] || 20059
|
11
|
+
@username = options[:username]
|
12
|
+
@password = options[:password]
|
13
|
+
@salt = options[:salt]
|
14
|
+
end
|
15
|
+
|
16
|
+
def make_url(method, args)
|
17
|
+
if method.is_a? Array
|
18
|
+
Minecraft::JSONAPI::CALL_MULTIPLE_URL % { host: @host, port: @port, method: Minecraft::JSONAPI.url_encoded_json(method), args: Minecraft::JSONAPI.url_encoded_json(args), key: create_key(method) }
|
19
|
+
else
|
20
|
+
Minecraft::JSONAPI::CALL_URL % { host: @host, port: @port, method: Minecraft::JSONAPI.url_encoded_json(method), args: Minecraft::JSONAPI.url_encoded_json(args), key: create_key(method) }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def call(methods)
|
25
|
+
method_names = methods.keys.map(&:to_s)
|
26
|
+
method_arguments = methods.values.map { |args| Minecraft::JSONAPI.map_to_array args }
|
27
|
+
|
28
|
+
url = make_url(method_names, method_arguments)
|
29
|
+
Minecraft::JSONAPI.send_request(url)
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_missing(method, *args, &block)
|
33
|
+
if block_given?
|
34
|
+
block.call Minecraft::JSONAPI::Namespace.new(self, method.to_s)
|
35
|
+
else
|
36
|
+
url = make_url(method.to_s, args)
|
37
|
+
Minecraft::JSONAPI.send_request(url)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def create_key(method)
|
42
|
+
method = JSON.generate(method) if method.is_a? Array
|
43
|
+
|
44
|
+
Digest::SHA2.new.update([@username, method, @password, @salt].join).to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
def namespace
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Minecraft
|
2
|
+
module JSONAPI
|
3
|
+
class Namespace
|
4
|
+
def initialize(parent, namespace)
|
5
|
+
@parent = parent
|
6
|
+
@namespace = namespace
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(method, *args)
|
10
|
+
method = [@namespace, method.to_s].join(".")
|
11
|
+
url = @parent.make_url(method, args)
|
12
|
+
|
13
|
+
Minecraft::JSONAPI.send_request(url)
|
14
|
+
end
|
15
|
+
|
16
|
+
# This will bubble upwards until we hit the actual JSONAPI class
|
17
|
+
def make_url(*args)
|
18
|
+
@parent.make_url(*args)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Minecraft
|
2
|
+
module JSONAPI
|
3
|
+
class Response
|
4
|
+
def initialize(data)
|
5
|
+
data = JSON.parse data
|
6
|
+
|
7
|
+
# Was this a multi-call?
|
8
|
+
if data["source"].is_a? Array
|
9
|
+
@result = data["success"].map { |r| r["result"] }
|
10
|
+
@source = data["success"].map { |r| r["source"] }
|
11
|
+
@response = data["success"].map { |r| r["success"] }
|
12
|
+
else
|
13
|
+
@result = data["result"]
|
14
|
+
@source = data["source"]
|
15
|
+
@response = data["success"]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def result
|
20
|
+
@result
|
21
|
+
end
|
22
|
+
|
23
|
+
def source
|
24
|
+
@source
|
25
|
+
end
|
26
|
+
|
27
|
+
def response
|
28
|
+
@response
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'minecraft-jsonapi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "minecraft-jsonapi"
|
8
|
+
gem.version = Minecraft::JSONAPI::VERSION
|
9
|
+
gem.authors = ["Steven Hoffman"]
|
10
|
+
gem.email = ["git@fustrate.com"]
|
11
|
+
gem.description = "A simple Ruby gem to interact with a Minecraft server running the JSONAPI mod"
|
12
|
+
gem.summary = "A simple Ruby gem to interact with a Minecraft server running the JSONAPI mod"
|
13
|
+
gem.homepage = "https://github.com/Fustrate/minecraft-jsonapi"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'minecraft-jsonapi'
|
3
|
+
|
4
|
+
class MinecraftJSONAPITest < Test::Unit::TestCase
|
5
|
+
def api
|
6
|
+
@api ||= Minecraft::JSONAPI.new(username: "admin", password: "12345", salt: "mmm")
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_create_new_shortcut
|
10
|
+
assert_equal Minecraft::JSONAPI::JSONAPI, api.class
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_map_to_array
|
14
|
+
assert_equal [], Minecraft::JSONAPI.map_to_array(nil)
|
15
|
+
assert_equal [""], Minecraft::JSONAPI.map_to_array("")
|
16
|
+
assert_equal ["Fustrate"], Minecraft::JSONAPI.map_to_array("Fustrate")
|
17
|
+
|
18
|
+
assert_equal [12345], Minecraft::JSONAPI.map_to_array(12345)
|
19
|
+
assert_equal [1, 2, 3], Minecraft::JSONAPI.map_to_array([1, 2, 3])
|
20
|
+
assert_equal [1, "2", 3.0], Minecraft::JSONAPI.map_to_array([1, "2", 3.0])
|
21
|
+
|
22
|
+
hash = { :one => "two", :three => "four" }
|
23
|
+
|
24
|
+
assert_equal hash, Minecraft::JSONAPI.map_to_array(hash)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_url_encoded_json
|
28
|
+
assert_equal "%5B%22test%22%5D", Minecraft::JSONAPI.url_encoded_json(["test"])
|
29
|
+
assert_equal "%5B%22test%22%2C5%5D", Minecraft::JSONAPI.url_encoded_json(["test", 5])
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_connection
|
33
|
+
assert_equal 0, api.getPlayerCount
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minecraft-jsonapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.7
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Steven Hoffman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-22 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simple Ruby gem to interact with a Minecraft server running the JSONAPI
|
15
|
+
mod
|
16
|
+
email:
|
17
|
+
- git@fustrate.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/minecraft-jsonapi.rb
|
28
|
+
- lib/minecraft-jsonapi/jsonapi.rb
|
29
|
+
- lib/minecraft-jsonapi/namespace.rb
|
30
|
+
- lib/minecraft-jsonapi/response.rb
|
31
|
+
- lib/minecraft-jsonapi/version.rb
|
32
|
+
- minecraft-jsonapi.gemspec
|
33
|
+
- test/test_minecraft-jsonapi.rb
|
34
|
+
homepage: https://github.com/Fustrate/minecraft-jsonapi
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.24
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: A simple Ruby gem to interact with a Minecraft server running the JSONAPI
|
58
|
+
mod
|
59
|
+
test_files:
|
60
|
+
- test/test_minecraft-jsonapi.rb
|