form_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +37 -0
  3. data/.rspec +2 -0
  4. data/.swagger-codegen-ignore +31 -0
  5. data/.swagger-codegen/VERSION +1 -0
  6. data/.swagger-revision +1 -0
  7. data/CHANGELOG.md +2 -0
  8. data/Gemfile +7 -0
  9. data/LICENSE +7 -0
  10. data/README.md +76 -0
  11. data/Rakefile +11 -0
  12. data/examples/generate_pdf.rb +43 -0
  13. data/extensions/lib/form_api/api/client.rb +61 -0
  14. data/form_api.gemspec +51 -0
  15. data/lib/form_api.rb +45 -0
  16. data/lib/form_api/api/pdf_api.rb +133 -0
  17. data/lib/form_api/api_client.rb +389 -0
  18. data/lib/form_api/api_error.rb +38 -0
  19. data/lib/form_api/configuration.rb +209 -0
  20. data/lib/form_api/models/data.rb +193 -0
  21. data/lib/form_api/models/inline_response_201.rb +235 -0
  22. data/lib/form_api/models/inline_response_201_submission.rb +249 -0
  23. data/lib/form_api/models/inline_response_401.rb +240 -0
  24. data/lib/form_api/models/inline_response_422.rb +242 -0
  25. data/lib/form_api/version.rb +15 -0
  26. data/script/clean +5 -0
  27. data/script/fix_gemspec.rb +31 -0
  28. data/script/generate_language +35 -0
  29. data/script/post_generate_language +9 -0
  30. data/script/swagger +35 -0
  31. data/script/test +24 -0
  32. data/spec/api/pdf_api_spec.rb +60 -0
  33. data/spec/api_client_spec.rb +226 -0
  34. data/spec/configuration_spec.rb +42 -0
  35. data/spec/models/data_spec.rb +42 -0
  36. data/spec/models/inline_response_201_spec.rb +52 -0
  37. data/spec/models/inline_response_201_submission_spec.rb +58 -0
  38. data/spec/models/inline_response_401_spec.rb +52 -0
  39. data/spec/models/inline_response_422_spec.rb +52 -0
  40. data/spec/spec_helper.rb +111 -0
  41. data/swagger-config.json +13 -0
  42. metadata +313 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 16637dd3f1d35a14cb131ac8ebf6432498d67906
