garru-ruby_scribe_client 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. data/CHANGELOG +3 -0
  2. data/LICENSE +0 -0
  3. data/Manifest +39 -0
  4. data/README +0 -0
  5. data/Rakefile +8 -0
  6. data/etc/scribe_cat.rb +23 -0
  7. data/lib/ruby_scribe_client/FacebookService.rb +685 -0
  8. data/lib/ruby_scribe_client/fb303_types.rb +16 -0
  9. data/lib/ruby_scribe_client/reflection_limited_types.rb +148 -0
  10. data/lib/ruby_scribe_client/scribe.rb +82 -0
  11. data/lib/ruby_scribe_client/scribe_types.rb +32 -0
  12. data/lib/ruby_scribe_client.rb +33 -0
  13. data/ruby_scribe_client.gemspec +31 -0
  14. data/vendor/thrift/client.rb +44 -0
  15. data/vendor/thrift/deprecation.rb +155 -0
  16. data/vendor/thrift/exceptions.rb +65 -0
  17. data/vendor/thrift/processor.rb +39 -0
  18. data/vendor/thrift/protocol/binaryprotocol.rb +213 -0
  19. data/vendor/thrift/protocol/binaryprotocolaccelerated.rb +19 -0
  20. data/vendor/thrift/protocol/tbinaryprotocol.rb +2 -0
  21. data/vendor/thrift/protocol/tprotocol.rb +2 -0
  22. data/vendor/thrift/protocol.rb +270 -0
  23. data/vendor/thrift/serializer.rb +27 -0
  24. data/vendor/thrift/server/httpserver.rb +44 -0
  25. data/vendor/thrift/server/nonblockingserver.rb +278 -0
  26. data/vendor/thrift/server/thttpserver.rb +2 -0
  27. data/vendor/thrift/server/tserver.rb +2 -0
  28. data/vendor/thrift/server.rb +135 -0
  29. data/vendor/thrift/struct.rb +272 -0
  30. data/vendor/thrift/thrift.rb +14 -0
  31. data/vendor/thrift/transport/httpclient.rb +29 -0
  32. data/vendor/thrift/transport/socket.rb +153 -0
  33. data/vendor/thrift/transport/thttpclient.rb +2 -0
  34. data/vendor/thrift/transport/tsocket.rb +2 -0
  35. data/vendor/thrift/transport/ttransport.rb +2 -0
  36. data/vendor/thrift/transport/unixsocket.rb +58 -0
  37. data/vendor/thrift/transport.rb +319 -0
  38. data/vendor/thrift/types.rb +83 -0
  39. data/vendor/thrift.rb +28 -0
  40. metadata +104 -0
