Class: Ig3tool::YAMLClient

Inherits:
GenericClient show all
Defined in:
lib/ig3client/yaml_client.rb

Constant Summary

Constants inherited from GenericClient

GenericClient::BIG_QUERIES, GenericClient::EXPIRY

Instance Attribute Summary

Attributes inherited from GenericClient

#token, #username

Instance Method Summary collapse

Methods inherited from GenericClient

#initialize, #request!, #valid_token?, #wannabe!

Constructor Details

This class inherits a constructor from Ig3tool::GenericClient

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ig3client/yaml_client.rb', line 35

def method_missing(msg, *args, &block)
	msg = msg.to_s

	if match = /^async_(.*)/.match(msg)
		raise "Async method called, no block given!" unless block_given?
		Thread.new do
			ans = self.send(match[1], *args)
			block.call(ans)
		end
	else
		uri = "/"+msg.gsub('_','/').sub(/!$/,'')


		if msg.ends_with? '!'
			args = [Hash[*args]] if args.length % 2 == 0
			req = Net::HTTP::Post.new(uri)
			req["Content-Type"] = "text/yaml"
			req.body = args.first.to_yaml
		else
			args = args[0].to_a.flatten if args[0].is_a? Hash
			extra = args.join('/')
			extra = '//'+extra unless extra.empty?
			req = Net::HTTP::Get.new(uri+extra)
		end
		req["Accept"] = "text/yaml"
		req["Token"] = @token if @token


		n = Net::HTTP.new(@host,@port)
		Timeout::timeout(@timeout) do
			n.request(req) do |res|
				rez = YAML::load(res.read_body)
				_reraise(rez["type"].to_sym, rez["message"]) if res.code.to_i > 200
				if block_given?
					yield rez
				else
					return rez
				end
			end
		end
	end
end