File: utils.rb

package info (click to toggle)
ruby-app-store-connect 0.37.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 444 kB
  • sloc: ruby: 856; makefile: 4
file content (32 lines) | stat: -rw-r--r-- 837 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
# frozen_string_literal: true

module AppStoreConnect
  class Client
    class Utils
      def self.encode(hash)
        hash
          .deep_transform_keys { |s| s.to_s.camelize(:lower) }
          .to_json
      end

      # Right now this only supports gzip and json responses.
      # If you need to support a different type then add it.
      def self.decode(string, content_type = 'application/json')
        decoded_data = nil

        case content_type
        when 'application/a-gzip'
          sio = StringIO.new string
          gz = Zlib::GzipReader.new sio
          decoded_data = gz.read
        else # Assume JSON
          decoded_data = JSON
                         .parse(string)
                         .deep_transform_keys { |k| k.underscore.to_sym }
        end

        decoded_data
      end
    end
  end
end