xdr 3.0.2 → 3.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f0a7d79379430aafb61eed9de69792863eae9f09d6879b9c45a6dfdf4899423
4
- data.tar.gz: 4077d91ba3482d12749dfebfd8af588c8ed8a71ed0ffbc84328752f18bc6099c
3
+ metadata.gz: 5e73176f8adac880de7eb0ed3761730773c9dadac9f572e9a9fd1f75ff396488
4
+ data.tar.gz: ea7c2d4f8fc312f33d4fb81be7a9074a6ed2f6e5dec2cccb41f7c00cdabff248
5
5
  SHA512:
6
- metadata.gz: '087033c3d5e43816556e6265b76892f64c193764af9342eec0e15e1ff615c2f1bed2ef1900d02fc6cd54afb3e158fe081b1a4b966fba019275d48e6c66cb14ac'
7
- data.tar.gz: 01b99345355417827e4087075724186082f713c807612a4ac481a9d84bfefeb6bac31abc9819f0c807ce0d31296f2854dd055f03b4b17c1257c27feb0151e442
6
+ metadata.gz: 9f6825750389ef2f19882533f169c9de4c05a264fa19228fcdbed3f3276196473d6e6e2cac75c420926a097f2543a71100cc17ad0784864e1c18c24da66aa455
7
+ data.tar.gz: 3c04035a3532da2c732aa47664a9bf4cfe79616806f3193948d5c8c37aa2c2862153cb2a74fe13b0afcb30d25f2f1f39729f510ac67655891a9d466de767ba64
data/CHANGELOG.md CHANGED
@@ -5,24 +5,29 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [Unreleased](https://github.com/stellar/ruby-xdr/compare/v3.0.2...master)
8
+ ### [3.0.3](https://github.com/astroband/ruby-xdr/compare/v3.0.2...v3.0.3) (2022-02-18)
9
+ ### Bug Fixes
10
+ - make `XDR::Option` write empty strings as values ([#15](https://github.com/astroband/ruby-xdr/issues/15)) ([ccab229](https://github.com/astroband/ruby-xdr/commit/ccab22928a1104dee525211b51d1d567079d27ba))
9
11
 
10
- ## [3.0.2](https://github.com/stellar/ruby-xdr/compare/v3.0.1...v3.0.2) - 2020-10-20
12
+
13
+ ## [3.0.2](https://github.com/astroband/ruby-xdr/compare/v3.0.1...v3.0.2) - 2020-10-20
11
14
  ### Changed
12
- - Relax lower bound for ActiveSupport dependency to support usage in Rails 4.2 ((#12)[https://github.com/stellar/ruby-xdr/pull/12])
15
+ - Relax lower bound for ActiveSupport dependency to support usage in Rails 4.2 ([#12](https://github.com/astroband/ruby-xdr/pull/12))
16
+
17
+ ### Bug Fixes
18
+ - Fix non-bang arm accessors for XDR::Union ([#13](https://github.com/astroband/ruby-xdr/pull/13))
19
+ - Fix .from_xdr encoding param to accept symbols ([#14](https://github.com/astroband/ruby-xdr/pull/14))
13
20
 
14
- ### Fixed
15
- - Fix non-bang arm accessors for XDR::Union ((#13)[https://github.com/stellar/ruby-xdr/pull/13])
16
- - Fix .from_xdr encoding param to accept symbols ((#14)[https://github.com/stellar/ruby-xdr/pull/14])
17
21
 
18
- ## [3.0.1](https://github.com/stellar/ruby-xdr/compare/v3.0.0...v3.0.1) - 2020-06-10
22
+ ## [3.0.1](https://github.com/astroband/ruby-xdr/compare/v3.0.0...v3.0.1) - 2020-06-10
19
23
  ### Added
20
- - Add encoding parameter to Union#to_xdr ((#7)[https://github.com/stellar/ruby-xdr/pull/7]).
24
+ - Add encoding parameter to Union#to_xdr ([#7](https://github.com/astroband/ruby-xdr/pull/7)).
21
25
 
22
- ### Fixed
26
+ ### Bug Fixes
23
27
  - Padding bytes are now properly validated when reading xdr values. According to the XDR spec, padding must be zeros.
24
28
 
25
- ## [1.0.0](https://github.com/stellar/ruby-xdr/compare/v0.1.0...v1.0.0) - 2015-10-02
29
+
30
+ ## [1.0.0](https://github.com/astroband/ruby-xdr/compare/v0.1.0...v1.0.0) - 2015-10-02
26
31
  ### Added
27
32
  - the `to_xdr` helpers can take a second parameter that will encode the resulting output to hex or base64 when requested.
28
33
  - the `from_xdr` helpers can take a second parameter that will trigger a decode from hex or base64 of the provided string before decoding from xdr.
data/README.md CHANGED
@@ -3,7 +3,9 @@
3
3
  [![Build Status](https://travis-ci.org/stellar/ruby-xdr.svg?branch=master)](https://travis-ci.org/stellar/ruby-xdr)
4
4
  [![Code Climate](https://codeclimate.com/github/stellar/ruby-xdr/badges/gpa.svg)](https://codeclimate.com/github/stellar/ruby-xdr)
5
5
 
6
- XDR is an open data format, specified in [RFC 4506](http://tools.ietf.org/html/rfc4506.html). This library provides a way to read and write XDR data from ruby. It can read/write all of the primitive XDR types and also provides facilities to define readers for the compound XDR types (enums, structs and unions)
6
+ XDR is an open data format, specified in [RFC 4506](http://tools.ietf.org/html/rfc4506.html). This library provides a
7
+ way to read and write XDR data from ruby. It can read/write all of the primitive XDR types and also provides facilities
8
+ to define readers for the compound XDR types (enums, structs and unions)
7
9
 
8
10
  ## Installation
9
11
 
@@ -15,28 +17,28 @@ gem 'xdr'
15
17
 
16
18
  And then execute:
17
19
 
18
- $ bundle
20
+ bundle
19
21
 
20
22
  Or install it yourself as:
21
23
 
22
- $ gem install xdr
24
+ gem install xdr
23
25
 
24
26
  ## Usage
25
27
 
26
28
  ```ruby
27
29
  # Reading/writing a primitive values
28
- XDR::Bool.to_xdr(false) # => "\x00\x00\x00\x00"
30
+ XDR::Bool.to_xdr(false) # => "\x00\x00\x00\x00"
29
31
  XDR::Bool.from_xdr("\x00\x00\x00\x01") # => true
30
32
 
31
33
  # Reading/writing arrays
32
- XDR::Array[XDR::Int,2].to_xdr([1,2]) # => "\x00\x00\x00\x01\x00\x00\x00\x02"
33
- XDR::Array[XDR::Int,2].from_xdr("\x00\x00\x00\x03\x00\x00\x00\x04") # => [3,4]
34
+ XDR::Array[XDR::Int, 2].to_xdr([1, 2]) # => "\x00\x00\x00\x01\x00\x00\x00\x02"
35
+ XDR::Array[XDR::Int, 2].from_xdr("\x00\x00\x00\x03\x00\x00\x00\x04") # => [3,4]
34
36
 
35
37
  # Defining an enum
36
38
 
37
39
  class ResultType < XDR::Enum
38
- member :ok, 0
39
- member :error, 1
40
+ member :ok, 0
41
+ member :error, 1
40
42
  seal
41
43
  end
42
44
 
@@ -88,24 +90,22 @@ Result.from_xdr("\x00\x00\x00\x00") # => #<Result ...>
88
90
 
89
91
  ## Thread safety
90
92
 
91
- Code generated by `xdrgen`, which targets this library, uses autoload extensively.
92
- Since autoloading is not thread-safe, neither is code generated from xdrgen. To
93
- work around this, any module including `XDR::Namespace` can be forced to load
93
+ Code generated by `xdrgen`, which targets this library, uses autoload extensively. Since autoloading is not thread-safe,
94
+ neither is code generated from xdrgen. To work around this, any module including `XDR::Namespace` can be forced to load
94
95
  all of it's children by calling `load_all!` on the module.
95
96
 
96
97
  ## Code generation
97
98
 
98
- ruby-xdr by itself does not have any ability to parse XDR IDL files and produce a parser for your custom
99
- data types. Instead, that is the responsibility of [xdrgen](http://github.com/stellar/xdrgen). `xdrgen` will
100
- take your `.x` files and produce a set of ruby files that target this library to allow for your own custom types.
99
+ ruby-xdr by itself does not have any ability to parse XDR IDL files and produce a parser for your custom data types.
100
+ Instead, that is the responsibility of [xdrgen](http://github.com/stellar/xdrgen). `xdrgen` will take your `.x` files
101
+ and produce a set of ruby files that target this library to allow for your own custom types.
101
102
 
102
103
  See [ruby-stellar-base](http://github.com/astroband/ruby-stellar-sdk/tree/master/base/generated) for an example.
103
104
 
104
105
  ## Contributing
105
106
 
106
- 1. Sign the [Contributor License Agreement](http://goo.gl/forms/6r7I4S0K4z)
107
- 2. Fork it ( https://github.com/stellar/ruby-xdr/fork )
108
- 2. Create your feature branch (`git checkout -b my-new-feature`)
109
- 3. Commit your changes (`git commit -am 'Add some feature'`)
110
- 4. Push to the branch (`git push origin my-new-feature`)
111
- 5. Create a new Pull Request
107
+ 1. Fork the repo ( https://github.com/astroband/ruby-xdr/fork )
108
+ 1. Create your feature branch (`git checkout -b my-new-feature`)
109
+ 1. Commit your changes (`git commit -am 'Add some feature'`)
110
+ 1. Push to the branch (`git push origin my-new-feature`)
111
+ 1. Create a new Pull Request
data/lib/xdr/array.rb CHANGED
@@ -6,7 +6,7 @@ class XDR::Array
6
6
 
7
7
  def initialize(child_type, length)
8
8
  @child_type = child_type
9
- @length = length
9
+ @length = length
10
10
  end
11
11
 
12
12
  def write(val, io)
@@ -19,10 +19,10 @@ class XDR::Array
19
19
  end
20
20
 
21
21
  def read(io)
22
- @length.times.map{ @child_type.read(io) }
22
+ @length.times.map { @child_type.read(io) }
23
23
  end
24
24
 
25
25
  def valid?(val)
26
26
  super(val) && val.length == @length
27
27
  end
28
- end
28
+ end
data/lib/xdr/bool.rb CHANGED
@@ -3,22 +3,22 @@ module XDR::Bool
3
3
 
4
4
  def self.write(val, io)
5
5
  case val
6
- when true ; XDR::Int.write(1, io)
7
- when false ; XDR::Int.write(0, io)
8
- else ; raise XDR::WriteError, "Invalid bool value: #{val}"
6
+ when true then XDR::Int.write(1, io)
7
+ when false then XDR::Int.write(0, io)
8
+ else; raise XDR::WriteError, "Invalid bool value: #{val}"
9
9
  end
10
10
  end
11
11
 
12
12
  def self.read(io)
13
13
  val = XDR::Int.read(io)
14
14
  case val
15
- when 0 ; false
16
- when 1 ; true
17
- else ; raise XDR::ReadError, "Invalid bool value: #{val}"
15
+ when 0 then false
16
+ when 1 then true
17
+ else; raise XDR::ReadError, "Invalid bool value: #{val}"
18
18
  end
19
19
  end
20
20
 
21
21
  def self.valid?(val)
22
22
  val == true || val == false
23
23
  end
24
- end
24
+ end
@@ -1,5 +1,5 @@
1
1
  module XDR::Concerns::ArrayConverter
2
2
  def valid?(val)
3
- val.is_a?(Array) && val.all?{|v| @child_type.valid?(v)}
3
+ val.is_a?(Array) && val.all? { |v| @child_type.valid?(v) }
4
4
  end
5
- end
5
+ end
@@ -1,9 +1,9 @@
1
- require 'base64'
1
+ require "base64"
2
2
 
3
3
  module XDR::Concerns::ConvertsToXDR
4
4
  include XDR::Concerns::ReadsBytes
5
5
 
6
- #
6
+ #
7
7
  # Serialized the provided `val` to xdr and writes it to `io`
8
8
  #
9
9
  # @abstract
@@ -14,7 +14,7 @@ module XDR::Concerns::ConvertsToXDR
14
14
  raise NotImplementedError, "implement in including class"
15
15
  end
16
16
 
17
- #
17
+ #
18
18
  # Reads from the provided IO an instance of the implementing class
19
19
  #
20
20
  # @abstract
@@ -35,7 +35,7 @@ module XDR::Concerns::ConvertsToXDR
35
35
 
36
36
  # Serialized the provided val to xdr, returning a string
37
37
  # of the serialized data
38
- #
38
+ #
39
39
  # @param val [Object] the value to serialize
40
40
  # @param encoding [:raw|:hex|:base64] encode the result with specified codec
41
41
  # @return [String] the produced bytes
@@ -46,7 +46,7 @@ module XDR::Concerns::ConvertsToXDR
46
46
 
47
47
  case String(encoding)
48
48
  when "raw" then raw
49
- when "hex" then raw.unpack("H*").first
49
+ when "hex" then raw.unpack1("H*")
50
50
  when "base64" then Base64.strict_encode64(raw)
51
51
  else
52
52
  raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
@@ -65,13 +65,13 @@ module XDR::Concerns::ConvertsToXDR
65
65
  when "base64" then Base64.strict_decode64(string)
66
66
  else
67
67
  raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
68
- end
68
+ end
69
69
 
70
70
  io = StringIO.new(raw)
71
71
  result = read(io)
72
72
 
73
73
  if io.pos != io.length
74
- raise ArgumentError, "Input string not fully consumed! are you decoding the right xdr type?"
74
+ raise ArgumentError, "Input string not fully consumed! are you decoding the right xdr type?"
75
75
  end
76
76
 
77
77
  result
@@ -81,10 +81,10 @@ module XDR::Concerns::ConvertsToXDR
81
81
 
82
82
  def padding_for(length)
83
83
  case length % 4
84
- when 0 ; 0
85
- when 1 ; 3
86
- when 2 ; 2
87
- when 3 ; 1
84
+ when 0 then 0
85
+ when 1 then 3
86
+ when 2 then 2
87
+ when 3 then 1
88
88
  end
89
89
  end
90
90
  end
@@ -2,4 +2,4 @@ module XDR::Concerns::FloatConverter
2
2
  def valid?(val)
3
3
  val.is_a?(Float)
4
4
  end
5
- end
5
+ end
@@ -2,4 +2,4 @@ module XDR::Concerns::IntegerConverter
2
2
  def valid?(val)
3
3
  val.is_a?(Integer)
4
4
  end
5
- end
5
+ end
@@ -1,5 +1,6 @@
1
1
  module XDR::Concerns::ReadsBytes
2
2
  private
3
+
3
4
  def read_bytes(io, length)
4
5
  io.read(length).tap do |bytes|
5
6
  raise EOFError if bytes.nil? || bytes.length != length
@@ -13,4 +14,4 @@ module XDR::Concerns::ReadsBytes
13
14
 
14
15
  nil
15
16
  end
16
- end
17
+ end
@@ -2,4 +2,4 @@ module XDR::Concerns::StringConverter
2
2
  def valid?(val)
3
3
  val.is_a?(String)
4
4
  end
5
- end
5
+ end
data/lib/xdr/double.rb CHANGED
@@ -8,7 +8,6 @@ module XDR::Double
8
8
  end
9
9
 
10
10
  def self.read(io)
11
- read_bytes(io, 8).unpack("G").first
11
+ read_bytes(io, 8).unpack1("G")
12
12
  end
13
-
14
- end
13
+ end
data/lib/xdr/dsl/enum.rb CHANGED
@@ -1,17 +1,16 @@
1
1
  module XDR::DSL::Enum
2
-
3
2
  def member(name, value)
4
- raise ArgumentError, "#{self} is sealed" if self.sealed
5
- raise ArgumentError, "#{value} is not Integer" unless value.is_a?(Integer)
6
- raise ArgumentError, "#{value} is already used" unless
7
-
8
3
  name = name.to_s.underscore
9
4
 
5
+ raise ArgumentError, "#{self} is sealed" if sealed
6
+ raise ArgumentError, "#{value} is not Integer" unless value.is_a?(Integer)
7
+ raise ArgumentError, "#{value} is already used" if members.key?(name)
8
+
10
9
  instance = new(name, value)
11
- self.members = self.members.merge(name => instance)
12
- self.by_value = self.by_value.merge(instance.value => instance)
10
+ self.members = members.merge(name => instance)
11
+ self.by_value = by_value.merge(instance.value => instance)
13
12
 
14
- class_eval <<-EOS, __FILE__, __LINE__
13
+ class_eval <<-EOS, __FILE__, __LINE__ + 1
15
14
  def self.#{name}
16
15
  members["#{name}"]
17
16
  end
@@ -21,4 +20,4 @@ module XDR::DSL::Enum
21
20
  def seal
22
21
  self.sealed = true
23
22
  end
24
- end
23
+ end
@@ -1,11 +1,10 @@
1
1
  module XDR::DSL::Struct
2
2
  def attribute(name, type)
3
-
4
3
  unless type.is_a?(XDR::Concerns::ConvertsToXDR)
5
4
  raise ArgumentError, "#{type} does not convert to xdr"
6
5
  end
7
6
 
8
- self.fields = self.fields.merge(name => type)
7
+ self.fields = fields.merge(name => type)
9
8
 
10
9
  define_method name do
11
10
  read_attribute(name)
data/lib/xdr/dsl/union.rb CHANGED
@@ -1,39 +1,39 @@
1
1
  module XDR::DSL::Union
2
2
  def switch_on(type, name)
3
- raise ArgumentError if self.switch_type.present?
3
+ raise ArgumentError if switch_type.present?
4
4
  self.switch_type = type
5
5
  self.switch_name = name
6
6
 
7
7
  alias_method name, :switch
8
8
  end
9
9
 
10
- def switch(switch, arm=nil)
11
- raise ArgumentError, "`switch_on` not defined yet" if self.switch_type.nil?
10
+ def switch(switch, arm = nil)
11
+ raise ArgumentError, "`switch_on` not defined yet" if switch_type.nil?
12
12
 
13
13
  switch = normalize_switch_declaration(switch)
14
- self.switches = self.switches.merge(switch => arm)
14
+ self.switches = switches.merge(switch => arm)
15
15
  end
16
16
 
17
17
  def attribute(name, type)
18
18
  raise ArgumentError, "#{type} does not convert to xdr" unless type.is_a?(XDR::Concerns::ConvertsToXDR)
19
19
 
20
- self.arms = self.arms.merge(name => type)
20
+ self.arms = arms.merge(name => type)
21
21
  define_attribute_methods name
22
22
  end
23
23
 
24
24
  private
25
+
25
26
  def normalize_switch_declaration(switch)
26
- case
27
- when switch.is_a?(self.switch_type)
27
+ if switch.is_a?(switch_type)
28
28
  switch
29
- when self.switch_type.valid?(switch)
29
+ elsif switch_type.valid?(switch)
30
30
  switch
31
- when switch == :default
31
+ elsif switch == :default
32
32
  switch
33
- when self.switch_type.respond_to?(:from_name)
34
- self.switch_type.from_name(switch)
33
+ elsif switch_type.respond_to?(:from_name)
34
+ switch_type.from_name(switch)
35
35
  else
36
- raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{self.switch_type}"
36
+ raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{switch_type}"
37
37
  end
38
38
  end
39
39
  end
data/lib/xdr/dsl.rb CHANGED
@@ -4,4 +4,4 @@ module XDR::DSL
4
4
  autoload :Struct
5
5
  autoload :Union
6
6
  autoload :Enum
7
- end
7
+ end
data/lib/xdr/enum.rb CHANGED
@@ -7,7 +7,7 @@ class XDR::Enum
7
7
  class_attribute :sealed
8
8
  self.members = ActiveSupport::OrderedHash.new.with_indifferent_access
9
9
  self.by_value = ActiveSupport::OrderedHash.new
10
- self.sealed = false
10
+ self.sealed = false
11
11
 
12
12
  def self.write(val, io)
13
13
  raise XDR::WriteError, "Invalid enum value: #{val.inspect}" unless val.is_a?(self)
@@ -21,14 +21,14 @@ class XDR::Enum
21
21
  raise XDR::EnumValueError, "Unknown #{name} member: #{value}" if result.blank?
22
22
  end
23
23
  end
24
-
24
+
25
25
  def self.valid?(val)
26
26
  val.is_a?(self)
27
27
  end
28
28
 
29
29
  def self.from_name(name)
30
30
  normalized = name.to_s.underscore
31
- members[normalized].tap do |r|
31
+ members[normalized].tap do |r|
32
32
  raise XDR::EnumNameError, "#{name} is not a member of #{self.name}" if r.blank?
33
33
  end
34
34
  end
@@ -37,12 +37,12 @@ class XDR::Enum
37
37
  attr_reader :value
38
38
 
39
39
  def initialize(name, value)
40
- raise ArgumentError, "#{self.class} is sealed" if self.sealed
41
- @name = name
40
+ raise ArgumentError, "#{self.class} is sealed" if sealed
41
+ @name = name
42
42
  @value = value
43
43
  end
44
44
 
45
45
  def to_s
46
46
  "#{self.class.name}.#{@name}(#{@value})"
47
47
  end
48
- end
48
+ end
data/lib/xdr/float.rb CHANGED
@@ -8,7 +8,6 @@ module XDR::Float
8
8
  end
9
9
 
10
10
  def self.read(io)
11
- read_bytes(io, 4).unpack("g").first
11
+ read_bytes(io, 4).unpack1("g")
12
12
  end
13
-
14
- end
13
+ end
data/lib/xdr/hyper.rb CHANGED
@@ -9,6 +9,6 @@ module XDR::Hyper
9
9
  end
10
10
 
11
11
  def self.read(io)
12
- read_bytes(io, 8).unpack("q>").first
12
+ read_bytes(io, 8).unpack1("q>")
13
13
  end
14
- end
14
+ end
data/lib/xdr/int.rb CHANGED
@@ -9,6 +9,6 @@ module XDR::Int
9
9
  end
10
10
 
11
11
  def self.read(io)
12
- read_bytes(io, 4).unpack("l>").first
12
+ read_bytes(io, 4).unpack1("l>")
13
13
  end
14
- end
14
+ end
data/lib/xdr/namespace.rb CHANGED
@@ -1,9 +1,9 @@
1
- #
1
+ #
2
2
  # A thin wrapper around ActiveSupport::Autoload configured to always eager_load
3
3
  # a child. By calling `load_all!` on the namespace module, all children will
4
4
  # be eager loaded, allowing us to more easily operate in a multi-threaded
5
5
  # environment.
6
- #
6
+ #
7
7
  module XDR::Namespace
8
8
  extend ActiveSupport::Concern
9
9
 
@@ -14,13 +14,11 @@ module XDR::Namespace
14
14
  module ClassMethods
15
15
  def load_all!
16
16
  constants.each do |const_name|
17
- begin
18
- const = const_get const_name
19
- const.public_send :load_all! if const.respond_to? :load_all!
20
- rescue NameError => e
21
- raise e unless e.message =~ /uninitialized constant #{const_name}/
22
- end
17
+ const = const_get const_name
18
+ const.public_send :load_all! if const.respond_to? :load_all!
19
+ rescue NameError => e
20
+ raise e unless /uninitialized constant #{const_name}/.match?(e.message)
23
21
  end
24
22
  end
25
23
  end
26
- end
24
+ end
data/lib/xdr/opaque.rb CHANGED
@@ -12,17 +12,17 @@ class XDR::Opaque
12
12
  def read(io)
13
13
  # read and return @length bytes
14
14
  # throw away @padding bytes
15
- read_bytes(io, @length).tap{ read_zeros(io, @padding) }
15
+ read_bytes(io, @length).tap { read_zeros(io, @padding) }
16
16
  end
17
17
 
18
- def write(val,io)
18
+ def write(val, io)
19
19
  length = val.bytesize
20
-
20
+
21
21
  if val.length != @length
22
- raise XDR::WriteError, "Value length is #{length}, must be #{@length}"
22
+ raise XDR::WriteError, "Value length is #{length}, must be #{@length}"
23
23
  end
24
24
 
25
25
  io.write val
26
26
  io.write "\x00" * @padding
27
27
  end
28
- end
28
+ end
data/lib/xdr/option.rb CHANGED
@@ -6,16 +6,16 @@ class XDR::Option
6
6
  attr_reader :child_type
7
7
 
8
8
  def initialize(child_type)
9
- #TODO, raise an error if child_type is not ConvertToXDR
9
+ # TODO, raise an error if child_type is not ConvertToXDR
10
10
  @child_type = child_type
11
11
  end
12
12
 
13
13
  def write(val, io)
14
- if val.present?
14
+ if val.nil?
15
+ XDR::Bool.write(false, io)
16
+ else
15
17
  XDR::Bool.write(true, io)
16
18
  @child_type.write(val, io)
17
- else
18
- XDR::Bool.write(false, io)
19
19
  end
20
20
  end
21
21
 
@@ -27,4 +27,4 @@ class XDR::Option
27
27
  def valid?(val)
28
28
  val.nil? || @child_type.valid?(val)
29
29
  end
30
- end
30
+ end
data/lib/xdr/quadruple.rb CHANGED
@@ -2,4 +2,4 @@ module XDR::Quadruple
2
2
  extend XDR::Concerns::ConvertsToXDR
3
3
 
4
4
  # No support for quads, so we don't overide read/write
5
- end
5
+ end
@@ -2,6 +2,6 @@ XDR::RPC::Record = Struct.new(:last, :length, :content)
2
2
 
3
3
  XDR::RPC::Record.class_eval do
4
4
  def last?
5
- self.last
5
+ last
6
6
  end
7
- end
7
+ end
@@ -1,16 +1,16 @@
1
1
  class XDR::RPC::RecordReader
2
2
  include XDR::Concerns::ReadsBytes
3
3
 
4
- LAST_MASK = 0x80000000
4
+ LAST_MASK = 0x80000000
5
5
  LENGTH_MASK = 0x7FFFFFFF
6
6
 
7
7
  def read(io)
8
- header = read_bytes(io, 4).unpack("L>").first
9
- length = header & LENGTH_MASK
10
- last = (header & LAST_MASK) > 0
8
+ header = read_bytes(io, 4).unpack1("L>")
9
+ length = header & LENGTH_MASK
10
+ last = (header & LAST_MASK) > 0
11
11
  raw_content = read_bytes(io, length)
12
- content = StringIO.new(raw_content)
13
-
12
+ content = StringIO.new(raw_content)
13
+
14
14
  XDR::RPC::Record.new(last, length, content)
15
15
  end
16
- end
16
+ end
data/lib/xdr/string.rb CHANGED
@@ -3,8 +3,8 @@ class XDR::String
3
3
  include XDR::Concerns::StringConverter
4
4
 
5
5
  singleton_class.send(:alias_method, :[], :new)
6
-
7
- def initialize(length=XDR::MAX_SIZE)
6
+
7
+ def initialize(length = XDR::MAX_SIZE)
8
8
  @length = length
9
9
  end
10
10
 
@@ -31,6 +31,6 @@ class XDR::String
31
31
 
32
32
  # read and return length bytes
33
33
  # throw away padding bytes
34
- read_bytes(io, length).tap{ read_zeros(io, padding) }
34
+ read_bytes(io, length).tap { read_zeros(io, padding) }
35
35
  end
36
- end
36
+ end
data/lib/xdr/struct.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'base64'
1
+ require "base64"
2
2
 
3
3
  class XDR::Struct
4
4
  include ActiveModel::Model
@@ -7,8 +7,8 @@ class XDR::Struct
7
7
  extend XDR::Concerns::ConvertsToXDR
8
8
  extend XDR::DSL::Struct
9
9
 
10
- attribute_method_prefix 'read_'
11
- attribute_method_suffix 'write_'
10
+ attribute_method_prefix "read_"
11
+ attribute_method_suffix "write_"
12
12
 
13
13
  class_attribute :fields
14
14
  self.fields = ActiveSupport::OrderedHash.new
@@ -36,26 +36,25 @@ class XDR::Struct
36
36
  val.is_a?(self)
37
37
  end
38
38
 
39
- def initialize(attributes={})
39
+ def initialize(attributes = {})
40
40
  @attributes = {}
41
41
  super
42
42
  end
43
43
 
44
-
45
44
  #
46
45
  # Serializes struct to xdr, return a string of bytes
47
46
  #
48
- # @param format=:raw [Symbol] The encoding used for the bytes produces, one of (:raw, :hex, :base64)
47
+ # @param format [:raw, :hex, :base64] The encoding used for the bytes produces, one of (:raw, :hex, :base64)
49
48
  #
50
49
  # @return [String] The encoded bytes of this struct
51
- def to_xdr(format=:raw)
50
+ def to_xdr(format = :raw)
52
51
  raw = self.class.to_xdr(self)
53
52
 
54
53
  case format
55
- when :raw ; raw
56
- when :hex ; raw.unpack("H*").first
57
- when :base64 ; Base64.strict_encode64(raw)
58
- else ;
54
+ when :raw then raw
55
+ when :hex then raw.unpack1("H*")
56
+ when :base64 then Base64.strict_encode64(raw)
57
+ else
59
58
  raise ArgumentError, "Invalid format #{format.inspect}; must be :raw, :hex, or :base64"
60
59
  end
61
60
  end
@@ -63,18 +62,18 @@ class XDR::Struct
63
62
  #
64
63
  # Compares two structs for equality
65
64
  #
66
- def == (other)
65
+ def ==(other)
67
66
  return false unless other.is_a?(self.class)
68
- other.attributes == self.attributes
67
+ other.attributes == attributes
69
68
  end
70
69
 
71
- def eql? (other)
70
+ def eql?(other)
72
71
  return false unless other.is_a?(self.class)
73
- other.attributes.eql? self.attributes
72
+ other.attributes.eql? attributes
74
73
  end
75
74
 
76
75
  def hash
77
- [self.class, self.attributes].hash
76
+ [self.class, attributes].hash
78
77
  end
79
78
 
80
79
  def read_attribute(attr)
@@ -3,4 +3,4 @@ class XDR::StructValidator < ActiveModel::Validator
3
3
  # validate each field
4
4
  # TODO
5
5
  end
6
- end
6
+ end
data/lib/xdr/union.rb CHANGED
@@ -5,20 +5,19 @@ class XDR::Union
5
5
  extend XDR::Concerns::ConvertsToXDR
6
6
  extend XDR::DSL::Union
7
7
 
8
-
9
8
  class_attribute :arms
10
9
  class_attribute :switches
11
10
  class_attribute :switch_type
12
11
  class_attribute :switch_name
13
- attr_reader :switch
14
- attr_reader :arm
12
+ attr_reader :switch
13
+ attr_reader :arm
15
14
 
16
- self.arms = ActiveSupport::OrderedHash.new
17
- self.switches = ActiveSupport::OrderedHash.new
15
+ self.arms = ActiveSupport::OrderedHash.new
16
+ self.switches = ActiveSupport::OrderedHash.new
18
17
  self.switch_type = nil
19
18
  self.switch_name = nil
20
19
 
21
- attribute_method_suffix '!'
20
+ attribute_method_suffix "!"
22
21
 
23
22
  def self.arm_for_switch(switch)
24
23
  begin
@@ -38,56 +37,55 @@ class XDR::Union
38
37
  end
39
38
 
40
39
  def self.normalize_switch(switch)
41
- case
42
- when switch.is_a?(self.switch_type)
40
+ if switch.is_a?(switch_type)
43
41
  switch
44
- when self.switch_type.valid?(switch)
42
+ elsif switch_type.valid?(switch)
45
43
  switch
46
- when self.switch_type.respond_to?(:from_name)
47
- self.switch_type.from_name(switch)
44
+ elsif switch_type.respond_to?(:from_name)
45
+ switch_type.from_name(switch)
48
46
  else
49
- raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{self.switch_type}"
47
+ raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{switch_type}"
50
48
  end
51
49
  end
52
50
 
53
51
  def self.read(io)
54
- switch = switch_type.read(io)
55
- arm = arm_for_switch(switch)
52
+ switch = switch_type.read(io)
53
+ arm = arm_for_switch(switch)
56
54
  arm_type = arms[arm] || XDR::Void
57
- value = arm_type.read(io)
55
+ value = arm_type.read(io)
58
56
  new(switch, value)
59
57
  end
60
58
 
61
59
  def self.write(val, io)
62
60
  switch_type.write(val.switch, io)
63
61
  arm_type = arms[val.arm] || XDR::Void
64
- arm_type.write(val.get,io)
62
+ arm_type.write(val.get, io)
65
63
  end
66
64
 
67
65
  def self.valid?(val)
68
66
  val.is_a?(self)
69
67
  end
70
68
 
71
- def initialize(switch=:__unset__, value=:void)
72
- @switch = nil
73
- @arm = nil
74
- @value = nil
69
+ def initialize(switch = :__unset__, value = :void)
70
+ @switch = nil
71
+ @arm = nil
72
+ @value = nil
75
73
  set(switch, value) unless switch == :__unset__
76
74
  end
77
75
 
78
76
  #
79
77
  # Serializes struct to xdr, return a string of bytes
80
78
  #
81
- # @param format=:raw [Symbol] The encoding used for the bytes produces, one of (:raw, :hex, :base64)
79
+ # @param format [:raw, :hex, :base64] The encoding used for the bytes produces, one of (:raw, :hex, :base64)
82
80
  #
83
81
  # @return [String] The encoded bytes of this struct
84
- def to_xdr(format=:raw)
82
+ def to_xdr(format = :raw)
85
83
  self.class.to_xdr(self, format)
86
84
  end
87
85
 
88
- def set(switch, value=:void)
86
+ def set(switch, value = :void)
89
87
  @switch = self.class.normalize_switch switch
90
- @arm = self.class.arm_for_switch @switch
88
+ @arm = self.class.arm_for_switch @switch
91
89
 
92
90
  raise XDR::InvalidValueError unless valid_for_arm_type(value, @arm)
93
91
 
@@ -100,7 +98,7 @@ class XDR::Union
100
98
  @value unless @value == :void
101
99
  end
102
100
 
103
- alias get value
101
+ alias_method :get, :value
104
102
 
105
103
  def attribute(attr)
106
104
  return nil unless @arm.to_s == attr
@@ -117,23 +115,24 @@ class XDR::Union
117
115
  #
118
116
  # Compares two unions for equality
119
117
  #
120
- def == (other)
118
+ def ==(other)
121
119
  return false unless other.is_a?(self.class)
122
- return false unless other.switch == self.switch
123
- other.value == self.value
120
+ return false unless other.switch == switch
121
+ other.value == value
124
122
  end
125
123
 
126
124
  def eql?(other)
127
125
  return false unless other.is_a?(self.class)
128
- return false unless other.switch.eql? self.switch
129
- other.value.eql? self.value
126
+ return false unless other.switch.eql? switch
127
+ other.value.eql? value
130
128
  end
131
-
129
+
132
130
  def hash
133
- [self.class, self.switch, self.value].hash
131
+ [self.class, switch, value].hash
134
132
  end
135
133
 
136
134
  private
135
+
137
136
  def valid_for_arm_type(value, arm)
138
137
  arm_type = arms[@arm]
139
138
 
@@ -4,4 +4,4 @@ class XDR::UnionValidator < ActiveModel::Validator
4
4
  # validate the arm is compatible with the set discriminant
5
5
  # TODO
6
6
  end
7
- end
7
+ end
@@ -9,6 +9,6 @@ module XDR::UnsignedHyper
9
9
  end
10
10
 
11
11
  def self.read(io)
12
- read_bytes(io, 8).unpack("Q>").first
12
+ read_bytes(io, 8).unpack1("Q>")
13
13
  end
14
- end
14
+ end
@@ -9,6 +9,6 @@ module XDR::UnsignedInt
9
9
  end
10
10
 
11
11
  def self.read(io)
12
- read_bytes(io, 4).unpack("L>").first
12
+ read_bytes(io, 4).unpack1("L>")
13
13
  end
14
- end
14
+ end
data/lib/xdr/var_array.rb CHANGED
@@ -4,8 +4,8 @@ class XDR::VarArray
4
4
 
5
5
  singleton_class.send(:alias_method, :[], :new)
6
6
 
7
- def initialize(child_type, length=XDR::MAX_SIZE)
8
- @child_type = child_type
7
+ def initialize(child_type, length = XDR::MAX_SIZE)
8
+ @child_type = child_type
9
9
  @length = length
10
10
  end
11
11
 
@@ -29,10 +29,10 @@ class XDR::VarArray
29
29
  raise XDR::ReadError, "VarArray length #{length} is greater than max #{@length}"
30
30
  end
31
31
 
32
- length.times.map{ @child_type.read(io) }
32
+ length.times.map { @child_type.read(io) }
33
33
  end
34
34
 
35
35
  def valid?(val)
36
36
  super(val) && val.length <= @length
37
37
  end
38
- end
38
+ end
@@ -4,7 +4,7 @@ class XDR::VarOpaque
4
4
 
5
5
  singleton_class.send(:alias_method, :[], :new)
6
6
 
7
- def initialize(length=XDR::MAX_SIZE)
7
+ def initialize(length = XDR::MAX_SIZE)
8
8
  @length = length
9
9
  end
10
10
 
@@ -31,6 +31,6 @@ class XDR::VarOpaque
31
31
 
32
32
  # read and return length bytes
33
33
  # throw away padding bytes
34
- read_bytes(io, length).tap{ read_zeros(io, padding) }
34
+ read_bytes(io, length).tap { read_zeros(io, padding) }
35
35
  end
36
- end
36
+ end
data/lib/xdr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module XDR
2
- VERSION = "3.0.2"
2
+ VERSION = "3.0.3"
3
3
  end
data/lib/xdr/void.rb CHANGED
@@ -11,4 +11,4 @@ module XDR::Void
11
11
  def self.valid?(val)
12
12
  val == :void
13
13
  end
14
- end
14
+ end
data/lib/xdr.rb CHANGED
@@ -56,17 +56,23 @@ module XDR
56
56
  autoload :ArrayConverter
57
57
  end
58
58
 
59
- class Error < StandardError ; end
60
- class ReadError < Error ; end
61
- class EnumValueError < ReadError ; end
62
- class EnumNameError < ReadError ; end
63
- class WriteError < Error ; end
59
+ class Error < StandardError; end
64
60
 
65
- class InvalidSwitchError < Error ; end
66
- class InvalidValueError < Error ; end
67
- class ArmNotSetError < Error ; end
61
+ class ReadError < Error; end
62
+
63
+ class EnumValueError < ReadError; end
64
+
65
+ class EnumNameError < ReadError; end
66
+
67
+ class WriteError < Error; end
68
+
69
+ class InvalidSwitchError < Error; end
70
+
71
+ class InvalidValueError < Error; end
72
+
73
+ class ArmNotSetError < Error; end
68
74
 
69
75
  mattr_accessor :logger
70
- self.logger = ActiveSupport::Logger.new(STDOUT)
71
- self.logger.level = Logger::WARN
76
+ self.logger = ActiveSupport::Logger.new($stdout)
77
+ logger.level = Logger::WARN
72
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xdr
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Fleckenstein
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-20 00:00:00.000000000 Z
11
+ date: 2022-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '4.2'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7.0'
22
+ version: '8.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '4.2'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7.0'
32
+ version: '8.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activemodel
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +39,7 @@ dependencies:
39
39
  version: '4.2'
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '7.0'
42
+ version: '8.0'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
@@ -49,49 +49,7 @@ dependencies:
49
49
  version: '4.2'
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '7.0'
53
- - !ruby/object:Gem::Dependency
54
- name: rake
55
- requirement: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - "~>"
58
- - !ruby/object:Gem::Version
59
- version: '13.0'
60
- type: :development
61
- prerelease: false
62
- version_requirements: !ruby/object:Gem::Requirement
63
- requirements:
64
- - - "~>"
65
- - !ruby/object:Gem::Version
66
- version: '13.0'
67
- - !ruby/object:Gem::Dependency
68
- name: rspec
69
- requirement: !ruby/object:Gem::Requirement
70
- requirements:
71
- - - "~>"
72
- - !ruby/object:Gem::Version
73
- version: '3.0'
74
- type: :development
75
- prerelease: false
76
- version_requirements: !ruby/object:Gem::Requirement
77
- requirements:
78
- - - "~>"
79
- - !ruby/object:Gem::Version
80
- version: '3.0'
81
- - !ruby/object:Gem::Dependency
82
- name: simplecov
83
- requirement: !ruby/object:Gem::Requirement
84
- requirements:
85
- - - "~>"
86
- - !ruby/object:Gem::Version
87
- version: '0.19'
88
- type: :development
89
- prerelease: false
90
- version_requirements: !ruby/object:Gem::Requirement
91
- requirements:
92
- - - "~>"
93
- - !ruby/object:Gem::Version
94
- version: '0.19'
52
+ version: '8.0'
95
53
  description:
96
54
  email:
97
55
  - scott@stellar.org
@@ -141,14 +99,15 @@ files:
141
99
  - lib/xdr/var_opaque.rb
142
100
  - lib/xdr/version.rb
143
101
  - lib/xdr/void.rb
144
- homepage: https://github.com/stellar/ruby-xdr
102
+ homepage: https://github.com/astroband/ruby-xdr
145
103
  licenses:
146
104
  - Apache-2.0
147
105
  metadata:
148
- bug_tracker_uri: https://github.com/stellar/ruby-xdr/issues
149
- changelog_uri: https://github.com/stellar/ruby-xdr/blob/v3.0.2/CHANGELOG.md
150
- documentation_uri: https://rubydoc.info/gems/xdr/3.0.2/
151
- source_code_uri: https://github.com/stellar/ruby-xdr/tree/v3.0.2
106
+ bug_tracker_uri: https://github.com/astroband/ruby-xdr/issues
107
+ changelog_uri: https://github.com/astroband/ruby-xdr/blob/v3.0.3/CHANGELOG.md
108
+ documentation_uri: https://rubydoc.info/gems/xdr/3.0.3/
109
+ github_repo: https://github.com/astroband/ruby-xdr
110
+ source_code_uri: https://github.com/astroband/ruby-xdr/tree/v3.0.3
152
111
  post_install_message:
153
112
  rdoc_options: []
154
113
  require_paths:
@@ -164,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
123
  - !ruby/object:Gem::Version
165
124
  version: '0'
166
125
  requirements: []
167
- rubygems_version: 3.1.4
126
+ rubygems_version: 3.3.5
168
127
  signing_key:
169
128
  specification_version: 4
170
129
  summary: XDR Helper Library