summaryrefslogtreecommitdiff
path: root/extra/rest_client.rb
blob: 90a64e47b51e9aad5017cc4ce5c0659e67efcd52 (plain)
1
2
3
4
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
41
42
43
44
45
46
47
48
49
module Oxidized
  class RestClient
    require 'net/http'
    require 'json'
    require 'uri'
    require 'asetus'

    class Config
      Root      = Root = ENV['OXIDIZED_HOME'] || File.join(ENV['HOME'], '.config', 'oxidized')
    end

    CFGS = Asetus.new :name=>'oxidized', :load=>false, :key_to_s=>true
    CFGS.default.rest = '127.0.0.1:8888'

    begin
      CFGS.load
    rescue => error
      raise InvalidConfig, "Error loading config: #{error.message}"
    ensure
      CFG = CFGS.cfg
    end

    restcfg = CFG.rest
    unless restcfg.match(/^http:\/\//)
      restcfg.insert(0, 'http://')
    end

    HOST = URI(restcfg).host
    PORT = URI(restcfg).port
    PATH = URI(restcfg).path

    class << self
      def next opt={}, host=HOST, port=PORT
        web = new host, port
        web.next opt
      end
    end

    def initialize host=HOST, port=PORT
      @web = Net::HTTP.new host, port
    end

    def next opt
      data = JSON.dump opt
      @web.put PATH + '/node/next/' + opt[:name].to_s, data
    end

  end
end