protip 0.10.7 → 0.11.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/definitions/google/protobuf/wrappers.proto +99 -0
- data/definitions/protip/messages/array.proto +3 -2
- data/definitions/protip/messages/errors.proto +5 -4
- data/definitions/protip/messages/types.proto +6 -4
- data/lib/google/protobuf/wrappers.rb +48 -0
- data/lib/protip/client.rb +3 -4
- data/lib/protip/error.rb +2 -3
- data/lib/protip/messages/array.rb +16 -0
- data/lib/protip/messages/errors.rb +22 -0
- data/lib/protip/messages/types.rb +18 -0
- data/lib/protip/resource.rb +11 -9
- data/lib/protip/standard_converter.rb +12 -41
- data/lib/protip/wrapper.rb +48 -26
- data/test/functional/protip/resource_test.rb +173 -138
- data/test/unit/protip/resource_test.rb +107 -85
- data/test/unit/protip/standard_converter_test.rb +14 -11
- data/test/unit/protip/wrapper_test.rb +105 -37
- metadata +12 -12
- data/definitions/protip/messages/wrappers.proto +0 -60
- data/lib/protip/messages/array.pb.rb +0 -27
- data/lib/protip/messages/errors.pb.rb +0 -34
- data/lib/protip/messages/types.pb.rb +0 -26
- data/lib/protip/messages/wrappers.pb.rb +0 -64
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25ad2e3a190f034f359fc8ca65ade0e535cb9ae6
|
4
|
+
data.tar.gz: 1e5a004ee50b758b10af25a4013dbb28f8aca8aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cfef2853b9da32d395e76fdffaf9ffa71f527cc31da27264b54a0f338aa8a9b78b4011ea307c93e85a6285325ab3b13b371752fa048cf51df0940d1538c77b9
|
7
|
+
data.tar.gz: cc4d369252fc7b80a414a039d59942643e8a70a2ecdb1544ca256a3a8577f6ed0dff15a08f62a07268ff61601d37cb2d7b5a4c4a15f86af896666a7ba20fddd7
|
@@ -0,0 +1,99 @@
|
|
1
|
+
// Protocol Buffers - Google's data interchange format
|
2
|
+
// Copyright 2008 Google Inc. All rights reserved.
|
3
|
+
// https://developers.google.com/protocol-buffers/
|
4
|
+
//
|
5
|
+
// Redistribution and use in source and binary forms, with or without
|
6
|
+
// modification, are permitted provided that the following conditions are
|
7
|
+
// met:
|
8
|
+
//
|
9
|
+
// * Redistributions of source code must retain the above copyright
|
10
|
+
// notice, this list of conditions and the following disclaimer.
|
11
|
+
// * Redistributions in binary form must reproduce the above
|
12
|
+
// copyright notice, this list of conditions and the following disclaimer
|
13
|
+
// in the documentation and/or other materials provided with the
|
14
|
+
// distribution.
|
15
|
+
// * Neither the name of Google Inc. nor the names of its
|
16
|
+
// contributors may be used to endorse or promote products derived from
|
17
|
+
// this software without specific prior written permission.
|
18
|
+
//
|
19
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
// Wrappers for primitive (non-message) types. These types are useful
|
32
|
+
// for embedding primitives in the `google.protobuf.Any` type and for places
|
33
|
+
// where we need to distinguish between the absence of a primitive
|
34
|
+
// typed field and its default value.
|
35
|
+
|
36
|
+
syntax = "proto3";
|
37
|
+
|
38
|
+
package google.protobuf;
|
39
|
+
|
40
|
+
option java_multiple_files = true;
|
41
|
+
option java_outer_classname = "WrappersProto";
|
42
|
+
option java_package = "com.google.protobuf";
|
43
|
+
option csharp_namespace = "Google.Protobuf.WellKnownTypes";
|
44
|
+
option objc_class_prefix = "GPB";
|
45
|
+
|
46
|
+
|
47
|
+
// Wrapper message for double.
|
48
|
+
message DoubleValue {
|
49
|
+
// The double value.
|
50
|
+
double value = 1;
|
51
|
+
}
|
52
|
+
|
53
|
+
// Wrapper message for float.
|
54
|
+
message FloatValue {
|
55
|
+
// The float value.
|
56
|
+
float value = 1;
|
57
|
+
}
|
58
|
+
|
59
|
+
// Wrapper message for int64.
|
60
|
+
message Int64Value {
|
61
|
+
// The int64 value.
|
62
|
+
int64 value = 1;
|
63
|
+
}
|
64
|
+
|
65
|
+
// Wrapper message for uint64.
|
66
|
+
message UInt64Value {
|
67
|
+
// The uint64 value.
|
68
|
+
uint64 value = 1;
|
69
|
+
}
|
70
|
+
|
71
|
+
// Wrapper message for int32.
|
72
|
+
message Int32Value {
|
73
|
+
// The int32 value.
|
74
|
+
int32 value = 1;
|
75
|
+
}
|
76
|
+
|
77
|
+
// Wrapper message for uint32.
|
78
|
+
message UInt32Value {
|
79
|
+
// The uint32 value.
|
80
|
+
uint32 value = 1;
|
81
|
+
}
|
82
|
+
|
83
|
+
// Wrapper message for bool.
|
84
|
+
message BoolValue {
|
85
|
+
// The bool value.
|
86
|
+
bool value = 1;
|
87
|
+
}
|
88
|
+
|
89
|
+
// Wrapper message for string.
|
90
|
+
message StringValue {
|
91
|
+
// The string value.
|
92
|
+
string value = 1;
|
93
|
+
}
|
94
|
+
|
95
|
+
// Wrapper message for bytes.
|
96
|
+
message BytesValue {
|
97
|
+
// The bytes value.
|
98
|
+
bytes value = 1;
|
99
|
+
}
|
@@ -1,5 +1,6 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
syntax = "proto3";
|
2
|
+
|
3
|
+
package protip.messages;
|
3
4
|
|
4
5
|
message Errors {
|
5
6
|
repeated string messages = 1;
|
@@ -7,6 +8,6 @@ message Errors {
|
|
7
8
|
}
|
8
9
|
|
9
10
|
message FieldError {
|
10
|
-
|
11
|
-
|
11
|
+
string field = 1;
|
12
|
+
string message = 2;
|
12
13
|
}
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
2
|
+
# source: google/protobuf/wrappers.proto
|
3
|
+
|
4
|
+
require 'google/protobuf'
|
5
|
+
|
6
|
+
Google::Protobuf::DescriptorPool.generated_pool.build do
|
7
|
+
add_message "google.protobuf.DoubleValue" do
|
8
|
+
optional :value, :double, 1
|
9
|
+
end
|
10
|
+
add_message "google.protobuf.FloatValue" do
|
11
|
+
optional :value, :float, 1
|
12
|
+
end
|
13
|
+
add_message "google.protobuf.Int64Value" do
|
14
|
+
optional :value, :int64, 1
|
15
|
+
end
|
16
|
+
add_message "google.protobuf.UInt64Value" do
|
17
|
+
optional :value, :uint64, 1
|
18
|
+
end
|
19
|
+
add_message "google.protobuf.Int32Value" do
|
20
|
+
optional :value, :int32, 1
|
21
|
+
end
|
22
|
+
add_message "google.protobuf.UInt32Value" do
|
23
|
+
optional :value, :uint32, 1
|
24
|
+
end
|
25
|
+
add_message "google.protobuf.BoolValue" do
|
26
|
+
optional :value, :bool, 1
|
27
|
+
end
|
28
|
+
add_message "google.protobuf.StringValue" do
|
29
|
+
optional :value, :string, 1
|
30
|
+
end
|
31
|
+
add_message "google.protobuf.BytesValue" do
|
32
|
+
optional :value, :string, 1
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module Google
|
37
|
+
module Protobuf
|
38
|
+
DoubleValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.DoubleValue").msgclass
|
39
|
+
FloatValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.FloatValue").msgclass
|
40
|
+
Int64Value = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Int64Value").msgclass
|
41
|
+
UInt64Value = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.UInt64Value").msgclass
|
42
|
+
Int32Value = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Int32Value").msgclass
|
43
|
+
UInt32Value = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.UInt32Value").msgclass
|
44
|
+
BoolValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.BoolValue").msgclass
|
45
|
+
StringValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.StringValue").msgclass
|
46
|
+
BytesValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.BytesValue").msgclass
|
47
|
+
end
|
48
|
+
end
|
data/lib/protip/client.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support/concern'
|
1
2
|
require 'protip/error'
|
2
3
|
require 'net/http'
|
3
4
|
|
@@ -23,7 +24,7 @@ module Protip
|
|
23
24
|
uri = URI.join base_uri, path
|
24
25
|
|
25
26
|
request = method.new uri
|
26
|
-
request.body = (message
|
27
|
+
request.body = (message ? message.class.encode(message) : nil)
|
27
28
|
request['Accept'] = 'application/x-protobuf'
|
28
29
|
request.content_type = 'application/x-protobuf'
|
29
30
|
|
@@ -45,9 +46,7 @@ module Protip
|
|
45
46
|
if response_type
|
46
47
|
begin
|
47
48
|
response_type.decode response.body
|
48
|
-
|
49
|
-
# We may be able to remove it if we switch to a different protobuf gem.
|
50
|
-
rescue StandardError, NotImplementedError => error
|
49
|
+
rescue StandardError => error
|
51
50
|
raise ::Protip::ParseError.new error, request, response
|
52
51
|
end
|
53
52
|
else
|
data/lib/protip/error.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
#
|
2
|
-
require 'protobuf'
|
1
|
+
# Runtime exceptions for the library (protip/messages/errors defines the messages backing resource errors).
|
3
2
|
|
4
|
-
require 'protip/messages/errors
|
3
|
+
require 'protip/messages/errors'
|
5
4
|
|
6
5
|
module Protip
|
7
6
|
class Error < RuntimeError
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
2
|
+
# source: protip/messages/array.proto
|
3
|
+
|
4
|
+
require 'google/protobuf'
|
5
|
+
|
6
|
+
Google::Protobuf::DescriptorPool.generated_pool.build do
|
7
|
+
add_message "protip.messages.Array" do
|
8
|
+
repeated :messages, :string, 1
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module Protip
|
13
|
+
module Messages
|
14
|
+
Array = Google::Protobuf::DescriptorPool.generated_pool.lookup("protip.messages.Array").msgclass
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
2
|
+
# source: protip/messages/errors.proto
|
3
|
+
|
4
|
+
require 'google/protobuf'
|
5
|
+
|
6
|
+
Google::Protobuf::DescriptorPool.generated_pool.build do
|
7
|
+
add_message "protip.messages.Errors" do
|
8
|
+
repeated :messages, :string, 1
|
9
|
+
repeated :field_errors, :message, 2, "protip.messages.FieldError"
|
10
|
+
end
|
11
|
+
add_message "protip.messages.FieldError" do
|
12
|
+
optional :field, :string, 1
|
13
|
+
optional :message, :string, 2
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module Protip
|
18
|
+
module Messages
|
19
|
+
Errors = Google::Protobuf::DescriptorPool.generated_pool.lookup("protip.messages.Errors").msgclass
|
20
|
+
FieldError = Google::Protobuf::DescriptorPool.generated_pool.lookup("protip.messages.FieldError").msgclass
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
2
|
+
# source: protip/messages/types.proto
|
3
|
+
|
4
|
+
require 'google/protobuf'
|
5
|
+
|
6
|
+
Google::Protobuf::DescriptorPool.generated_pool.build do
|
7
|
+
add_message "protip.messages.Date" do
|
8
|
+
optional :year, :int64, 1
|
9
|
+
optional :month, :uint32, 2
|
10
|
+
optional :day, :uint32, 3
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module Protip
|
15
|
+
module Messages
|
16
|
+
Date = Google::Protobuf::DescriptorPool.generated_pool.lookup("protip.messages.Date").msgclass
|
17
|
+
end
|
18
|
+
end
|
data/lib/protip/resource.rb
CHANGED
@@ -19,7 +19,7 @@ require 'protip/error'
|
|
19
19
|
require 'protip/standard_converter'
|
20
20
|
require 'protip/wrapper'
|
21
21
|
|
22
|
-
require 'protip/messages/array
|
22
|
+
require 'protip/messages/array'
|
23
23
|
|
24
24
|
module Protip
|
25
25
|
module Resource
|
@@ -141,26 +141,28 @@ module Protip
|
|
141
141
|
attr_accessor :client
|
142
142
|
|
143
143
|
attr_reader :message
|
144
|
-
attr_reader :converter
|
145
144
|
|
146
145
|
attr_writer :base_path
|
147
146
|
def base_path
|
148
147
|
@base_path == nil ? raise(RuntimeError.new 'Base path not yet set') : @base_path.gsub(/\/$/, '')
|
149
148
|
end
|
150
149
|
|
150
|
+
attr_writer :converter
|
151
|
+
def converter
|
152
|
+
@converter || (@_standard_converter ||= Protip::StandardConverter.new)
|
153
|
+
end
|
154
|
+
|
151
155
|
private
|
152
156
|
|
153
157
|
# Primary entry point for defining resourceful behavior.
|
154
|
-
def resource(actions:, message:, query: nil
|
158
|
+
def resource(actions:, message:, query: nil)
|
155
159
|
if @message
|
156
160
|
raise RuntimeError.new('Only one call to `resource` is allowed')
|
157
161
|
end
|
158
162
|
|
159
|
-
@converter = converter
|
160
|
-
|
161
163
|
# Define attribute readers/writers
|
162
164
|
@message = message
|
163
|
-
@message.
|
165
|
+
@message.descriptor.each do |field|
|
164
166
|
def_delegator :@wrapper, :"#{field.name}"
|
165
167
|
def_delegator :@wrapper, :"#{field.name}="
|
166
168
|
end
|
@@ -269,13 +271,13 @@ module Protip
|
|
269
271
|
end
|
270
272
|
|
271
273
|
def persisted?
|
272
|
-
|
274
|
+
id != nil
|
273
275
|
end
|
274
276
|
|
275
277
|
def attributes
|
276
278
|
# Like `.as_json`, but includes nil fields to match ActiveRecord behavior.
|
277
|
-
self.class.message.
|
278
|
-
hash[attribute_name] =
|
279
|
+
self.class.message.descriptor.map{|field| field.name}.inject({}) do |hash, attribute_name|
|
280
|
+
hash[attribute_name] = public_send(attribute_name)
|
279
281
|
hash
|
280
282
|
end
|
281
283
|
end
|
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'protip/converter'
|
2
2
|
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
require 'protip/messages/wrappers.pb'
|
3
|
+
require 'protip/messages/types'
|
4
|
+
require 'google/protobuf'
|
6
5
|
|
7
6
|
module Protip
|
8
7
|
class StandardConverter
|
@@ -14,62 +13,34 @@ module Protip
|
|
14
13
|
@conversions = {}
|
15
14
|
|
16
15
|
## Protip types
|
17
|
-
@conversions[
|
16
|
+
@conversions['protip.messages.Date'] = {
|
18
17
|
to_object: ->(message) { ::Date.new(message.year, message.month, message.day) },
|
19
|
-
to_message: lambda do |date|
|
18
|
+
to_message: lambda do |date, message_class|
|
20
19
|
raise ArgumentError unless date.is_a?(::Date)
|
21
|
-
|
20
|
+
message_class.new year: date.year, month: date.month, day: date.day
|
22
21
|
end
|
23
22
|
}
|
24
23
|
|
25
24
|
## Standard wrappers
|
26
|
-
|
27
|
-
@conversions[
|
25
|
+
%w(Int64 Int32 UInt64 UInt32 Double Float Bool String Bytes).map{|type| "google.protobuf.#{type}Value"}.each do |name|
|
26
|
+
@conversions[name] = {
|
28
27
|
to_object: ->(message) { message.value },
|
29
|
-
to_message: lambda do |
|
30
|
-
|
31
|
-
message_class.new value: integer
|
32
|
-
end
|
33
|
-
}
|
34
|
-
end
|
35
|
-
[Protip::DoubleValue, Protip::FloatValue].each do |message_class|
|
36
|
-
@conversions[message_class] = {
|
37
|
-
to_object: ->(message) { message.value },
|
38
|
-
to_message: lambda do |float|
|
39
|
-
raise ArgumentError unless float.is_a?(Float)
|
40
|
-
message_class.new value: float
|
41
|
-
end
|
42
|
-
}
|
43
|
-
end
|
44
|
-
[Protip::BoolValue].each do |message_class|
|
45
|
-
@conversions[message_class] = {
|
46
|
-
to_object: ->(message) { message.value },
|
47
|
-
to_message: lambda do |bool|
|
48
|
-
# Protobuf throws a type error if this isn't the correct type, so we don't need to check
|
49
|
-
message_class.new value: bool
|
50
|
-
end
|
51
|
-
}
|
52
|
-
end
|
53
|
-
[Protip::StringValue, Protip::BytesValue].each do |message_class|
|
54
|
-
@conversions[message_class] = {
|
55
|
-
to_object: ->(message) { message.value },
|
56
|
-
to_message: lambda do |string|
|
57
|
-
# Protobuf throws a type error if this isn't the correct type, so we don't need to check
|
58
|
-
message_class.new value: string
|
28
|
+
to_message: lambda do |value, message_class|
|
29
|
+
message_class.new value: value
|
59
30
|
end
|
60
31
|
}
|
61
32
|
end
|
62
33
|
|
63
34
|
def convertible?(message_class)
|
64
|
-
self.class.conversions.has_key?(message_class)
|
35
|
+
self.class.conversions.has_key?(message_class.descriptor.name)
|
65
36
|
end
|
66
37
|
|
67
38
|
def to_object(message)
|
68
|
-
self.class.conversions[message.class][:to_object].call(message)
|
39
|
+
self.class.conversions[message.class.descriptor.name][:to_object].call(message)
|
69
40
|
end
|
70
41
|
|
71
42
|
def to_message(object, message_class)
|
72
|
-
self.class.conversions[message_class][:to_message].call(object)
|
43
|
+
self.class.conversions[message_class.descriptor.name][:to_message].call(object, message_class)
|
73
44
|
end
|
74
45
|
end
|
75
46
|
end
|