minecraft_jsonapiv2 0.0.2 → 0.0.3

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
  SHA1:
3
- metadata.gz: 1b3abfecbb87f73064b8b8f616559b5c787f3168
4
- data.tar.gz: 60bdcaab1e6f8433adfd37deaaa18804f555644d
3
+ metadata.gz: 98c43c779d2f78b2c8fbabe9324f3338a9458c52
4
+ data.tar.gz: a685946592264bc6e795b915e0398968ba20b916
5
5
  SHA512:
6
- metadata.gz: 4131dd2f1280bae09edc3de7e90443221b6639b9138de614656473b441c2d6c92fc51c373402ef50d41ce2e2ae41dce3fd5637e90b366a70bf9db3d049b50660
7
- data.tar.gz: ee69e1c8b426c4c09f3c9d89c74eb6eac1883ab62b2c5133c8b31d84acb023883b01fea11f0147e4c1b9bca9a59da054a50294927afcb448c5134a68b260e3b7
6
+ metadata.gz: bff260125ffd8cb014560cde8cc810c2eeb134756e458cd9194ac362ab53fff16e0456d8eb754555a450a5ee8bb2521b858d0fdb1e41df3c8b4da8945eb7dce4
7
+ data.tar.gz: 2519bd7d4cac0664cfa4c7d2ca7b5cedd290bda99264273b6c7512b4d44c68bf208b97541508c6637e494d6f823120b38b3e61a5d58bd520ebf9b1d078b94cde
data/lib/core_ext/hash.rb CHANGED
@@ -1,14 +1,14 @@
1
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
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
13
  end
14
14
 
@@ -1,10 +1,10 @@
1
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
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
9
  end
10
10
 
@@ -1,48 +1,40 @@
1
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
- if method.is_a? Array
13
- method.each { |m| call m }
14
- else
15
- if method.is_a? Hash
16
- response = Minecraft::JSONAPIv2::Response.new( @conn.make_request(method) ).response[method[:name].gsub(/\./, '_')]
17
- response = Mash.new(response) if response.is_a? Hash
18
- end
19
- end
20
- begin
21
- unless response.nil?
22
- response
23
- else
24
- raise NoResponseError
25
- end
26
- end
27
- end
28
-
29
- def method_missing(name, *args, &block)
30
- if block_given?
31
- block.call Minecraft::JSONAPIv2::Namespace.new(self, name.to_s)
32
- else
33
- response = Minecraft::JSONAPIv2::Response.new( @conn.make_request(name: name.to_s.gsub(/_/, '.'), args: args) ).response[name.to_s]
34
- response = Mash.new(response) if response.is_a? Hash
35
- begin
36
- unless response.nil?
37
- response
38
- else
39
- raise NoResponseError
40
- end
41
- end
42
- end
43
- end
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
+ request = Minecraft::JSONAPIv2::Response.new( @conn.make_request(method) )
13
+ get_response_for request
14
+ end
15
+
16
+ def get_response_for(request)
17
+ response = request.response
18
+ begin
19
+ unless response.nil?
20
+ response
21
+ else
22
+ raise NoResponseError
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ def method_missing(name, *args, &block)
29
+ if block_given?
30
+ block.call Minecraft::JSONAPIv2::Namespace.new(self, name.to_s)
31
+ else
32
+ request = Minecraft::JSONAPIv2::Response.new( @conn.make_request(name: name.to_s.gsub(/_/, '.'), args: args) )
33
+ get_response_for request
44
34
  end
35
+ end
45
36
  end
37
+ end
46
38
  end
47
39
 
48
40
  # Custom Error classes
@@ -1,21 +1,21 @@
1
1
  module Minecraft
2
- module JSONAPIv2
3
- class Authorization
4
- attr_reader :username, :key
2
+ module JSONAPIv2
3
+ class Authorization
4
+ attr_reader :username, :key
5
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
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
15
 
16
- def key_for(method)
17
- @key = Digest::SHA256.new.update([@user, method, @pass, @salt].join).to_s
18
- end
19
- end
16
+ def key_for(method)
17
+ @key = Digest::SHA256.new.update([@user, method, @pass, @salt].join).to_s
18
+ end
20
19
  end
20
+ end
21
21
  end
@@ -1,21 +1,21 @@
1
1
  module Minecraft
2
- module JSONAPIv2
3
- class Namespace
4
- def initialize(parent, namespace, options={})
5
- @parent = parent
6
- @namespace = namespace
7
- end
2
+ module JSONAPIv2
3
+ class Namespace
4
+ def initialize(parent, namespace, options={})
5
+ @parent = parent
6
+ @namespace = namespace
7
+ end
8
8
 
9
- def method_missing(method, *args)
10
- method = [@namespace.gsub(/_/,'.'), method.to_s].join('.')
11
- opts = {name: method, args: args}
9
+ def method_missing(method, *args)
10
+ method = [@namespace.gsub(/_/,'.'), method.to_s].join('.')
11
+ opts = {name: method, args: args}
12
12
 
13
- @parent.call(opts)
14
- end
13
+ @parent.call(opts)
14
+ end
15
15
 
16
- def call(method)
17
- @parent.call(methodd)
18
- end
19
- end
16
+ def call(method)
17
+ @parent.call(methodd)
18
+ end
20
19
  end
20
+ end
21
21
  end
@@ -1,68 +1,68 @@
1
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
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
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
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
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
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
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
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
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
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
+ })
66
63
  end
64
+ @json
65
+ end
67
66
  end
