Class: UltraHook::Client
- Inherits:
-
Object
- Object
- UltraHook::Client
- Defined in:
- lib/ultrahook/client.rb
Instance Method Summary collapse
- #error(msg) ⇒ Object
- #full_path(uri) ⇒ Object
- #http_post(uri, data, headers = nil) ⇒ Object
- #init_stream ⇒ Object
- #parse_destination(dest) ⇒ Object
- #process(msg) ⇒ Object
- #process_error(payload) ⇒ Object
- #process_init(payload) ⇒ Object
- #process_request(payload) ⇒ Object
- #process_warning(payload) ⇒ Object
- #retrieve_config ⇒ Object
- #start(*args) ⇒ Object
Instance Method Details
#error(msg) ⇒ Object
164 165 166 167 |
# File 'lib/ultrahook/client.rb', line 164 def error(msg) puts "Error: #{msg}" exit -1 end |
#full_path(uri) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/ultrahook/client.rb', line 103 def full_path(uri) return "/" if uri.path == "" && uri.query == "" return "/?#{uri.query}" if uri.path == "" && uri.query != "" return "#{uri.path}" if uri.path != "" && uri.query == "" return "#{uri.path}?#{uri.query}" if uri.path != "" && uri.query != "" raise "huh? this is impossible!" end |
#http_post(uri, data, headers = nil) ⇒ Object
118 119 120 |
# File 'lib/ultrahook/client.rb', line 118 def http_post(uri, data, headers=nil) Net::HTTP.new(uri.host, uri.port).post(full_path(uri), data, headers) end |
#init_stream ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/ultrahook/client.rb', line 138 def init_stream uri = URI(@stream_url) Net::HTTP.start(uri.host, uri.port) do |http| begin request = Net::HTTP::Get.new "#{uri.path}?#{uri.query}" http.read_timeout = 3660 # give server an extra 60 seconds to send disconnect http.request request do |response| io = "" response.read_body do |chunk| io += chunk if idx = io.index("\n\n") msg = io.slice!(0, idx+2) process(msg) end end end error "Lost connection to UltraHook server" rescue Interrupt http.finish end end end |
#parse_destination(dest) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ultrahook/client.rb', line 65 def parse_destination(dest) if dest =~ /^\d+/ "http://localhost:#{dest}" elsif dest =~ /^[[:alnum:]\.\-]+:\d+$/ "http://#{dest}" elsif dest =~ /^[[:alnum:]\.\-]+$/ "http://#{dest}" elsif dest =~ /^http:\/\// dest else error "Cannot parse destination url" end end |
#process(msg) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ultrahook/client.rb', line 79 def process(msg) begin payload = JSON.parse(Base64.decode64(msg)) rescue error "Cannot communicate with server." end method_name = "process_#{payload["type"]}" send(method_name, payload) if respond_to?(method_name) end |
#process_error(payload) ⇒ Object
95 96 97 |
# File 'lib/ultrahook/client.rb', line 95 def process_error(payload) error payload["message"] end |
#process_init(payload) ⇒ Object
90 91 92 93 |
# File 'lib/ultrahook/client.rb', line 90 def process_init(payload) puts "Forwarding activated..." puts "http://#{@options["host"]}.#{@namespace}.ultrahook.com -> #{@options["destination"]}" end |
#process_request(payload) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/ultrahook/client.rb', line 111 def process_request(payload) uri = URI("#{@options["destination"]}#{payload["path"]}?#{payload["query"]}") response = http_post(uri, payload["body"], payload["headers"]) puts "[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] POST http://#{uri.host}:#{uri.port}#{full_path(uri)} - #{response.code}" end |
#process_warning(payload) ⇒ Object
99 100 101 |
# File 'lib/ultrahook/client.rb', line 99 def process_warning(payload) puts "Warning: "+payload["message"] end |
#retrieve_config ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/ultrahook/client.rb', line 122 def retrieve_config response = http_post(URI("http://www.ultrahook.com/init"), "key=#{@options["key"]}&host=#{@options["host"]}&version=#{UltraHook::VERSION}") error "Could not retrieve config from server: #{response.code}" if response.code.to_i != 200 payload = JSON.parse(response.body) if payload["success"] == true @stream_url = payload["url"] @namespace = payload["namespace"] puts "Authenticated as #{@namespace}" else error payload["error"] end end |
#start(*args) ⇒ Object
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ultrahook/client.rb', line 10 def start(*args) @options = {} optparse = OptionParser.new do |o| o. = "Usage: ultrahook [options] <subdomain> <destination>" o.on("-k", "--key <api key>", String, "API Key") do |key| @options["key"] = key end o.on_tail("-h", "--help", "Display this help") do puts o exit end o.on("-V", "--version", "Display client version") do puts "UltraHook client version: #{UltraHook::VERSION}" exit end end optparse.parse!(args) if @options["key"].nil? || @options["key"] == "" @options["key"] = ENV["ULTRAHOOK_API_KEY"] end if @options["key"].nil? || @options["key"] == "" path = File.("~/.ultrahook") if File.readable?(path) if matchdata = /api_key:\s*([^\s]+)/.match(IO.read(path)) @options["key"] = matchdata.captures[0] end end end if @options["key"].nil? || @options["key"] == "" error "An API key must be provided. Get one from http://www.ultrahook.com" end if args.size != 2 puts optparse exit end @options["host"] = args[0].downcase error "Subdomain can only contain alphanumeric characters and hyphen (-)" unless @options["host"] =~ /^[[:alnum:]\-]+$/ error "Subdomain cannot start or end with hyphens" if @options["host"] =~ /^\-/ || @options["host"] =~ /\-$/ error "Subdomain cannot contain consecutive hyphens" if @options["host"] =~ /\-\-/ @options["destination"] = parse_destination(args[1]) retrieve_config init_stream end |