2525### Barrister Module
2626
2727module Barrister
28-
28+
2929 # Reads the given filename and returns a Barrister::Contract
3030 # object. The filename should be a Barrister IDL JSON file created with
3131 # the `barrister` tool.
@@ -70,7 +70,7 @@ def parse_method(method)
7070 end
7171 end
7272 module_function :parse_method
73-
73+
7474 # Helper function to create a JSON-RPC 2.0 response hash.
7575 #
7676 # * `req` - Request hash sent from the client
@@ -123,7 +123,7 @@ def initialize(code, message, data=nil)
123123 end
124124
125125 end
126-
126+
127127 ### Server
128128
129129 # The Server class is responsible for taking an incoming request, validating
@@ -134,7 +134,7 @@ def initialize(code, message, data=nil)
134134 # It uses the Contract for validation.
135135 #
136136 # The Server doesn't do any network communication. It contains a default
137- # `handle_json` convenience method that encapsulates JSON serialization, and a
137+ # `handle_json` convenience method that encapsulates JSON serialization, and a
138138 # lower level `handle` method. This will make it easy to add other serialization
139139 # formats (such as MessagePack) later.
140140 #
@@ -161,7 +161,7 @@ def add_handler(iface_name, handler)
161161 @handlers [ iface_name ] = handler
162162 end
163163
164- # Handles a request encoded as JSON.
164+ # Handles a request encoded as JSON.
165165 # Returns the result as a JSON encoded string.
166166 def handle_json ( json_str )
167167 begin
@@ -170,7 +170,7 @@ def handle_json(json_str)
170170 rescue JSON ::ParserError => e
171171 resp = err_resp ( { } , -32700 , "Unable to parse JSON: #{ e . message } " )
172172 end
173-
173+
174174 # Note the `:ascii_only` usage here. Important.
175175 return JSON ::generate ( resp , { :ascii_only => true } )
176176 end
@@ -212,13 +212,13 @@ def handle_single(req)
212212 if err_resp != nil
213213 return err_resp
214214 end
215-
215+
216216 # Make sure that the params on the request match the IDL types
217217 err_resp = @contract . validate_params ( req , func )
218218 if err_resp != nil
219219 return err_resp
220220 end
221-
221+
222222 params = [ ]
223223 if req [ "params" ]
224224 params = req [ "params" ]
@@ -237,10 +237,10 @@ def handle_single(req)
237237 return err_resp ( req , -32000 , "Server error. Handler for #{ iface . name } does not implement #{ func . name } " )
238238 end
239239
240- begin
240+ begin
241241 # Call the handler function. This is where your code gets invoked.
242242 result = handler . send ( func . name , *params )
243-
243+
244244 # Verify that the handler function's return value matches the
245245 # correct type as specified in the IDL
246246 err_resp = @contract . validate_result ( req , result , func )
@@ -261,13 +261,13 @@ def handle_single(req)
261261 end
262262
263263 end
264-
264+
265265 ### Client
266266
267267 # This is the main class used when writing a client for a Barrister service.
268268 #
269- # Clients accept a transport class on the constructor which encapsulates
270- # serialization and network communciation. Currently this module only provides a
269+ # Clients accept a transport class on the constructor which encapsulates
270+ # serialization and network communciation. Currently this module only provides a
271271 # basic HTTP transport, but other transports can be easily written.
272272 class Client
273273 include Barrister
@@ -298,7 +298,7 @@ def initialize(trans, validate_req=true, validate_result=true)
298298 def get_meta
299299 return @contract . meta
300300 end
301-
301+
302302 # Returns a Barrister::BatchClient instance that is associated with this Client instance
303303 #
304304 # Batches let you send multiple requests in a single round trip
@@ -330,7 +330,7 @@ def init_proxies
330330 end
331331 end
332332
333- # Sends a JSON-RPC request. This method is automatically called by the proxy classes,
333+ # Sends a JSON-RPC request. This method is automatically called by the proxy classes,
334334 # so in practice you don't usually call it directly. However, it is available if you
335335 # wish to avoid the use of proxy classes.
336336 #
@@ -342,37 +342,37 @@ def request(method, params)
342342 if params
343343 req [ "params" ] = params
344344 end
345-
345+
346346 # We always validate that the method is valid
347347 err_resp , iface , func = @contract . resolve_method ( req )
348348 if err_resp != nil
349349 return err_resp
350350 end
351-
351+
352352 if @validate_req
353353 err_resp = @contract . validate_params ( req , func )
354354 if err_resp != nil
355355 return err_resp
356356 end
357357 end
358-
358+
359359 # This makes the request to the server
360360 resp = @trans . request ( req )
361-
361+
362362 if @validate_result && resp != nil && resp . key? ( "result" )
363363 err_resp = @contract . validate_result ( req , resp [ "result" ] , func )
364364 if err_resp != nil
365365 resp = err_resp
366366 end
367367 end
368-
368+
369369 return resp
370370 end
371371
372372 end
373373
374374 # Default HTTP transport implementation. This is a simple implementation that
375- # doesn't support many options. We may extend this class in the future, but
375+ # doesn't support many options. We may extend this class in the future, but
376376 # you can always write your own transport class based on this one.
377377 class HttpTransport
378378
@@ -382,7 +382,7 @@ def initialize(url)
382382 @uri = URI . parse ( url )
383383 end
384384
385- # `request` is the only required method on a transport class.
385+ # `request` is the only required method on a transport class.
386386 #
387387 # `req` is a JSON-RPC request with `id`, `method`, and optionally `params` slots.
388388 #
@@ -397,6 +397,7 @@ def request(req)
397397 json_str = JSON ::generate ( req , { :ascii_only => true } )
398398 http = Net ::HTTP . new ( @uri . host , @uri . port )
399399 http . use_ssl = ( @uri . scheme == 'https' )
400+ http . ssl_version = :SSLv23
400401 request = Net ::HTTP ::Post . new ( @uri . request_uri )
401402 request . body = json_str
402403 request [ "Content-Type" ] = "application/json"
@@ -469,7 +470,7 @@ def request(req)
469470 # Use a batch if you have many small requests that you'd like to send at once.
470471 #
471472 # **Note:** the JSON-RPC spec indicates that servers **may** execute batch
472- # requests in parallel. Do **not** batch requests that depend on being
473+ # requests in parallel. Do **not** batch requests that depend on being
473474 # sequentially executed.
474475 class BatchClient < Client
475476
@@ -509,7 +510,7 @@ def send
509510
510511 # Send request batch to server
511512 resp_list = @parent . trans . request ( requests )
512-
513+
513514 # Build a hash for the responses so we can re-order them
514515 # in request order.
515516 sorted = [ ]
@@ -536,7 +537,7 @@ def send
536537 end
537538
538539 # Internal class used by the Client and BatchClient classes
539- #
540+ #
540541 # Each instance represents a proxy for a single interface in the IDL,
541542 # and will contain a method for each function in the interface.
542543 #
@@ -567,7 +568,7 @@ def initialize(client, iface)
567568 end
568569
569570 ### Contract / IDL
570-
571+
571572 # Represents a single parsed IDL definition
572573 class Contract
573574 include Barrister
@@ -576,7 +577,7 @@ class Contract
576577
577578 # `idl` must be an Array loaded from a Barrister IDL JSON file
578579 #
579- # `initialize` iterates through the IDL and stores the
580+ # `initialize` iterates through the IDL and stores the
580581 # interfaces, structs, and enums specified in the IDL
581582 def initialize ( idl )
582583 @idl = idl
@@ -612,7 +613,7 @@ def interface(name)
612613 def interfaces
613614 return @interfaces . values
614615 end
615-
616+
616617 # Takes a JSON-RPC request hash, and returns a 3 element tuple. This is called as
617618 # part of the request validation sequence.
618619 #
@@ -625,7 +626,7 @@ def resolve_method(req)
625626 if iface_name == nil
626627 return err_resp ( req , -32601 , "Method not found: #{ method } " )
627628 end
628-
629+
629630 iface = interface ( iface_name )
630631 if !iface
631632 return err_resp ( req , -32601 , "Interface not found on IDL: #{ iface_name } " )
@@ -635,10 +636,10 @@ def resolve_method(req)
635636 if !func
636637 return err_resp ( req , -32601 , "Function #{ func_name } does not exist on interface #{ iface_name } " )
637638 end
638-
639+
639640 return nil , iface , func
640641 end
641-
642+
642643 # Validates that the parameters on the JSON-RPC request match the types specified for
643644 # this function
644645 #
@@ -670,7 +671,7 @@ def validate_params(req, func)
670671 # valid
671672 return nil
672673 end
673-
674+
674675 # Validates that the result from a handler method invocation match the return type
675676 # for this function
676677 #
@@ -726,7 +727,7 @@ def validate(name, expected, expect_array, val)
726727 else
727728 return type_err ( name , "[]" +expected [ "type" ] , val )
728729 end
729-
730+
730731 # Check the built in Barrister primitive types
731732 elsif exp_type == "string"
732733 if val . class == String
@@ -748,23 +749,23 @@ def validate(name, expected, expect_array, val)
748749 else
749750 return type_err ( name , exp_type , val )
750751 end
751-
752+
752753 # Expected type is not an array or a Barrister primitive.
753754 # It must be a struct or an enum.
754755 else
755-
756+
756757 # Try to find a struct
757758 struct = @structs [ exp_type ]
758759 if struct
759760 if !val . kind_of? ( Hash )
760761 return "#{ name } #{ exp_type } value must be a map/hash. not: " + val . class . name
761762 end
762-
763+
763764 s_field_keys = { }
764-
765+
765766 # Resolve all fields on the struct and its ancestors
766767 s_fields = all_struct_fields ( [ ] , struct )
767-
768+
768769 # Validate that each field on the struct has a valid value
769770 s_fields . each do |f |
770771 fname = f [ "name" ]
@@ -774,14 +775,14 @@ def validate(name, expected, expect_array, val)
774775 end
775776 s_field_keys [ fname ] = 1
776777 end
777-
778+
778779 # Validate that there are no extraneous elements on the value
779780 val . keys . each do |k |
780781 if !s_field_keys . key? ( k )
781782 return "#{ name } .#{ k } is not a field in struct '#{ exp_type } '"
782783 end
783784 end
784-
785+
785786 # Struct is valid
786787 return nil
787788 end
@@ -839,7 +840,7 @@ def type_err(name, exp_type, val)
839840
840841 end
841842
842- # Represents a Barrister IDL "interface"
843+ # Represents a Barrister IDL "interface"
843844 class Interface
844845
845846 attr_accessor :name
@@ -859,14 +860,14 @@ def functions
859860 def function ( name )
860861 return @functions [ name ]
861862 end
862-
863+
863864 end
864865
865- # Represents a single function on a Barrister IDL "interface"
866+ # Represents a single function on a Barrister IDL "interface"
866867 class Function
867868
868869 attr_accessor :name , :returns , :params
869-
870+
870871 def initialize ( f )
871872 @name = f [ "name" ]
872873 @returns = f [ "returns" ]
0 commit comments