Class: Ig3tool::Client

Inherits:
GenericClient show all
Defined in:
lib/ig3client/marshal_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



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
77
78
79
80
81
82
83
84
85
# File 'lib/ig3client/marshal_client.rb', line 43

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/marshal"
			req.body = Marshal.dump(args.first)
		else
			args = args[0].to_a.flatten if args[0].is_a? Hash
			extra = args.map{|a| a.class == String ? URI.encode(a) : a}.join('/')
			extra = '//'+extra unless extra.empty?
			req = Net::HTTP::Get.new(uri+extra)
		end
		req["Accept"] = "text/marshal"
		req["Token"] = @token if @token


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