4
+ data.tar.gz: c99c0ad76de1990a41656289284b2dc32c8faf43
5
+ SHA512:
6
+ metadata.gz: 0cfef6e030171349458ef9c01d50464e26354da4fe13ab60e2123e276bbbc01ff1b9bb0b9350e88a77d4bbbf816f45b72b85cf0bab88502690f54fa4ec55dba4
7
+ data.tar.gz: 3bb5fc4eb9359d1077ee0e59e36cfec537a1386dd4aa48491619bb3c655b457c629c00dde923e9223fc1b0a9442862dc71459dee69fdec5c0dc4d5b5f434d6fd
data/.gitignore ADDED
@@ -0,0 +1,37 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+ .byebug_history
13
+
14
+ ## Documentation cache and generated files:
15
+ /.yardoc/
16
+ /_yardoc/
17
+ /doc/
18
+ /rdoc/
19
+
20
+ ## Environment normalization:
21
+ /.bundle/
22
+ /vendor/bundle
23
+ /lib/bundler/man/
24
+ Gemfile.lock
25
+ .ruby-version
26
+ .ruby-gemset
27
+ .rvmrc
28
+
29
+ ## Emacs files
30
+ *~
31
+
32
+ ## OSX files
33
+ .DS_Store
34
+ Thumbs.db
35
+
36
+ ## Ignore baked in swagger generator
37
+ swagger-codegen
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,31 @@
1
+ # Swagger Codegen Ignore
2
+ # Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
3
+
4
+ # Use this file to prevent files from being overwritten by the generator.
5
+ # The patterns follow closely to .gitignore or .dockerignore.
6
+
7
+ # As an example, the C# client generator defines ApiClient.cs.
8
+ # You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
9
+ #ApiClient.cs
10
+
11
+ # You can match any string of characters against a directory, file or extension with a single asterisk (*):
12
+ #foo/*/qux
13
+ # The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14
+
15
+ # You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16
+ #foo/**/qux
17
+ # This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18
+
19
+ # You can also negate patterns with an exclamation (!).
20
+ # For example, you can ignore all files in a docs folder with the file extension .md:
21
+ #docs/*.md
22
+ # Then explicitly reverse the ignore rule for a single file:
23
+ #!docs/README.md
24
+
25
+ README.md
26
+ CHANGELOG.md
27
+ git_push.sh
28
+ docs/
29
+ .gitignore
30
+ LICENSE
31
+ Rakefile
@@ -0,0 +1 @@
1
+ 2.3.0-SNAPSHOT
data/.swagger-revision ADDED
@@ -0,0 +1 @@
1
+ 28a5b74dbf313529a2fc697d46e9ed870cfd6a30
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ### 0.1.0 [October 3, 2017]
2
+ * Initial Release
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'rake', '~> 12.0.0'
7
+ end
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright © 2017 Form Services Ltd.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # FormAPI Ruby Client
2
+
3
+ This is a Ruby gem for using [FormAPI](https://formapi.io) to generate PDF documents from configured templates.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add the following to your `Gemfile`.
9
+
10
+ ```ruby
11
+ gem "form_api"
12
+ ```
13
+
14
+ Then run:
15
+
16
+ ```bash
17
+ bundle install
18
+ ```
19
+
20
+
21
+ ## Usage
22
+
23
+ See [examples](examples/) for runnable examples with file output, error handling, etc.
24
+
25
+ ```ruby
26
+ FormAPI.configure do |config|
27
+ config.username = "YOUR_API_KEY_HERE"
28
+ # config.debugging = true
29
+ end
30
+
31
+ formapi = FormAPI::Client.new
32
+
33
+ response = formapi.generate_pdf(
34
+ template_id: '<TEMPLATE_ID>', # ID of a template that you have configured
35
+ test: true, # Test documents are free but watermarked
36
+ template_data: { # Data to fill in the template
37
+ name: "foo",
38
+ number: 42
39
+ },
40
+ key: "<CUSTOM_KEY>", # Helps you find a PDF later
41
+ )
42
+ ```
43
+
44
+ This submits a PDF request and waits for the job to finish.
45
+
46
+
47
+ ## More Help
48
+
49
+ See the FormAPI documentation for more information.
50
+
51
+ Please [email us](mailto:support@formapi.io) if you need help.
52
+
53
+
54
+ ## Development
55
+
56
+ The majority of the code in this repo is generated using swagger-codegen on [formapi.yaml](formapi.yaml). You can modify this file and regenerate the client using `script/generate_language ruby`.
57
+
58
+
59
+ ## Release Process
60
+
61
+ 1. Pull latest master
62
+ 2. Merge feature branch(es) into master
63
+ 3. `script/test`
64
+ 4. Increment version in code:
65
+ - `swagger-config.json`
66
+ - `lib/formapi/version.rb`
67
+ 5. Update [CHANGELOG.md](CHANGELOG.md)
68
+ 6. Commit "Release version vX.Y.Z"
69
+ 7. `rake release`
70
+ 8. Verify package release at https://rubygems.org/gems/formapi
71
+ 9. Refresh documentation on formapi.io
72
+
73
+
74
+ ## Version Policy
75
+
76
+ This library follows [Semantic Versioning 2.0.0](http://semver.org).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ require 'bundler/gem_tasks'
3
+
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ task default: :spec
9
+ rescue LoadError
10
+ # no rspec available
11
+ end
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # This example demonstrates generating a PDF from a preconfigured template,
3
+ # and downloading the PDF to a local file.
4
+ #
5
+ # You can run this example with: ./examples/generate_pdf.rb
6
+
7
+ require "bundler/setup"
8
+ Bundler.require
9
+
10
+ PDF_FILENAME = '/tmp/formapi-test.pdf'
11
+
12
+ begin
13
+ FormAPI.configure do |c|
14
+ c.username = "yRaaR9JmTPtGX7EN" # Your API Token ID
15
+ c.password = "IB3TRkSdm4f2BdtU_D3YgxjdMB7l-r2fOgvxD1Yzwec" # Your API Token Secret
16
+ # c.debugging = true
17
+ end
18
+
19
+ formapi = FormAPI::Client.new
20
+
21
+ puts "Downloading PDF to /tmp/formapi-test.pdf..."
22
+
23
+ formapi.generate_and_download_pdf(
24
+ template_id: '6zz3dYRYM67fxMXA',
25
+ filename: PDF_FILENAME,
26
+ data: {
27
+ first_name: 'John',
28
+ last_name: 'Smith',
29
+ favorite_color: 'Blue'
30
+ }
31
+ )
32
+
33
+ puts "PDF was downloaded to /tmp/formapi-test.pdf"
34
+
35
+ # Open the PDF on Mac or Linux.
36
+ `type xdg-open > /dev/null 2>&1 && xdg-open '#{PDF_FILENAME}' || open '#{PDF_FILENAME}'`
37
+
38
+ rescue FormAPI::ApiError => ex
39
+ puts "#{ex.class}: #{ex.message}"
40
+ puts ex.code # HTTP response code
41
+ puts ex.response_body # HTTP response body
42
+ puts ex.backtrace[0..3].join("\n")
43
+ end
@@ -0,0 +1,61 @@
1
+ require 'form_api/api/pdf_api'
2
+
3
+ # The original 'lib' files are overriden every time we
4
+ # regenerate the client, so custom methods go in here.
5
+ # Also I want to call it "Client".
6
+
7
+ module FormAPI
8
+ class Client < PDFApi
9
+ class InvalidDataError < ApiError; end
10
+ class PollTimeoutError < ApiError; end
11
+ class InvalidResponseError < ApiError; end
12
+
13
+ def generate_pdf(template_id, opts = {})
14
+ unless opts[:data].kind_of?(::Hash)
15
+ raise InvalidDataError, ":data is required, and must be a Hash."
16
+ end
17
+
18
+ # FormAPI requires a nested :data object.
19
+ opts[:data] = { data: opts.delete(:data) }
20
+
21
+ response = super(template_id, opts)
22
+ return response unless opts[:wait]
23
+
24
+ timeout = opts[:timeout] || 60
25
+ start_time = Time.now
26
+
27
+ submission = response.submission
28
+
29
+ # Wait for submission to be ready
30
+ while submission.state != 'processed'
31
+ sleep 1
32
+ submission = get_submission(submission.id)
33
+
34
+ if Time.now - start_time > timeout
35
+ raise PollTimeoutError, "PDF was not ready after #{timeout} seconds!"
36
+ end
37
+ end
38
+
39
+ return InlineResponse201.new(
40
+ status: submission.state == 'processed' ? 'success' : 'error',
41
+ submission: submission
42
+ )
43
+ end
44
+
45
+ def generate_and_download_pdf(opts = {})
46
+ template_id = opts.delete :template_id
47
+ filename = opts.delete :filename
48
+
49
+ response = generate_pdf(template_id, opts.merge(wait: true))
50
+ submission = response.submission
51
+
52
+ pdf_response = Typhoeus.get(submission.download_url, followlocation: true)
53
+
54
+ File.open(filename, 'wb') do |f|
55
+ f.write pdf_response.body
56
+ end
57
+
58
+ response
59
+ end
60
+ end
61
+ end
data/form_api.gemspec ADDED
@@ -0,0 +1,51 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ =begin
4
+ #API V1
5
+
6
+ #No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
7
+
8
+ OpenAPI spec version: v1
9
+
10
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
11
+ Swagger Codegen version: 2.3.0-SNAPSHOT
12
+
13
+ =end
14
+
15
+ $:.push File.expand_path("../lib", __FILE__)
16
+ require "form_api/version"
17
+
18
+ Gem::Specification.new do |s|
19
+ s.name = "form_api"
20
+ s.version = FormAPI::VERSION
21
+ s.platform = Gem::Platform::RUBY
22
+ s.authors = ["Form Services Ltd."]
23
+ s.email = ["support@formapi.io"]
24
+ s.homepage = "https://github.com/FormAPI/formapi-ruby"
25
+ s.summary = "A client library for the FormAPI PDF generation service"
26
+ s.description = "A client library for the FormAPI PDF generation service"
27
+ s.license = "MIT"
28
+ s.required_ruby_version = ">= 1.9"
29
+
30
+ s.add_runtime_dependency 'typhoeus', '~> 1.0', '>= 1.0.1'
31
+ s.add_runtime_dependency 'json', '~> 2.1', '>= 2.1.0'
32
+
33
+ s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'
34
+ s.add_development_dependency 'vcr', '~> 3.0', '>= 3.0.1'
35
+ s.add_development_dependency 'webmock', '~> 1.24', '>= 1.24.3'
36
+ s.add_development_dependency 'autotest', '~> 4.4', '>= 4.4.6'
37
+ s.add_development_dependency 'autotest-rails-pure', '~> 4.1', '>= 4.1.2'
38
+ s.add_development_dependency 'autotest-growl', '~> 0.2', '>= 0.2.16'
39
+ s.add_development_dependency 'autotest-fsevent', '~> 0.2', '>= 0.2.12'
40
+
41
+ # added by script/fix_gemspec.rb.
42
+ s.add_development_dependency 'rake', '~>11.2', '>= 11.2.2'
43
+ s.add_development_dependency 'pry', '~>0.10', '>= 0.10.4'
44
+ # </added> : if the above lines are missing in the gemspec, then
45
+ # the matcher for autotest is probably broken
46
+
47
+ s.files = `git ls-files`.split("\n").uniq.sort.select{|f| !f.empty? }
48
+ s.test_files = `git ls-files spec test`.split("\n")
49
+ s.executables = []
50
+ s.require_paths = ["lib"]
51
+ end
data/lib/form_api.rb ADDED
@@ -0,0 +1,45 @@
1
+ =begin
2
+ #API V1
3
+
4
+ #No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
5
+
6
+ OpenAPI spec version: v1
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+ Swagger Codegen version: 2.3.0-SNAPSHOT
10
+
11
+ =end
12
+
13
+ # Common files
14
+ require 'form_api/api_client'
15
+ require 'form_api/api_error'
16
+ require 'form_api/version'
17
+ require 'form_api/configuration'
18
+
19
+ # Models
20
+ require 'form_api/models/data'
21
+ require 'form_api/models/inline_response_201'
22
+ require 'form_api/models/inline_response_201_submission'
23
+ require 'form_api/models/inline_response_401'
24
+ require 'form_api/models/inline_response_422'
25
+
26
+ # APIs
27
+ require File.join(File.dirname(__FILE__), '../extensions/lib/form_api/api/client')
28
+
29
+ module FormAPI
30
+ class << self
31
+ # Customize default settings for the SDK using block.
32
+ # FormAPI.configure do |config|
33
+ # config.username = "xxx"
34
+ # config.password = "xxx"
35
+ # end
36
+ # If no block given, return the default Configuration object.
37
+ def configure
38
+ if block_given?
39
+ yield(Configuration.default)
40
+ else
41
+ Configuration.default
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,133 @@
1
+ =begin
2
+ #API V1
3
+
4
+ #No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
5
+
6
+ OpenAPI spec version: v1
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+ Swagger Codegen version: 2.3.0-SNAPSHOT
10
+
11
+ =end
12
+
13
+ require "uri"
14
+
15
+ module FormAPI
16
+ class PDFApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+
23
+ # Generates a new PDF
24
+ #
25
+ # @param template_id
26
+ # @param [Hash] opts the optional parameters
27
+ # @option opts [Data] :data
28
+ # @return [InlineResponse201]
29
+ def generate_pdf(template_id, opts = {})
30
+ data, _status_code, _headers = generate_pdf_with_http_info(template_id, opts)
31
+ return data
32
+ end
33
+
34
+ # Generates a new PDF
35
+ #
36
+ # @param template_id
37
+ # @param [Hash] opts the optional parameters
38
+ # @option opts [Data] :data
39
+ # @return [Array<(InlineResponse201, Fixnum, Hash)>] InlineResponse201 data, response status code and response headers
40
+ def generate_pdf_with_http_info(template_id, opts = {})
41
+ if @api_client.config.debugging
42
+ @api_client.config.logger.debug "Calling API: PDFApi.generate_pdf ..."
43
+ end
44
+ # verify the required parameter 'template_id' is set
45
+ if @api_client.config.client_side_validation && template_id.nil?
46
+ fail ArgumentError, "Missing the required parameter 'template_id' when calling PDFApi.generate_pdf"
47
+ end
48
+ # resource path
49
+ local_var_path = "/templates/{template_id}/submissions".sub('{' + 'template_id' + '}', template_id.to_s)
50
+
51
+ # query parameters
52
+ query_params = {}
53
+
54
+ # header parameters
55
+ header_params = {}
56
+ # HTTP header 'Accept' (if needed)
57
+ header_params['Accept'] = @api_client.select_header_accept(['application/json'])
58
+ # HTTP header 'Content-Type'
59
+ header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])
60
+
61
+ # form parameters
62
+ form_params = {}
63
+
64
+ # http body (model)
65
+ post_body = @api_client.object_to_http_body(opts[:'data'])
66
+ auth_names = ['basic']
67
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path,
68
+ :header_params => header_params,
69
+ :query_params => query_params,
70
+ :form_params => form_params,
71
+ :body => post_body,
72
+ :auth_names => auth_names,
73
+ :return_type => 'InlineResponse201')
74
+ if @api_client.config.debugging
75
+ @api_client.config.logger.debug "API called: PDFApi#generate_pdf\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
76
+ end
77
+ return data, status_code, headers
78
+ end
79
+
80
+ # Check the status of a PDF
81
+ #
82
+ # @param submission_id
83
+ # @param [Hash] opts the optional parameters
84
+ # @return [InlineResponse201Submission]
85
+ def get_submission(submission_id, opts = {})
86
+ data, _status_code, _headers = get_submission_with_http_info(submission_id, opts)
87
+ return data
88
+ end
89
+
90
+ # Check the status of a PDF
91
+ #
92
+ # @param submission_id
93
+ # @param [Hash] opts the optional parameters
94
+ # @return [Array<(InlineResponse201Submission, Fixnum, Hash)>] InlineResponse201Submission data, response status code and response headers
95
+ def get_submission_with_http_info(submission_id, opts = {})
96
+ if @api_client.config.debugging
97
+ @api_client.config.logger.debug "Calling API: PDFApi.get_submission ..."
98
+ end
99
+ # verify the required parameter 'submission_id' is set
100
+ if @api_client.config.client_side_validation && submission_id.nil?
101
+ fail ArgumentError, "Missing the required parameter 'submission_id' when calling PDFApi.get_submission"
102
+ end
103
+ # resource path
104
+ local_var_path = "/submissions/{submission_id}".sub('{' + 'submission_id' + '}', submission_id.to_s)
105
+
106
+ # query parameters
107
+ query_params = {}
108
+
109
+ # header parameters
110
+ header_params = {}
111
+ # HTTP header 'Accept' (if needed)
112
+ header_params['Accept'] = @api_client.select_header_accept(['application/json'])
113
+
114
+ # form parameters
115
+ form_params = {}
116
+
117
+ # http body (model)
118
+ post_body = nil
119
+ auth_names = ['basic']
120
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path,
121
+ :header_params => header_params,
122
+ :query_params => query_params,
123
+ :form_params => form_params,
124
+ :body => post_body,
125
+ :auth_names => auth_names,
126
+ :return_type => 'InlineResponse201Submission')
127
+ if @api_client.config.debugging
128
+ @api_client.config.logger.debug "API called: PDFApi#get_submission\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
129
+ end
130
+ return data, status_code, headers
131
+ end
132
+ end
133
+ end