ruby-anvil 0.0.1.pre.rc.1

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.
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ require "stringio"
5
+ require "tempfile"
6
+
7
+ module Anvil
8
+ # Finds the files in a GraphQL operation and checks they are somewhere the
9
+ # Anvil API will accept them.
10
+ #
11
+ # Ported from node-anvil's `src/validation.js` and its use of `extract-files`,
12
+ # so an operation that uploads in one client uploads in the other.
13
+ module Uploads
14
+ module_function
15
+
16
+ # Is this value something we should upload as a file rather than send as
17
+ # JSON? A String is not: node-anvil treats strings as paths only when they
18
+ # were handed to {Anvil.prepare_graphql_file}, and so do we.
19
+ #
20
+ # @param value [Object]
21
+ # @return [Boolean]
22
+ def file?(value)
23
+ value.is_a?(UploadWithOptions) ||
24
+ value.is_a?(IO) ||
25
+ value.is_a?(StringIO) ||
26
+ value.is_a?(Tempfile) ||
27
+ value.is_a?(Pathname)
28
+ end
29
+
30
+ # Replaces every file in the operation with null and records where it was,
31
+ # which is what the multipart request spec needs.
32
+ #
33
+ # @param operation [Hash] `{ query:, variables: }`
34
+ # @return [Array(Hash, Array<Array(UploadWithOptions, Array<String>)>)] the
35
+ # operation with files nulled out, and each upload with the paths it was
36
+ # found at
37
+ # @see https://github.com/jaydenseric/graphql-multipart-request-spec
38
+ def extract_files(operation)
39
+ found = {}.compare_by_identity
40
+ clone = walk(operation, nil, found)
41
+ [clone, found.values]
42
+ end
43
+
44
+ # Checks the places files appear. The API only accepts an upload as the
45
+ # `file` key of an object, never at the top level or under another name.
46
+ #
47
+ # @param schema [Object] the operation, or any part of it
48
+ # @param parent [Object, nil]
49
+ # @param key [Object, nil]
50
+ # @return [Boolean]
51
+ def valid_upload_schema?(schema, parent = nil, key = nil)
52
+ return true if schema.nil?
53
+ return valid_file_value?(schema, parent) if key.to_s == "file"
54
+
55
+ case schema
56
+ when Array then schema.all? { |sub| valid_upload_schema?(sub, schema) }
57
+ when Hash then schema.all? { |sub_key, sub| valid_upload_schema?(sub, schema, sub_key) }
58
+ else
59
+ !file?(schema)
60
+ end
61
+ end
62
+
63
+ # The value under a `file` key, which is where every kind of upload goes.
64
+ #
65
+ # @api private
66
+ def valid_file_value?(schema, parent)
67
+ return false unless parent.is_a?(Hash) && fetch_either(parent, :file).equal?(schema)
68
+ return file?(schema) unless schema.is_a?(Hash)
69
+
70
+ # A base64 upload: `{ file: { data:, filename:, mimetype: } }`
71
+ if fetch_either(schema, :data)
72
+ return fetch_either(schema, :data).is_a?(String) &&
73
+ %i[filename mimetype].all? { |required| fetch_either(schema, required) }
74
+ end
75
+
76
+ # A URL upload, which Anvil fetches itself: `{ file: { url: } }`.
77
+ # node-anvil rejects this shape; the API takes it, so we allow it.
78
+ return fetch_either(schema, :url).is_a?(String) if fetch_either(schema, :url)
79
+
80
+ false
81
+ end
82
+
83
+ # @api private
84
+ def walk(value, path, found)
85
+ if file?(value)
86
+ record(value, path, found)
87
+ return nil
88
+ end
89
+
90
+ case value
91
+ when Hash
92
+ value.each_with_object({}) do |(key, sub), clone|
93
+ clone[key] = walk(sub, join(path, key), found)
94
+ end
95
+ when Array
96
+ value.each_with_index.map { |sub, index| walk(sub, join(path, index), found) }
97
+ else
98
+ value
99
+ end
100
+ end
101
+
102
+ # @api private
103
+ def record(value, path, found)
104
+ entry = found[value] ||= [value.is_a?(UploadWithOptions) ? value : Anvil.prepare_graphql_file(value), []]
105
+ entry.last << path
106
+ entry
107
+ end
108
+
109
+ # @api private
110
+ def join(path, key)
111
+ path ? "#{path}.#{key}" : key.to_s
112
+ end
113
+
114
+ # @api private
115
+ def fetch_either(hash, key)
116
+ hash[key.to_s] || hash[key.to_sym]
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anvil
4
+ VERSION = "0.0.1.pre.rc.1"
5
+ end
data/lib/anvil.rb ADDED
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "anvil/version"
4
+ require_relative "anvil/errors"
5
+ require_relative "anvil/auth"
6
+ require_relative "anvil/rate_limiter"
7
+ require_relative "anvil/upload_with_options"
8
+ require_relative "anvil/uploads"
9
+ require_relative "anvil/multipart"
10
+ require_relative "anvil/graphql"
11
+ require_relative "anvil/response"
12
+ require_relative "anvil/types/base"
13
+ require_relative "anvil/payloads/base"
14
+ # Generated from the API specs: every type, then the hand-written files reopen them
15
+ require_relative "anvil/types/generated/graphql"
16
+ require_relative "anvil/types/generated/operations"
17
+ require_relative "anvil/types/generated/rest"
18
+ require_relative "anvil/types/etch_packet"
19
+ require_relative "anvil/payloads/fill_pdf"
20
+ require_relative "anvil/payloads/generate_pdf"
21
+ require_relative "anvil/payloads/etch_signer"
22
+ require_relative "anvil/payloads/etch_file"
23
+ require_relative "anvil/payloads/create_etch_packet"
24
+ require_relative "anvil/payloads/forge_submit"
25
+ require_relative "anvil/client"
26
+
27
+ # The Anvil API client for Ruby.
28
+ #
29
+ # client = Anvil::Client.new(api_key: ENV.fetch("ANVIL_API_KEY"))
30
+ #
31
+ # @see https://www.useanvil.com/docs/api/getting-started
32
+ module Anvil
33
+ # The version number to pass as a PDF template's `version_number` to use the
34
+ # latest version, published or not. That is usually the current draft.
35
+ VERSION_LATEST = -1
36
+
37
+ # The version number to pass as a PDF template's `version_number` to use the
38
+ # latest published version. This is what the API uses when no version is
39
+ # given.
40
+ VERSION_LATEST_PUBLISHED = -2
41
+
42
+ # The response types the API's objects are returned as.
43
+ module Types; end
44
+
45
+ # Prepares a file for upload through the GraphQL API.
46
+ #
47
+ # file = Anvil.prepare_graphql_file("./contract.pdf")
48
+ # file = Anvil.prepare_graphql_file(File.open("./contract.pdf", "rb"))
49
+ # file = Anvil.prepare_graphql_file(StringIO.new(bytes), filename: "contract.pdf")
50
+ #
51
+ # Put the result wherever the API takes an `Upload`, which is always the
52
+ # `file` key of an object:
53
+ #
54
+ # client.create_etch_packet(variables: {
55
+ # files: [{ id: "contract", title: "Contract", file: file }]
56
+ # })
57
+ #
58
+ # The client uploads it as multipart, per the GraphQL multipart request spec.
59
+ # This is the Ruby equivalent of node-anvil's `Anvil.prepareGraphQLFile()`.
60
+ #
61
+ # @param file [String, Pathname, IO, StringIO, Tempfile] a path to a file, or
62
+ # an open IO holding its bytes
63
+ # @param filename [String] the name to upload it under. Required for an IO
64
+ # that has no path of its own, since there is nothing to derive a name from
65
+ # @param mimetype [String] the content type to upload it under. Guessed from
66
+ # the filename's extension otherwise
67
+ # @param ignore_filename_validation [Boolean] send an IO with no derivable
68
+ # filename anyway
69
+ # @param options [Hash] any other options, kept on the returned object
70
+ # @return [UploadWithOptions]
71
+ # @raise [ConfigurationError] when the path does not exist, or a filename
72
+ # could not be worked out
73
+ # @see https://github.com/jaydenseric/graphql-multipart-request-spec
74
+ def self.prepare_graphql_file(file, filename: nil, mimetype: nil, ignore_filename_validation: false, **options)
75
+ upload = UploadWithOptions.new(
76
+ file,
77
+ options.merge(
78
+ filename: filename,
79
+ mimetype: mimetype,
80
+ ignore_filename_validation: ignore_filename_validation
81
+ ).compact
82
+ )
83
+
84
+ if upload.path?
85
+ raise ConfigurationError, "No file at #{file.to_s.inspect} to upload" unless File.file?(file.to_s)
86
+ elsif !file.respond_to?(:read)
87
+ raise ConfigurationError,
88
+ "prepare_graphql_file takes a path or an IO, not a #{file.class}. " \
89
+ "To upload bytes you already have, wrap them in a StringIO and pass a filename."
90
+ elsif !ignore_filename_validation && (upload.filename.nil? || upload.filename.empty?)
91
+ raise ConfigurationError,
92
+ "When passing a #{file.class} to prepare_graphql_file, `filename:` must be provided. " \
93
+ "If you think you can ignore this, pass `ignore_filename_validation: true`."
94
+ end
95
+
96
+ upload
97
+ end
98
+ end
data/lib/ruby-anvil.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The gem is named ruby-anvil and the library is named Anvil, so both
4
+ # `require "anvil"` and `require "ruby-anvil"` work.
5
+ require_relative "anvil"
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-anvil
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre.rc.1
5
+ platform: ruby
6
+ authors:
7
+ - Anvil Foundry Inc.
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Fill PDFs, generate PDFs from HTML or markdown, send documents for e-signature,
13
+ and run Workflows with the Anvil API.
14
+ email:
15
+ - support@useanvil.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE.txt
22
+ - README.md
23
+ - lib/anvil.rb
24
+ - lib/anvil/auth.rb
25
+ - lib/anvil/client.rb
26
+ - lib/anvil/errors.rb
27
+ - lib/anvil/graphql.rb
28
+ - lib/anvil/multipart.rb
29
+ - lib/anvil/payloads/base.rb
30
+ - lib/anvil/payloads/create_etch_packet.rb
31
+ - lib/anvil/payloads/etch_file.rb
32
+ - lib/anvil/payloads/etch_signer.rb
33
+ - lib/anvil/payloads/fill_pdf.rb
34
+ - lib/anvil/payloads/forge_submit.rb
35
+ - lib/anvil/payloads/generate_pdf.rb
36
+ - lib/anvil/rate_limiter.rb
37
+ - lib/anvil/response.rb
38
+ - lib/anvil/types/base.rb
39
+ - lib/anvil/types/etch_packet.rb
40
+ - lib/anvil/types/generated/graphql.rb
41
+ - lib/anvil/types/generated/operations.rb
42
+ - lib/anvil/types/generated/rest.rb
43
+ - lib/anvil/upload_with_options.rb
44
+ - lib/anvil/uploads.rb
45
+ - lib/anvil/version.rb
46
+ - lib/ruby-anvil.rb
47
+ homepage: https://github.com/anvilco/ruby-anvil
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://github.com/anvilco/ruby-anvil
52
+ bug_tracker_uri: https://github.com/anvilco/ruby-anvil/issues
53
+ changelog_uri: https://github.com/anvilco/ruby-anvil/blob/master/CHANGELOG.md
54
+ documentation_uri: https://www.useanvil.com/docs/api/getting-started
55
+ rubygems_mfa_required: 'true'
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 3.1.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 4.0.23
71
+ specification_version: 4
72
+ summary: Anvil API client for Ruby
73
+ test_files: []