File: json_representation.rb

package info (click to toggle)
ruby-google-apis-core 0.11.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: ruby: 1,964; makefile: 4
file content (153 lines) | stat: -rw-r--r-- 5,308 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'representable/json'
require 'representable/json/hash'
require 'base64'
require 'date'

module Google
  module Apis
    module Core
      # Support for serializing hashes + property value/nil/unset tracking
      # To be included in representers as a feature.
      # @private
      module JsonRepresentationSupport
        def self.included(base)
          base.extend(JsonSupport)
        end

        # @private
        module JsonSupport
          # Returns a customized getter function for Representable. Allows
          # indifferent hash/attribute access.
          #
          # @param [String] name Property name
          # @return [Proc]
          def getter_fn(name)
            ivar_name = "@#{name}".to_sym
            lambda do |_|
              if respond_to?(:fetch)
                fetch(name, instance_variable_get(ivar_name))
              else
                instance_variable_get(ivar_name)
              end
            end
          end

          # Returns a customized function for Representable that checks whether or not
          # an attribute should be serialized. Allows proper patch semantics by distinguishing
          # between nil & unset values
          #
          # @param [String] name Property name
          # @return [Proc]
          def if_fn(name)
            ivar_name = "@#{name}".to_sym
            lambda do |opts|
              if opts[:user_options] && opts[:user_options][:skip_undefined]
                if respond_to?(:key?)
                  self.key?(name) || instance_variable_defined?(ivar_name)
                else
                  instance_variable_defined?(ivar_name)
                end
              else
                true
              end
            end
          end

          def set_default_options(name, options)
            if options[:base64]
              options[:render_filter] = ->(value, _doc, *_args) { value.nil? ? nil : Base64.urlsafe_encode64(value) }
              options[:parse_filter] = ->(fragment, _doc, *_args) { Base64.urlsafe_decode64(fragment) }
            end
            if options[:numeric_string]
              options[:render_filter] = ->(value, _doc, *_args) { value.nil? ? nil : value.to_s}
              options[:parse_filter] = ->(fragment, _doc, *_args) { fragment.to_i }
            end
            if options[:type] == DateTime
              options[:render_filter] = ->(value, _doc, *_args) { value.nil? ? nil : value.is_a?(DateTime) ? value.rfc3339(3) : value.to_s }
              options[:parse_filter] = ->(fragment, _doc, *_args) { DateTime.parse(fragment) }
            end
            if options[:type] == Date
              options[:render_filter] = ->(value, _doc, *_args) { value.nil? ? nil : value.to_s}
              options[:parse_filter] = ->(fragment, _doc, *_args) { Date.parse(fragment) }
            end

            options[:render_nil] = true
            options[:getter] = getter_fn(name)
            options[:if] = if_fn(name)
          end

          # Define a single value property
          #
          # @param [String] name
          #  Property name
          # @param [Hash] options
          def property(name, options = {})
            set_default_options(name, options)
            super(name, options)
          end

          # Define a collection property
          #
          # @param [String] name
          #  Property name
          # @param [Hash] options
          def collection(name, options = {})
            set_default_options(name, options)
            super(name, options)
          end

          # Define a hash property
          #
          # @param [String] name
          #  Property name
          # @param [Hash] options
          def hash(name = nil, options = nil)
            return super() unless name # Allow Object.hash
            set_default_options(name, options)
            super(name, options)
          end
        end
      end

      # Base decorator for JSON representers
      #
      # @see https://github.com/apotonick/representable
      class JsonRepresentation < Representable::Decorator
        include Representable::JSON
        feature JsonRepresentationSupport
      end

      module JsonObjectSupport
        def self.included(base)
          base.extend(ClassMethods)
        end

        module ClassMethods
          def from_json(json)
            representation = self.const_get(:Representation)
            representation.new(self.new).from_json(json, unwrap: self)
          end
        end

        def to_json(*a)
          representation = self.class.const_get(:Representation)
          representation.new(self).to_hash(user_options: { skip_undefined: true }).to_json(*a)
        end
      end
    end
  end
end