Class: Minecraft::JSONAPI::JSONAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/minecraft-jsonapi/jsonapi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JSONAPI



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/minecraft-jsonapi/jsonapi.rb', line 6

def initialize(options = {})
	raise "A username must be provided." if options[:username].nil?
	raise "A password must be provided." if options[:password].nil?
	raise "Please use a salt."           if options[:salt].nil?

	@host     = options[:host]     || 'localhost'
	@port     = options[:port]     || 20059
	@username = options[:username]
	@password = options[:password]
	@salt     = options[:salt]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/minecraft-jsonapi/jsonapi.rb', line 38

def method_missing(method, *args, &block)
	if block_given?
		block.call Minecraft::JSONAPI::Namespace.new(self, method.to_s)
	else
		url = make_url(method.to_s, args)
		Minecraft::JSONAPI.send_request(url)
	end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



4
5
6
# File 'lib/minecraft-jsonapi/jsonapi.rb', line 4

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



4
5
6
# File 'lib/minecraft-jsonapi/jsonapi.rb', line 4

def port
  @port
end

#usernameObject (readonly)

Returns the value of attribute username.



4
5
6
# File 'lib/minecraft-jsonapi/jsonapi.rb', line 4

def username
  @username
end

Instance Method Details

#call(methods) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/minecraft-jsonapi/jsonapi.rb', line 30

def call(methods)
	method_names = methods.keys.map(&:to_s)
	method_arguments = methods.values.map { |args| Minecraft::JSONAPI.map_to_array args }

	url = make_url(method_names, method_arguments)
	Minecraft::JSONAPI.send_request(url)
end

#create_key(method) ⇒ Object



47
48
49
50
51
# File 'lib/minecraft-jsonapi/jsonapi.rb', line 47

def create_key(method)
	method = JSON.generate(method) if method.is_a? Array

	Digest::SHA2.new.update([@username, method, @password, @salt].join).to_s
end

#inspectObject



18
19
20
# File 'lib/minecraft-jsonapi/jsonapi.rb', line 18

def inspect
	 %Q{#<Minecraft::JSONAPI::JSONAPI:#{object_id} @host="#{@host}", @port=#{@port}, @username="#{@username}">}
end

#make_url(method, args) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/minecraft-jsonapi/jsonapi.rb', line 22

def make_url(method, args)
	if method.is_a? Array
		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) }
	else
		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) }
	end
end

#namespaceObject



53
54
# File 'lib/minecraft-jsonapi/jsonapi.rb', line 53

def namespace
end