minecraft_jsonapiv2 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e53ffa2e43cefecd7267720543ba1d55acc93feb
4
+ data.tar.gz: a65f73e05a2e38053f91b0357b54a0c8d526785e
5
+ SHA512:
6
+ metadata.gz: d0d6b8fdc72e407db27cf7c0f98a290d55f79c77c1451451fe9fa12887a769ff8a536c01ebce81eba10d8735fc803fc9c8930dadbad4325d7300e886c047767e
7
+ data.tar.gz: 090007701dc7fb3cf599436b98eaf9bb71022f0cf3fc16cca2db703b09a59575cdcc7c06c33a8676841774fc9b3370ea9f75bf3ec108ee6a4860d8e95d9164f4
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in minecraft_jsonapi.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Patrick Neff
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,62 @@
1
+ # Minecraft JSONAPIv2
2
+
3
+ A ruby API wrapper for the [JSONAPI Plugin](http://mcjsonapi.com/) for Minecraft
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'minecraft_jsonapiv2'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install minecraft_jsonapiv2
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'minecraft_jsonapiv2'
23
+
24
+ api = Minecraft::JSONAPIv2.new(host: '127.0.0.1', port: 20059, user: 'admin', password: 'changme', salt: 'mmm')
25
+
26
+ #Call a non namespaced method
27
+ api.server
28
+
29
+ #Replace dots with underscores in namespaced method names
30
+ api.server_version
31
+
32
+ #If the method name itself contains underscores call it as a block
33
+ api.worlds_world do |world|
34
+ world.save_off
35
+ end
36
+
37
+ #Call a method with arguments
38
+ chat_broadcast('Hello World')
39
+
40
+ #Call a method with arguments in a block
41
+ api.worlds_world do |world|
42
+ world.set_time('world', 6000)
43
+ end
44
+
45
+ #Or call a method manally
46
+ options = {
47
+ name: 'worlds.world.set_time'
48
+ args: ['world', 6000]
49
+ }
50
+ api.call(options)
51
+
52
+ ```
53
+
54
+ See [JSONAPI Method Documentation](http://mcjsonapi.com/apidocs/) for more information
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it ( https://github.com/masterodie/minecraft_jsonapiv2/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubygems'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ class Hash
2
+ # Converts all of the keys to strings, optionally formatting key name
3
+ def rubyify_keys!
4
+ keys.each{|k|
5
+ v = delete(k)
6
+ new_key = k.to_s.underscore
7
+ self[new_key] = v
8
+ v.rubyify_keys! if v.is_a?(Hash)
9
+ v.each{|p| p.rubyify_keys! if p.is_a?(Hash)} if v.is_a?(Array)
10
+ }
11
+ self
12
+ end
13
+ end
14
+
@@ -0,0 +1,10 @@
1
+ class String
2
+ def underscore
3
+ self.gsub(/::/, '/').
4
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
6
+ tr("-", "_").
7
+ downcase
8
+ end
9
+ end
10
+
@@ -0,0 +1,42 @@
1
+ module Minecraft
2
+ module JSONAPIv2
3
+ class API
4
+ attr_reader :user
5
+
6
+ def initialize(options={})
7
+ @user= options[:user]
8
+ @conn = Minecraft::JSONAPIv2::Request.new(options)
9
+ end
10
+
11
+ def call(method)
12
+ response = Minecraft::JSONAPIv2::Response.new( @conn.make_request(method) ).response[method[:name].gsub(/\./, '_')]
13
+ response = Mash.new(response) if response.is_a? Hash or response.is_a? Array
14
+ begin
15
+ unless response.nil?
16
+ response
17
+ else
18
+ raise NoResponseError
19
+ end
20
+ end
21
+ end
22
+
23
+ def method_missing(name, *args, &block)
24
+ if block_given?
25
+ block.call Minecraft::JSONAPIv2::Namespace.new(self, name.to_s)
26
+ else
27
+ response = Minecraft::JSONAPIv2::Response.new( @conn.make_request(name: name.to_s.gsub(/_/, '.'), args: args) ).response[name.to_s]
28
+
29
+ super if (response.nil?)
30
+ response = Mash.new(response) if response.is_a? Hash or response.is_a? Array
31
+ response
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ # Custom Error classes
39
+ class NoResponseError < StandardError
40
+ end
41
+
42
+
@@ -0,0 +1,21 @@
1
+ module Minecraft
2
+ module JSONAPIv2
3
+ class Authorization
4
+ attr_reader :username, :key
5
+
6
+ def initialize(options={})
7
+ raise "No username given" if options[:user].nil?
8
+ raise "No password given" if options[:password].nil?
9
+ raise "No salt given" if options[:salt].nil?
10
+ @user = options[:user]
11
+ @pass = options[:password]
12
+ @salt = options[:salt]
13
+ @key = nil
14
+ end
15
+
16
+ def key_for(method)
17
+ @key = Digest::SHA256.new.update([@user, method, @pass, @salt].join).to_s
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Minecraft
2
+ module JSONAPIv2
3
+ class Namespace
4
+ def initialize(parent, namespace, options={})
5
+ @parent = parent
6
+ @namespace = namespace
7
+ end
8
+
9
+ def method_missing(method, *args)
10
+ method = [@namespace.gsub(/_/,'.'), method.to_s].join('.')
11
+ opts = {name: method, args: args}
12
+
13
+ @parent.call(opts)
14
+ end
15
+
16
+ def call(method)
17
+ @parent.call(methodd)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,68 @@
1
+ module Minecraft
2
+ module JSONAPIv2
3
+ class Request
4
+ def initialize(options={})
5
+ @host = options[:host].nil? ? '127.0.0.1' : options[:host]
6
+ @port = options[:port].nil? ? 20059 : options[:port]
7
+ @user = options[:user]
8
+ @auth = Minecraft::JSONAPIv2::Authorization.new(options)
9
+ @json = []
10
+ @req = []
11
+ end
12
+
13
+ def make_request(options)
14
+ url = Minecraft::JSONAPIv2::BASE_URI % {host: @host, port: @port}
15
+ request = generate_array_for_json( generate_request(options) ).to_json
16
+ response = HTTParty.post(url, body: request, headers: {'Content-Type' => 'application/json'} )
17
+ @json = []
18
+ @req = []
19
+ response.body
20
+ end
21
+
22
+ def generate_request(method)
23
+ if method.is_a? Array
24
+ method.each do |m|
25
+ generate_request m
26
+ end
27
+ else
28
+ @req.push(
29
+ user: @user,
30
+ method: method,
31
+ key: @auth.key_for(method[:name])
32
+ )
33
+ end
34
+ @req
35
+ end
36
+
37
+ def map_to_array(arguments)
38
+ if arguments.nil?
39
+ []
40
+ elsif arguments.is_a?(Array) || arguments.is_a?(Hash)
41
+ arguments
42
+ else
43
+ [arguments]
44
+ end
45
+ end
46
+
47
+ def generate_array_for_json(options={})
48
+ if options.is_a? Array
49
+ options.each do |o|
50
+ generate_array_for_json o
51
+ end
52
+ else
53
+ raise 'No username given' if options[:user].nil?
54
+ raise 'No key given' if options[:key].nil?
55
+ raise 'No method given' if options[:method].nil?
56
+ raise 'No method name given' if options[:method][:name].nil?
57
+ @json.push({
58
+ name: options[:method][:name],
59
+ username: options[:user],
60
+ arguments: map_to_array(options[:method][:args]),
61
+ key: options[:key],
62
+ })
63
+ end
64
+ @json
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,20 @@
1
+ module Minecraft
2
+ module JSONAPIv2
3
+ class Response
4
+ attr_reader :response, :raw_response
5
+ def initialize(data)
6
+ @response = {}
7
+ data = JSON.parse(data)
8
+ @raw_response = data
9
+ if data.is_a? Array
10
+ data.each do |d|
11
+ @response[d['source'].gsub(/\./, '_')] = d['success']
12
+ end
13
+ else
14
+ @response[data['source'].gsub(/\./, '_')] = data['success']
15
+ end
16
+ @response.rubyify_keys!
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module Minecraft
2
+ module JSONAPIv2
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ require 'json'
2
+ require 'digest/sha2'
3
+ require 'httparty'
4
+ require 'yaml'
5
+ require 'mash'
6
+
7
+
8
+ module Minecraft
9
+ module JSONAPIv2
10
+ BASE_URI = 'http://%{host}:%{port}/api/2/call'
11
+
12
+ Dir[File.join(File.dirname(__FILE__), '**', '*.rb')].each { |f| require f }
13
+
14
+ def self.new(options={})
15
+ Minecraft::JSONAPIv2::API.new(options)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'minecraft_jsonapiv2/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "minecraft_jsonapiv2"
8
+ spec.version = Minecraft::JSONAPIv2::VERSION
9
+ spec.authors = ["Patrick Neff"]
10
+ spec.email = ["odie86@gmail.com"]
11
+ spec.summary = %q{Minecraft JSONAPI Wrapper}
12
+ spec.description = %q{Simple Minecraft JSONAPI Wrapper}
13
+ spec.homepage = "https://github.com/masterodie/minecraft_jsonapiv2"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", ">= 10.0.0"
23
+
24
+ spec.add_dependency "json", "~> 1.8.0"
25
+ spec.add_dependency "httparty", "~> 0.13.0"
26
+ spec.add_dependency "mash", "~> 0.1.0"
27
+ end
@@ -0,0 +1,51 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe Minecraft::JSONAPIv2::API do
4
+ before do
5
+ @api = Minecraft::JSONAPIv2::API.new(host: '10.10.0.10', port: 20059, user: 'admin', password: 'changeme', salt: 'mmm')
6
+ end
7
+
8
+ describe 'method' do
9
+ it 'must raise method not found' do
10
+ lambda { @api.foobar }.must_raise NoMethodError
11
+ end
12
+
13
+ it 'must be String if response is String' do
14
+ @api.server_version.must_be_instance_of String
15
+ end
16
+
17
+ it 'must be Mash when API response is Hash' do
18
+ @api.server.must_be_instance_of Mash
19
+ end
20
+ end
21
+
22
+ describe 'methhod with arguments' do
23
+ it 'must equal called arguments' do
24
+ @api.players_name('masterodie').name.must_equal 'masterodie'
25
+ end
26
+
27
+ it 'must be Mash response is Hash' do
28
+ @api.server.must_be_instance_of Mash
29
+ end
30
+ end
31
+
32
+ describe 'block' do
33
+ it 'must respond' do
34
+ @api.server {|s| s.version }.must_be_instance_of String
35
+ end
36
+ end
37
+
38
+ describe 'manual call' do
39
+ it 'must raise no response' do
40
+ lambda { @api.call(name: 'foobar') }.must_raise NoResponseError
41
+ end
42
+
43
+ it 'must be String response is string' do
44
+ @api.call(name: 'server.version').must_be_instance_of String
45
+ end
46
+
47
+ it 'must be Mash when response is Hash' do
48
+ @api.call(name: 'server').must_be_instance_of Mash
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ require_relative '../../spec_helper.rb'
2
+
3
+ describe Minecraft::JSONAPIv2::Authorization do
4
+ describe 'key_for' do
5
+ before do
6
+ @auth = Minecraft::JSONAPIv2::Authorization.new(user: 'admin', password: 'changeme', salt: 'mmm')
7
+ end
8
+
9
+ it 'must be String' do
10
+ @auth.key_for(name: 'server.version').must_be_instance_of String
11
+ end
12
+
13
+ it 'must have the right length' do
14
+ @auth.key_for(name: 'server.version').length.must_equal 64
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../spec_helper.rb'
2
+
3
+ describe Minecraft::JSONAPIv2 do
4
+ describe 'shortcut' do
5
+ before do
6
+ @api = Minecraft::JSONAPIv2.new(host: '10.10.0.10', port: 20059, user: 'admin', password: 'changeme', salt: 'mmm')
7
+ end
8
+
9
+ it 'should work' do
10
+ @api.must_be_instance_of Minecraft::JSONAPIv2::API
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'minecraft_jsonapiv2'
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/spec'
6
+
7
+ if __FILE__ == $0
8
+ $LOAD_PATH.unshift('lib', 'spec')
9
+ Dir.glob('./spec/**/*_spec.rb') { |f| require f }
10
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minecraft_jsonapiv2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Neff
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 10.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 10.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.8.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.13.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.13.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: mash
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.1.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.1.0
83
+ description: Simple Minecraft JSONAPI Wrapper
84
+ email:
85
+ - odie86@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/core_ext/hash.rb
96
+ - lib/core_ext/string.rb
97
+ - lib/minecraft_jsonapiv2.rb
98
+ - lib/minecraft_jsonapiv2/api.rb
99
+ - lib/minecraft_jsonapiv2/authorization.rb
100
+ - lib/minecraft_jsonapiv2/namespace.rb
101
+ - lib/minecraft_jsonapiv2/request.rb
102
+ - lib/minecraft_jsonapiv2/response.rb
103
+ - lib/minecraft_jsonapiv2/version.rb
104
+ - minecraft_jsonapiv2.gemspec
105
+ - spec/lib/minecraft_jsonapiv2/api_spec.rb
106
+ - spec/lib/minecraft_jsonapiv2/authorization_spec.rb
107
+ - spec/lib/minecraft_jsonapiv2_spec.rb
108
+ - spec/spec_helper.rb
109
+ homepage: https://github.com/masterodie/minecraft_jsonapiv2
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.2.2
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Minecraft JSONAPI Wrapper
133
+ test_files:
134
+ - spec/lib/minecraft_jsonapiv2/api_spec.rb
135
+ - spec/lib/minecraft_jsonapiv2/authorization_spec.rb
136
+ - spec/lib/minecraft_jsonapiv2_spec.rb
137
+ - spec/spec_helper.rb