xdr 0.1.0 → 1.0.0
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 +4 -4
- data/CHANGELOG.md +17 -0
- data/README.md +3 -0
- data/lib/xdr/concerns/converts_to_xdr.rb +30 -6
- data/lib/xdr/version.rb +1 -1
- data/spec/lib/xdr/concerns/converts_to_xdr_spec.rb +37 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b24c48ffd27b5c67a69f0d55cc19e95f647c4523
|
4
|
+
data.tar.gz: 3d5d64c5b6282ad0d780413e553f45a3e60a0c89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6658735971aa0ed6a7bc21290d83ffd88560846e544cab8f734484d79284fa5cf1cdf4b7947d0bc62f1247f02e10b72044e0ade90e5d389e109a66b7a091b5be
|
7
|
+
data.tar.gz: 99be5a4e551ef005438b802103fdf7038b2a9bb021155cba47597c0fcbd0f5f28b87e62f66ec4ba9e464b008f172e547990008b470ba0d25764675f217341731
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this
|
4
|
+
file. This project adheres to [Semantic Versioning](http://semver.org/).
|
5
|
+
|
6
|
+
As this project is pre 1.0, breaking changes may happen for minor version
|
7
|
+
bumps. A breaking change will get clearly notified in this log.
|
8
|
+
|
9
|
+
## [1.0.0](https://github.com/stellar/ruby-stellar-base/compare/v0.1.0...v1.0.0)
|
10
|
+
|
11
|
+
### Added
|
12
|
+
- the `to_xdr` helpers can take a second parameter that will encode the resulting output to hex or base64 when requested.
|
13
|
+
- 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.
|
14
|
+
|
15
|
+
### Changed
|
16
|
+
|
17
|
+
- `from_xdr` raises an ArgumentError when the input data is not fully consumed
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# XDR, for Ruby
|
2
2
|
|
3
|
+
[](https://travis-ci.org/stellar/ruby-xdr)
|
4
|
+
[](https://codeclimate.com/github/stellar/ruby-xdr)
|
5
|
+
|
3
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)
|
4
7
|
|
5
8
|
## Installation
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
1
3
|
module XDR::Concerns::ConvertsToXDR
|
2
4
|
include XDR::Concerns::ReadsBytes
|
3
5
|
|
@@ -37,11 +39,19 @@ module XDR::Concerns::ConvertsToXDR
|
|
37
39
|
# @param val [Object] the value to serialize
|
38
40
|
#
|
39
41
|
# @return [String] the produced bytes
|
40
|
-
def to_xdr(val)
|
41
|
-
StringIO.
|
42
|
+
def to_xdr(val, encoding='raw')
|
43
|
+
raw = StringIO.
|
42
44
|
new.
|
43
45
|
tap{|io| write(val, io)}.
|
44
46
|
string.force_encoding("ASCII-8BIT")
|
47
|
+
|
48
|
+
case encoding
|
49
|
+
when 'raw' ; raw
|
50
|
+
when 'base64' ; Base64.strict_encode64(raw)
|
51
|
+
when 'hex' ; raw.unpack("H*").first
|
52
|
+
else
|
53
|
+
raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
|
54
|
+
end
|
45
55
|
end
|
46
56
|
|
47
57
|
#
|
@@ -50,9 +60,23 @@ module XDR::Concerns::ConvertsToXDR
|
|
50
60
|
# @param string [String] the bytes to read from
|
51
61
|
#
|
52
62
|
# @return [Object] the deserialized value
|
53
|
-
def from_xdr(string)
|
54
|
-
|
55
|
-
|
63
|
+
def from_xdr(string, encoding='raw')
|
64
|
+
raw = case encoding
|
65
|
+
when 'raw' ; string
|
66
|
+
when 'base64' ; Base64.strict_decode64(string)
|
67
|
+
when 'hex' ; [string].pack("H*")
|
68
|
+
else
|
69
|
+
raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
|
70
|
+
end
|
71
|
+
|
72
|
+
io = StringIO.new(raw)
|
73
|
+
result = read(io)
|
74
|
+
|
75
|
+
if io.pos != io.length
|
76
|
+
raise ArgumentError, "Input string not fully consumed! are you decoding the right xdr type?"
|
77
|
+
end
|
78
|
+
|
79
|
+
result
|
56
80
|
end
|
57
81
|
|
58
82
|
private
|
@@ -64,4 +88,4 @@ module XDR::Concerns::ConvertsToXDR
|
|
64
88
|
when 3 ; 1
|
65
89
|
end
|
66
90
|
end
|
67
|
-
end
|
91
|
+
end
|
data/lib/xdr/version.rb
CHANGED
@@ -23,14 +23,47 @@ describe XDR::Concerns::ConvertsToXDR, "#to_xdr" do
|
|
23
23
|
expect(subject).to receive(:write).with("hiya", kind_of(StringIO))
|
24
24
|
subject.to_xdr("hiya")
|
25
25
|
end
|
26
|
+
|
27
|
+
context "using an actual xdr type" do
|
28
|
+
subject{ XDR::Opaque.new(4) }
|
29
|
+
|
30
|
+
it "encodes to hex" do
|
31
|
+
r = subject.to_xdr("\x00\x01\x02\x03", "hex")
|
32
|
+
expect(r).to eql("00010203")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "encodes to base64" do
|
36
|
+
r = subject.to_xdr("\x00\x01\x02\x03", "base64")
|
37
|
+
expect(r).to eql("AAECAw==")
|
38
|
+
end
|
39
|
+
end
|
26
40
|
end
|
27
41
|
|
28
42
|
describe XDR::Concerns::ConvertsToXDR, "#from_xdr" do
|
29
43
|
subject{ ImplementedConvertible.new }
|
30
44
|
|
31
|
-
it "calls through to
|
32
|
-
|
45
|
+
it "calls through to read" do
|
46
|
+
allow(subject).to receive(:read).and_call_original
|
33
47
|
subject.from_xdr("hiya")
|
48
|
+
expect(subject).to have_received(:read).with(kind_of(StringIO))
|
49
|
+
end
|
50
|
+
|
51
|
+
context "using an actual xdr type" do
|
52
|
+
subject{ XDR::Opaque.new(4) }
|
53
|
+
|
54
|
+
it "decodes from hex" do
|
55
|
+
r = subject.from_xdr("00010203", "hex")
|
56
|
+
expect(r).to eql("\x00\x01\x02\x03")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "decodes from base64" do
|
60
|
+
r = subject.from_xdr("AAECAw==", "base64")
|
61
|
+
expect(r).to eql("\x00\x01\x02\x03")
|
62
|
+
end
|
63
|
+
|
64
|
+
it "raises an ArgumentError if the input is not fully consumed" do
|
65
|
+
expect{ subject.from_xdr("\x00\x00\x00\x00\x00") }.to raise_error(ArgumentError)
|
66
|
+
end
|
34
67
|
end
|
35
68
|
end
|
36
69
|
|
@@ -42,7 +75,7 @@ class ImplementedConvertible
|
|
42
75
|
include XDR::Concerns::ConvertsToXDR
|
43
76
|
|
44
77
|
def read(io)
|
45
|
-
read_bytes(4)
|
78
|
+
read_bytes(io, 4)
|
46
79
|
end
|
47
80
|
|
48
81
|
def write(val, io)
|
@@ -52,4 +85,4 @@ class ImplementedConvertible
|
|
52
85
|
def valid?(val)
|
53
86
|
true
|
54
87
|
end
|
55
|
-
end
|
88
|
+
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:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Fleckenstein
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -132,6 +132,7 @@ files:
|
|
132
132
|
- ".gitignore"
|
133
133
|
- ".travis.yml"
|
134
134
|
- ".yardopts"
|
135
|
+
- CHANGELOG.md
|
135
136
|
- Gemfile
|
136
137
|
- Guardfile
|
137
138
|
- LICENSE.txt
|