Class: Lacuna::Module

Inherits:
Object
  • Object
show all
Defined in:
lib/lacuna/module.rb

Class Method Summary collapse

Class Method Details

.method_missing(name, *args) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/lacuna/module.rb', line 57

def self.method_missing(name, *args)
    if !@module_name.nil?
        self.session_stuff
        self.send(Lacuna.url, @module_name, name.id2name, args)
    else
        puts "#{self} isn't working!"
    end
end

.send(url, post_module, post_method, data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lacuna/module.rb', line 5

def self.send(url, post_module, post_method, data)
    uri = URI.parse url
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    request = Net::HTTP::Post.new '/' + post_module
    request.add_field('Content-Type', 'application/json')

    body = {
        :id      => 1,
        :jsonrpc => '2.0',
        :method  => post_method,
        :params  => data
    }

    # Include session id in requests that need it
    if "#{post_module}/#{post_method}" != 'empire/login'
        body[:params].unshift Session.get
    end

    request.body = JSON.generate body

    response = http.request request
    rv = JSON.parse response.read_body

    if rv['result']
        rv['result']
    elsif rv['error']
        # TODO: throw error?
        p rv['error']
        rv['error']
    else
        rv
    end
end

.session_stuffObject

Check that we’re logged in, if not, get session a set up.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lacuna/module.rb', line 43

def self.session_stuff
    if !Session.valid?
        res = self.send(Lacuna.url, 'empire', 'login', [
            Lacuna.args[:name],
            Lacuna.args[:password],
            Lacuna.api_key,
        ])
        Session.set res['session_id']
    else
        # check time
        return
    end
end