Class: Minecraft::JSONAPIv2::API

Inherits:
Object
  • Object
show all
Defined in:
lib/minecraft_jsonapiv2/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



6
7
8
9
# File 'lib/minecraft_jsonapiv2/api.rb', line 6

def initialize(options={})
    @user= options[:user]
    @conn = Minecraft::JSONAPIv2::Request.new(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/minecraft_jsonapiv2/api.rb', line 29

def method_missing(name, *args, &block)
    if block_given?
        block.call Minecraft::JSONAPIv2::Namespace.new(self, name.to_s)
    else
        response = Minecraft::JSONAPIv2::Response.new( @conn.make_request(name: name.to_s.gsub(/_/, '.'), args: args) ).response[name.to_s]
        response = Mash.new(response) if response.is_a? Hash
        begin
            unless response.nil?
                response
            else
                raise NoResponseError
            end
        end
    end
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'lib/minecraft_jsonapiv2/api.rb', line 4

def user
  @user
end

Instance Method Details

#call(method) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/minecraft_jsonapiv2/api.rb', line 11

def call(method)
    if method.is_a? Array
        method.each { |m| call m }
    else
        if method.is_a? Hash
            response = Minecraft::JSONAPIv2::Response.new( @conn.make_request(method) ).response[method[:name].gsub(/\./, '_')]
            response = Mash.new(response) if response.is_a? Hash
        end
    end
    begin
        unless response.nil?
            response
        else
            raise NoResponseError
        end
    end
end