Class: ForestClient::Forest

Inherits:
Object
  • Object
show all
Defined in:
lib/forest-client.rb

Instance Method Summary collapse

Constructor Details

#initialize(org_id) ⇒ Forest

Returns a new instance of Forest.



17
18
19
# File 'lib/forest-client.rb', line 17

def initialize(org_id)
    @org_id = org_id
end

Instance Method Details

#api_call(url) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/forest-client.rb', line 9

def api_call(url)
    url = "https://forest-api.herokuapp.com/api/#{url}"
    url = URI.escape(url)
    url = URI.parse(url)
    content = Net::HTTP.get(url)
    return JSON.parse(content)
end

#get_printer(id) ⇒ Object



27
28
29
30
31
# File 'lib/forest-client.rb', line 27

def get_printer(id)
    url = "printerGet?organizationId=#{@org_id}&printerId=#{id}"
    printer = api_call(url)
    return printer
end

#get_printersObject



21
22
23
24
25
# File 'lib/forest-client.rb', line 21

def get_printers
    url = "printerList?organizationId=#{@org_id}"
    printers = api_call(url)
    return printers['printers']
end

#update_printer(printer) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/forest-client.rb', line 33

def update_printer(printer)
    id = printer['id']
    ip = printer['ipAddress']

    p = Printer.new(ip)
    url = 'printerUpdate'
    url += '?printerId=' + id
    url += '&model=' + p.get_model
    url += '&location=' + p.get_location
    url += '&serial=' + p.get_serial
    api_call(url)
    return
end

#update_status(printer) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/forest-client.rb', line 47

def update_status(printer)
    id = printer['id']
    ip = printer['ipAddress']
    p = Printer.new(ip)

    url = 'statusCreate'
    url += '?printerId=' + id
    url += '&status=' + p.get_status
    url += '&pageCount=' + p.get_page_count.to_s

    trays = Array.new

    t = p.get_trays
    if t
        t.each { |tray|
            trays.push({
                'name' => tray[:name],
                'xdim' => tray[:x],
                'ydim' => tray[:y],
                'capacity' => tray[:capacity]
            })
        }
    end

    url += '&trays=' + trays.to_s.gsub!('=>', ':')

    consumables = Array.new

    c = p.get_consumables
    if c
        c.each { |cons|
            consumables.push({
                'name' => cons[:name],
                'level' => cons[:level],
                'capacity' => cons[:capacity],
                'percentage' => cons[:percentage]
             })
        }
    end

    url += '&consumables=' + consumables.to_s.gsub!('=>', ':')

    api_call url
end