@@ -0,0 +1,29 @@
1
+ require 'thrift/transport'
2
+
3
+ require 'net/http'
4
+ require 'net/https'
5
+ require 'uri'
6
+ require 'stringio'
7
+
8
+ ## Very simple HTTP client
9
+ module Thrift
10
+ class HTTPClient < Transport
11
+ def initialize(url)
12
+ @url = URI url
13
+ @outbuf = ""
14
+ end
15
+
16
+ def open?; true end
17
+ def read(sz); @inbuf.read sz end
18
+ def write(buf); @outbuf << buf end
19
+ def flush
20
+ http = Net::HTTP.new @url.host, @url.port
21
+ http.use_ssl = @url.scheme == "https"
22
+ headers = { 'Content-Type' => 'application/x-thrift' }
23
+ resp, data = http.post(@url.path, @outbuf, headers)
24
+ @inbuf = StringIO.new data
25
+ @outbuf = ""
26
+ end
27
+ end
28
+ deprecate_class! :THttpClient => HTTPClient
29
+ end
@@ -0,0 +1,153 @@
1
+ # Copyright (c) 2006- Facebook
2
+ # Distributed under the Apache Software License
3
+ #
4
+ # See accompanying file LICENSE or visit the Thrift site at:
5
+ # http://developers.facebook.com/thrift/
6
+ #
7
+ # Author: Mark Slee <mcslee@facebook.com>
8
+ #
9
+ require 'thrift/transport'
10
+ require 'socket'
11
+
12
+ module Thrift
13
+ class Socket < Transport
14
+ def initialize(host='localhost', port=9090, timeout=nil)
15
+ @host = host
16
+ @port = port
17
+ @timeout = timeout
18
+ @desc = "#{host}:#{port}"
19
+ @handle = nil
20
+ end
21
+
22
+ attr_accessor :handle, :timeout
23
+
24
+ def open
25
+ begin
26
+ @handle = TCPSocket.new(@host, @port)
27
+ rescue StandardError
28
+ raise TransportException.new(TransportException::NOT_OPEN, "Could not connect to #{@desc}")
29
+ end
30
+ end
31
+
32
+ def open?
33
+ !@handle.nil? and !@handle.closed?
34
+ end
35
+
36
+ def write(str)
37
+ raise IOError, "closed stream" unless open?
38
+ begin
39
+ if @timeout.nil? or @timeout == 0
40
+ @handle.write(str)
41
+ else
42
+ len = 0
43
+ start = Time.now
44
+ while Time.now - start < @timeout
45
+ rd, wr, = IO.select(nil, [@handle], nil, @timeout)
46
+ if wr and not wr.empty?
47
+ len += @handle.write_nonblock(str[len..-1])
48
+ break if len >= str.length
49
+ end
50
+ end
51
+ if len < str.length
52
+ raise TransportException.new(TransportException::TIMED_OUT, "Socket: Timed out writing #{str.length} bytes to #{@desc}")
53
+ else
54
+ len
55
+ end
56
+ end
57
+ rescue TransportException => e
58
+ # pass this on
59
+ raise e
60
+ rescue StandardError => e
61
+ @handle.close
62
+ @handle = nil
63
+ raise TransportException.new(TransportException::NOT_OPEN, e.message)
64
+ end
65
+ end
66
+
67
+ def read(sz)
68
+ raise IOError, "closed stream" unless open?
69
+
70
+ begin
71
+ if @timeout.nil? or @timeout == 0
72
+ data = @handle.readpartial(sz)
73
+ else
74
+ # it's possible to interrupt select for something other than the timeout
75
+ # so we need to ensure we've waited long enough
76
+ start = Time.now
77
+ rd = nil # scoping
78
+ loop do
79
+ rd, = IO.select([@handle], nil, nil, @timeout)
80
+ break if (rd and not rd.empty?) or Time.now - start >= @timeout
81
+ end
82
+ if rd.nil? or rd.empty?
83
+ raise TransportException.new(TransportException::TIMED_OUT, "Socket: Timed out reading #{sz} bytes from #{@desc}")
84
+ else
85
+ data = @handle.readpartial(sz)
86
+ end
87
+ end
88
+ rescue TransportException => e
89
+ # don't let this get caught by the StandardError handler
90
+ raise e
91
+ rescue StandardError => e
92
+ @handle.close unless @handle.closed?
93
+ @handle = nil
94
+ raise TransportException.new(TransportException::NOT_OPEN, e.message)
95
+ end
96
+ if (data.nil? or data.length == 0)
97
+ raise TransportException.new(TransportException::UNKNOWN, "Socket: Could not read #{sz} bytes from #{@desc}")
98
+ end
99
+ data
100
+ end
101
+
102
+ def close
103
+ @handle.close unless @handle.nil? or @handle.closed?
104
+ @handle = nil
105
+ end
106
+
107
+ def to_io
108
+ @handle
109
+ end
110
+ end
111
+ deprecate_class! :TSocket => Socket
112
+
113
+ class ServerSocket < ServerTransport
114
+ # call-seq: initialize(host = nil, port)
115
+ def initialize(host_or_port, port = nil)
116
+ if port
117
+ @host = host_or_port
118
+ @port = port
119
+ else
120
+ @host = nil
121
+ @port = host_or_port
122
+ end
123
+ @handle = nil
124
+ end
125
+
126
+ attr_reader :handle
127
+
128
+ def listen
129
+ @handle = TCPServer.new(@host, @port)
130
+ end
131
+
132
+ def accept
133
+ unless @handle.nil?
134
+ sock = @handle.accept
135
+ trans = Socket.new
136
+ trans.handle = sock
137
+ trans
138
+ end
139
+ end
140
+
141
+ def close
142
+ @handle.close unless @handle.nil? or @handle.closed?
143
+ @handle = nil
144
+ end
145
+
146
+ def closed?
147
+ @handle.nil? or @handle.closed?
148
+ end
149
+
150
+ alias to_io handle
151
+ end
152
+ deprecate_class! :TServerSocket => ServerSocket
153
+ end
@@ -0,0 +1,2 @@
1
+ require 'thrift/deprecation'
2
+ require 'thrift/transport/httpclient'
@@ -0,0 +1,2 @@
1
+ require 'thrift/deprecation'
2
+ require 'thrift/transport/socket'
@@ -0,0 +1,2 @@
1
+ require 'thrift/deprecation'
2
+ require 'thrift/transport'
@@ -0,0 +1,58 @@
1
+ require 'thrift/transport'
2
+ require 'socket'
3
+
4
+ module Thrift
5
+ class UNIXSocket < Socket
6
+ def initialize(path, timeout=nil)
7
+ @path = path
8
+ @timeout = timeout
9
+ @desc = @path # for read()'s error
10
+ @handle = nil
11
+ end
12
+
13
+ def open
14
+ begin
15
+ @handle = ::UNIXSocket.new(@path)
16
+ rescue StandardError
17
+ raise TransportException.new(TransportException::NOT_OPEN, "Could not open UNIX socket at #{@path}")
18
+ end
19
+ end
20
+ end
21
+
22
+ class UNIXServerSocket < ServerTransport
23
+ def initialize(path)
24
+ @path = path
25
+ @handle = nil
26
+ end
27
+
28
+ attr_accessor :handle
29
+
30
+ def listen
31
+ @handle = ::UNIXServer.new(@path)
32
+ end
33
+
34
+ def accept
35
+ unless @handle.nil?
36
+ sock = @handle.accept
37
+ trans = UNIXSocket.new(nil)
38
+ trans.handle = sock
39
+ trans
40
+ end
41
+ end
42
+
43
+ def close
44
+ if @handle
45
+ @handle.close unless @handle.closed?
46
+ @handle = nil
47
+ # UNIXServer doesn't delete the socket file, so we have to do it ourselves
48
+ File.delete(@path)
49
+ end
50
+ end
51
+
52
+ def closed?
53
+ @handle.nil? or @handle.closed?
54
+ end
55
+
56
+ alias to_io handle
57
+ end
58
+ end
@@ -0,0 +1,319 @@
1
+ # Copyright (c) 2006- Facebook
2
+ # Distributed under the Apache Software License
3
+ #
4
+ # See accompanying file LICENSE or visit the Thrift site at:
5
+ # http://developers.facebook.com/thrift/
6
+ #
7
+ # Author: Mark Slee <mcslee@facebook.com>
8
+ #
9
+
10
+ module Thrift
11
+ class TransportException < Exception
12
+ UNKNOWN = 0
13
+ NOT_OPEN = 1
14
+ ALREADY_OPEN = 2
15
+ TIMED_OUT = 3
16
+ END_OF_FILE = 4
17
+
18
+ attr_reader :type
19
+
20
+ def initialize(type=UNKNOWN, message=nil)
21
+ super(message)
22
+ @type = type
23
+ end
24
+ end
25
+ deprecate_class! :TTransportException => TransportException
26
+
27
+ # Transport is basically an abstract class, but isn't raising NotImplementedError
28
+ # TODO: Think about if this is the right thing - Kevin Clark - 3/27/08
29
+ class Transport
30
+ def open?; end
31
+ deprecate! :isOpen => :open?
32
+ deprecate! :is_open? => :open?
33
+
34
+ def open; end
35
+
36
+ def close; end
37
+
38
+ def read(sz); end
39
+
40
+ def read_all(size)
41
+ buf = ''
42
+
43
+ while (buf.length < size)
44
+ chunk = read(size - buf.length)
45
+ buf << chunk
46
+ end
47
+
48
+ buf
49
+ end
50
+ deprecate! :readAll => :read_all
51
+
52
+ def write(buf); end
53
+ alias_method :<<, :write
54
+
55
+ def flush; end
56
+ end
57
+ deprecate_class! :TTransport => Transport
58
+
59
+ class ServerTransport
60
+ def listen; nil; end
61
+
62
+ def accept; nil; end
63
+
64
+ def close; nil; end
65
+
66
+ def closed?; nil; end
67
+ end
68
+ deprecate_class! :TServerTransport => ServerTransport
69
+
70
+ class TransportFactory
71
+ def get_transport(trans)
72
+ return trans
73
+ end
74
+ deprecate! :getTransport => :get_transport
75
+ end
76
+ deprecate_class! :TTransportFactory => TransportFactory
77
+
78
+ class BufferedTransport < Transport
79
+ DEFAULT_BUFFER = 4096
80
+
81
+ def initialize(transport)
82
+ @transport = transport
83
+ @wbuf = ''
84
+ @rbuf = ''
85
+ end
86
+
87
+ def open?
88
+ return @transport.open?
89
+ end
90
+
91
+ def open
92
+ @transport.open
93
+ end
94
+
95
+ def close
96
+ flush
97
+ @transport.close
98
+ end
99
+
100
+ def read(sz)
101
+ ret = @rbuf.slice!(0...sz)
102
+ if ret.length == 0
103
+ @rbuf = @transport.read([sz, DEFAULT_BUFFER].max)
104
+ @rbuf.slice!(0...sz)
105
+ else
106
+ ret
107
+ end
108
+ end
109
+
110
+ def write(buf)
111
+ @wbuf << buf
112
+ end
113
+
114
+ def flush
115
+ if @wbuf != ''
116
+ @transport.write(@wbuf)
117
+ @wbuf = ''
118
+ end
119
+
120
+ @transport.flush
121
+ end
122
+
123
+ def borrow(requested_length = 0)
124
+ # $stderr.puts "#{Time.now.to_f} Have #{@rbuf.length} asking for #{requested_length.inspect}"
125
+ return @rbuf if @rbuf.length > requested_length
126
+
127
+ if @rbuf.length < DEFAULT_BUFFER
128
+ @rbuf << @transport.read([requested_length, DEFAULT_BUFFER].max)
129
+ end
130
+
131
+ if @rbuf.length < requested_length
132
+ @rbuf << @transport.read_all(requested_length - @rbuf.length)
133
+ end
134
+
135
+ @rbuf
136
+ end
137
+
138
+ def consume!(size)
139
+ @rbuf.slice!(0...size)
140
+ end
141
+ end
142
+ deprecate_class! :TBufferedTransport => BufferedTransport
143
+
144
+ class BufferedTransportFactory < TransportFactory
145
+ def get_transport(transport)
146
+ return BufferedTransport.new(transport)
147
+ end
148
+ end
149
+ deprecate_class! :TBufferedTransportFactory => BufferedTransportFactory
150
+
151
+ class FramedTransport < Transport
152
+ def initialize(transport, read=true, write=true)
153
+ @transport = transport
154
+ @rbuf = ''
155
+ @wbuf = ''
156
+ @read = read
157
+ @write = write
158
+ end
159
+
160
+ def open?
161
+ @transport.open?
162
+ end
163
+
164
+ def open
165
+ @transport.open
166
+ end
167
+
168
+ def close
169
+ @transport.close
170
+ end
171
+
172
+ def read(sz)
173
+ return @transport.read(sz) unless @read
174
+
175
+ return '' if sz <= 0
176
+
177
+ read_frame if @rbuf.empty?
178
+
179
+ @rbuf.slice!(0, sz)
180
+ end
181
+
182
+ def write(buf,sz=nil)
183
+ return @transport.write(buf) unless @write
184
+
185
+ @wbuf << (sz ? buf[0...sz] : buf)
186
+ end
187
+
188
+ #
189
+ # Writes the output buffer to the stream in the format of a 4-byte length
190
+ # followed by the actual data.
191
+ #
192
+ def flush
193
+ return @transport.flush unless @write
194
+
195
+ out = [@wbuf.length].pack('N')
196
+ out << @wbuf
197
+ @transport.write(out)
198
+ @transport.flush
199
+ @wbuf = ''
200
+ end
201
+
202
+ def borrow(requested_length = 0)
203
+ read_frame if @rbuf.empty?
204
+ # there isn't any more coming, so if it's not enough, it's an error.
205
+ raise EOFError if requested_length > @rbuf.size
206
+ @rbuf
207
+ end
208
+
209
+ def consume!(size)
210
+ @rbuf.slice!(0...size)
211
+ end
212
+
213
+ private
214
+
215
+ def read_frame
216
+ sz = @transport.read_all(4).unpack('N').first
217
+
218
+ @rbuf = @transport.read_all(sz).dup # protect against later #slice!
219
+ end
220
+ end
221
+ deprecate_class! :TFramedTransport => FramedTransport
222
+
223
+ class FramedTransportFactory < TransportFactory
224
+ def get_transport(transport)
225
+ return FramedTransport.new(transport)
226
+ end
227
+ end
228
+ deprecate_class! :TFramedTransportFactory => FramedTransportFactory
229
+
230
+ class MemoryBuffer < Transport
231
+ GARBAGE_BUFFER_SIZE = 4*(2**10) # 4kB
232
+
233
+ # If you pass a string to this, you should #dup that string
234
+ # unless you want it to be modified by #read and #write
235
+ #--
236
+ # this behavior is no longer required. If you wish to change it
237
+ # go ahead, just make sure the specs pass
238
+ def initialize(buffer = nil)
239
+ @buf = buffer || ''
240
+ @index = 0
241
+ end
242
+
243
+ def open?
244
+ return true
245
+ end
246
+
247
+ def open
248
+ end
249
+
250
+ def close
251
+ end
252
+
253
+ def peek
254
+ @index < @buf.size
255
+ end
256
+
257
+ # this method does not use the passed object directly but copies it
258
+ def reset_buffer(new_buf = '')
259
+ @buf.replace new_buf
260
+ @index = 0
261
+ end
262
+
263
+ def available
264
+ @buf.length - @index
265
+ end
266
+
267
+ def read(len)
268
+ data = @buf.slice(@index, len)
269
+ @index += len
270
+ @index = @buf.size if @index > @buf.size
271
+ if @index >= GARBAGE_BUFFER_SIZE
272
+ @buf = @buf.slice(@index..-1)
273
+ @index = 0
274
+ end
275
+ data
276
+ end
277
+
278
+ def write(wbuf)
279
+ @buf << wbuf
280
+ end
281
+
282
+ def flush
283
+ end
284
+
285
+ # For fast binary protocol access
286
+ def borrow(size = nil)
287
+ if size.nil?
288
+ @buf[@index..-1]
289
+ else
290
+ if size > available
291
+ raise EOFError # Memory buffers only get one shot.
292
+ else
293
+ @buf[@index, size]
294
+ end
295
+ end
296
+ end
297
+
298
+ alias_method :consume!, :read
299
+ end
300
+ deprecate_class! :TMemoryBuffer => MemoryBuffer
301
+
302
+ ## Very very simple implementation of wrapping two objects, one with a #read
303
+ ## method and one with a #write method, into a transport for thrift.
304
+ ##
305
+ ## Assumes both objects are open, remain open, don't require flushing, etc.
306
+ class IOStreamTransport < Transport
307
+ def initialize(input, output)
308
+ @input = input
309
+ @output = output
310
+ end
311
+
312
+ def open?; not @input.closed? or not @output.closed? end
313
+ def read(sz); @input.read(sz) end
314
+ def write(buf); @output.write(buf) end
315
+ def close; @input.close; @output.close end
316
+ def to_io; @input end # we're assuming this is used in a IO.select for reading
317
+ end
318
+ deprecate_class! :TIOStreamTransport => IOStreamTransport
319
+ end
@@ -0,0 +1,83 @@
1
+ require 'set'
2
+
3
+ module Thrift
4
+ module Types
5
+ STOP = 0
6
+ VOID = 1
7
+ BOOL = 2
8
+ BYTE = 3
9
+ DOUBLE = 4
10
+ I16 = 6
11
+ I32 = 8
12
+ I64 = 10
13
+ STRING = 11
14
+ STRUCT = 12
15
+ MAP = 13
16
+ SET = 14
17
+ LIST = 15
18
+ end
19
+ deprecate_module! :TType => Types
20
+
21
+ class << self
22
+ attr_accessor :type_checking
23
+ end
24
+
25
+ class TypeError < Exception
26
+ end
27
+
28
+ def self.check_type(value, field, name, skip_nil=true)
29
+ return if value.nil? and skip_nil
30
+ klasses = case field[:type]
31
+ when Types::VOID
32
+ NilClass
33
+ when Types::BOOL
34
+ [TrueClass, FalseClass]
35
+ when Types::BYTE, Types::I16, Types::I32, Types::I64
36
+ Integer
37
+ when Types::DOUBLE
38
+ Float
39
+ when Types::STRING
40
+ String
41
+ when Types::STRUCT
42
+ Struct
43
+ when Types::MAP
44
+ Hash
45
+ when Types::SET
46
+ Set
47
+ when Types::LIST
48
+ Array
49
+ end
50
+ valid = klasses && [*klasses].any? { |klass| klass === value }
51
+ raise TypeError, "Expected #{type_name(field[:type])}, received #{value.class} for field #{name}" unless valid
52
+ # check elements now
53
+ case field[:type]
54
+ when Types::MAP
55
+ value.each_pair do |k,v|
56
+ check_type(k, field[:key], "#{name}.key", false)
57
+ check_type(v, field[:value], "#{name}.value", false)
58
+ end
59
+ when Types::SET, Types::LIST
60
+ value.each do |el|
61
+ check_type(el, field[:element], "#{name}.element", false)
62
+ end
63
+ when Types::STRUCT
64
+ raise TypeError, "Expected #{field[:class]}, received #{value.class} for field #{name}" unless field[:class] == value.class
65
+ end
66
+ end
67
+
68
+ def self.type_name(type)
69
+ Types.constants.each do |const|
70
+ return "Types::#{const}" if Types.const_get(const) == type
71
+ end
72
+ nil
73
+ end
74
+
75
+ module MessageTypes
76
+ CALL = 1
77
+ REPLY = 2
78
+ EXCEPTION = 3
79
+ end
80
+ deprecate_module! :TMessageType => MessageTypes
81
+ end
82
+
83
+ Thrift.type_checking = false if Thrift.type_checking.nil?
data/vendor/thrift.rb ADDED
@@ -0,0 +1,28 @@
1
+ #
2
+ # Copyright (c) 2006- Facebook
3
+ # Distributed under the Apache Software License
4
+ #
5
+ # See accompanying file LICENSE or visit the Thrift site at:
6
+ # http://developers.facebook.com/thrift/
7
+ #
8
+ # Author: Mark Slee <mcslee@facebook.com>
9
+ #
10
+
11
+ $:.unshift File.dirname(__FILE__)
12
+
13
+ module Thrift
14
+ # prevent the deprecation layer from being loaded if you require 'thrift'
15
+ DEPRECATION = false unless const_defined? :DEPRECATION
16
+ end
17
+
18
+ require 'thrift/deprecation'
19
+ require 'thrift/exceptions'
20
+ require 'thrift/types'
21
+ require 'thrift/processor'
22
+ require 'thrift/client'
23
+ require 'thrift/struct'
24
+ require 'thrift/protocol'
25
+ require 'thrift/protocol/binaryprotocol'
26
+ require 'thrift/transport'
27
+ require 'thrift/transport/socket'
28
+ require 'thrift/server'