67
+ end
68
68
  end
@@ -1,20 +1,25 @@
1
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
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.count == 1
10
+ data = data[0]
18
11
  end
12
+ if data.is_a? Array
13
+ data.each do |d|
14
+ @response[d['source'].gsub(/\./, '_')] = d['success']
15
+ end
16
+ @response = Mash.new @response.rubyify_keys!
17
+ else
18
+ @response = data['success']
19
+ @response = Mash.new @response if @response.is_a? Hash
20
+ end
21
+ @response
22
+ end
19
23
  end
24
+ end
20
25
  end
@@ -1,5 +1,5 @@
1
1
  module Minecraft
2
- module JSONAPIv2
3
- VERSION = "0.0.2"
4
- end
2
+ module JSONAPIv2
3
+ VERSION = "0.0.3"
4
+ end
5
5
  end
@@ -6,13 +6,13 @@ require 'mash'
6
6
 
7
7
 
8
8
  module Minecraft
9
- module JSONAPIv2
10
- BASE_URI = 'http://%{host}:%{port}/api/2/call'
9
+ module JSONAPIv2
10
+ BASE_URI = 'http://%{host}:%{port}/api/2/call'
11
11
 
12
- Dir[File.join(File.dirname(__FILE__), '**', '*.rb')].each { |f| require f }
12
+ Dir[File.join(File.dirname(__FILE__), '**', '*.rb')].each { |f| require f }
13
13
 
14
- def self.new(options={})
15
- Minecraft::JSONAPIv2::API.new(options)
16
- end
14
+ def self.new(options={})
15
+ Minecraft::JSONAPIv2::API.new(options)
17
16
  end
17
+ end
18
18
  end
@@ -1,51 +1,51 @@
1
1
  require_relative '../../spec_helper'
2
2
 
3
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
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
7
 
8
- describe 'method' do
9
- it 'must raise method not found' do
10
- lambda { @api.foobar }.must_raise NoMethodError
11
- end
8
+ describe 'method' do
9
+ it 'must raise method not found' do
10
+ lambda { @api.foobar }.must_raise NoMethodError
11
+ end
12
12
 
13
- it 'must be String if response is String' do
14
- @api.server_version.must_be_instance_of String
15
- end
13
+ it 'must be String if response is String' do
14
+ @api.server_version.must_be_instance_of String
15
+ end
16
16
 
17
- it 'must be Mash when API response is Hash' do
18
- @api.server.must_be_instance_of Mash
19
- end
17
+ it 'must be Mash when API response is Hash' do
18
+ @api.server.must_be_instance_of Mash
20
19
  end
20
+ end
21
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
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
26
 
27
- it 'must be Mash response is Hash' do
28
- @api.server.must_be_instance_of Mash
29
- end
27
+ it 'must be Mash response is Hash' do
28
+ @api.server.must_be_instance_of Mash
30
29
  end
30
+ end
31
31
 
32
- describe 'block' do
33
- it 'must respond' do
34
- @api.server {|s| s.version }.must_be_instance_of String
35
- end
32
+ describe 'block' do
33
+ it 'must respond' do
34
+ @api.server {|s| s.version }.must_be_instance_of String
36
35
  end
36
+ end
37
37
 
38
- describe 'manual call' do
39
- it 'must raise no response' do
40
- lambda { @api.call(name: 'foobar') }.must_raise NoResponseError
41
- end
38
+ describe 'manual call' do
39
+ it 'must raise no response' do
40
+ lambda { @api.call(name: 'foobar') }.must_raise NoResponseError
41
+ end
42
42
 
43
- it 'must be String response is string' do
44
- @api.call(name: 'server.version').must_be_instance_of String
45
- end
43
+ it 'must be String response is string' do
44
+ @api.call(name: 'server.version').must_be_instance_of String
45
+ end
46
46
 
47
- it 'must be Mash when response is Hash' do
48
- @api.call(name: 'server').must_be_instance_of Mash
49
- end
47
+ it 'must be Mash when response is Hash' do
48
+ @api.call(name: 'server').must_be_instance_of Mash
50
49
  end
50
+ end
51
51
  end
@@ -1,17 +1,17 @@
1
1
  require_relative '../../spec_helper.rb'
2
2
 
3
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
4
+ describe 'key_for' do
5
+ before do
6
+ @auth = Minecraft::JSONAPIv2::Authorization.new(user: 'admin', password: 'changeme', salt: 'mmm')
7
+ end
8
8
 
9
- it 'must be String' do
10
- @auth.key_for(name: 'server.version').must_be_instance_of String
11
- end
9
+ it 'must be String' do
10
+ @auth.key_for(name: 'server.version').must_be_instance_of String
11
+ end
12
12
 
13
- it 'must have the right length' do
14
- @auth.key_for(name: 'server.version').length.must_equal 64
15
- end
13
+ it 'must have the right length' do
14
+ @auth.key_for(name: 'server.version').length.must_equal 64
16
15
  end
16
+ end
17
17
  end
@@ -1,13 +1,13 @@
1
1
  require_relative '../spec_helper.rb'
2
2
 
3
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
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
8
 
9
- it 'should work' do
10
- @api.must_be_instance_of Minecraft::JSONAPIv2::API
11
- end
9
+ it 'should work' do
10
+ @api.must_be_instance_of Minecraft::JSONAPIv2::API
12
11
  end
12
+ end
13
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minecraft_jsonapiv2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Neff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-01 00:00:00.000000000 Z
11
+ date: 2014-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler