Class: Paubox::FormsClient
- Inherits:
-
Object
- Object
- Paubox::FormsClient
- Defined in:
- lib/paubox/forms_client.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- FORMS_HOST =
'api.paubox.com'- FORMS_PROTOCOL =
'https://'- FORMS_BASE =
'/forms'- TIMEOUT_SECONDS =
30- UUID_RE =
/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i- LIST_FORMS_PARAMS =
%i[customer_id form_id search order order_by archived active page items].freeze
- LIST_SUBMISSIONS_PARAMS =
%i[submission_id order_by order page items].freeze
Instance Method Summary collapse
- #archive_form(form_id) ⇒ Object
- #copy_form(form_id, title:) ⇒ Object
- #create_form(attrs) ⇒ Object
- #find_form(form_id) ⇒ Object
- #form_stats(customer_id: nil) ⇒ Object
- #get_form(form_id) ⇒ Object
-
#initialize(args = {}) ⇒ FormsClient
constructor
A new instance of FormsClient.
- #list_forms(params = {}) ⇒ Object
- #list_submissions(form_id, params = {}) ⇒ Object
- #submission_pdf(form_id, submission_id) ⇒ Object
- #submissions_csv(form_id, submission_id: nil) ⇒ Object
- #submit_form(form_id, form_data:, attachments: []) ⇒ Object
- #unarchive_form(form_id) ⇒ Object
- #update_form(form_id, attrs) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ FormsClient
Returns a new instance of FormsClient.
34 35 36 37 38 39 40 |
# File 'lib/paubox/forms_client.rb', line 34 def initialize(args = {}) @host = args[:host] || FORMS_HOST @protocol = args[:protocol] || FORMS_PROTOCOL @base = args[:base] || FORMS_BASE @api_key = args[:api_key] || (Paubox.configuration && Paubox.configuration.forms_api_key) end |
Instance Method Details
#archive_form(form_id) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/paubox/forms_client.rb', line 97 def archive_form(form_id) assert_uuid!(form_id, 'form_id') url = "#{@protocol}#{@host}#{@base}/api/forms/#{encode_segment(form_id)}/archive" response = request(method: :post, url: url, payload: '{}', headers: auth_headers.merge(content_type: :json)) JSON.parse(response.body) end |
#copy_form(form_id, title:) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/paubox/forms_client.rb', line 113 def copy_form(form_id, title:) assert_uuid!(form_id, 'form_id') url = "#{@protocol}#{@host}#{@base}/api/forms/copy" payload = { form_id: form_id, title: title } response = request(method: :post, url: url, payload: payload.to_json, headers: auth_headers.merge(content_type: :json)) Paubox::Form.new(JSON.parse(response.body)) end |
#create_form(attrs) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/paubox/forms_client.rb', line 75 def create_form(attrs) url = "#{@protocol}#{@host}#{@base}/api/forms" response = request(method: :post, url: url, payload: attrs.to_json, headers: auth_headers.merge(content_type: :json)) JSON.parse(response.body) end |
#find_form(form_id) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/paubox/forms_client.rb', line 82 def find_form(form_id) assert_uuid!(form_id, 'form_id') url = "#{@protocol}#{@host}#{@base}/api/forms/#{encode_segment(form_id)}" response = request(method: :get, url: url, headers: auth_headers) Paubox::Form.new(JSON.parse(response.body)['data']) end |
#form_stats(customer_id: nil) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/paubox/forms_client.rb', line 122 def form_stats(customer_id: nil) url = "#{@protocol}#{@host}#{@base}/api/forms/stats" headers = auth_headers headers = headers.merge(params: { customer_id: customer_id }) unless customer_id.nil? response = request(method: :get, url: url, headers: headers) JSON.parse(response.body) end |
#get_form(form_id) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/paubox/forms_client.rb', line 42 def get_form(form_id) assert_uuid!(form_id, 'form_id') url = "#{@protocol}#{@host}#{@base}/public/form_data/#{encode_segment(form_id)}" response = request(method: :get, url: url, headers: { accept: :json }) Paubox::Form.new(JSON.parse(response.body)) end |
#list_forms(params = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/paubox/forms_client.rb', line 58 def list_forms(params = {}) url = "#{@protocol}#{@host}#{@base}/api/forms" query = params.transform_keys(&:to_sym) .select { |k, _v| LIST_FORMS_PARAMS.include?(k) } if query[:customer_id].nil? raise ArgumentError, 'customer_id is required for list_forms and must ' \ "match the API key's customer, e.g. " \ 'client.list_forms(customer_id: 123)' end response = request(method: :get, url: url, headers: auth_headers.merge(params: query)) body = JSON.parse(response.body) forms = (body['results'] || []).map { |form| Paubox::Form.new(form) } { forms: forms, page_info: body['page_info'] } end |
#list_submissions(form_id, params = {}) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/paubox/forms_client.rb', line 130 def list_submissions(form_id, params = {}) assert_uuid!(form_id, 'form_id') url = "#{@protocol}#{@host}#{@base}/api/forms/#{encode_segment(form_id)}/submissions" query = params.transform_keys(&:to_sym) .select { |k, _v| LIST_SUBMISSIONS_PARAMS.include?(k) } response = request(method: :get, url: url, headers: auth_headers.merge(params: query)) body = JSON.parse(response.body) submissions = (body['data'] || []).map { |s| Paubox::FormSubmission.new(s) } { submissions: submissions, total: body['total'], page: body['page'], items: body['items'] } end |
#submission_pdf(form_id, submission_id) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/paubox/forms_client.rb', line 156 def submission_pdf(form_id, submission_id) assert_uuid!(form_id, 'form_id') assert_uuid!(submission_id, 'submission_id') url = "#{@protocol}#{@host}#{@base}/api/forms/#{encode_segment(form_id)}/submissions/" \ "#{encode_segment(submission_id)}/submission-pdf" request(method: :get, url: url, headers: auth_headers(accept: 'application/pdf')).body end |
#submissions_csv(form_id, submission_id: nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/paubox/forms_client.rb', line 143 def submissions_csv(form_id, submission_id: nil) assert_uuid!(form_id, 'form_id') base = "#{@protocol}#{@host}#{@base}/api/forms/#{encode_segment(form_id)}" \ '/submissions/submission-csv' url = if submission_id.nil? base else assert_uuid!(submission_id, 'submission_id') "#{base}/#{encode_segment(submission_id)}" end request(method: :get, url: url, headers: auth_headers(accept: 'text/csv')).body end |
#submit_form(form_id, form_data:, attachments: []) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/paubox/forms_client.rb', line 49 def submit_form(form_id, form_data:, attachments: []) assert_uuid!(form_id, 'form_id') url = "#{@protocol}#{@host}#{@base}/api/forms/#{encode_segment(form_id)}/submissions" payload = { form_data: form_data } payload[:attachments] = unless .empty? request(method: :post, url: url, payload: payload.to_json, headers: { content_type: :json, accept: :json }) end |
#unarchive_form(form_id) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/paubox/forms_client.rb', line 105 def unarchive_form(form_id) assert_uuid!(form_id, 'form_id') url = "#{@protocol}#{@host}#{@base}/api/forms/#{encode_segment(form_id)}/unarchive" response = request(method: :post, url: url, payload: '{}', headers: auth_headers.merge(content_type: :json)) JSON.parse(response.body) end |
#update_form(form_id, attrs) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/paubox/forms_client.rb', line 89 def update_form(form_id, attrs) assert_uuid!(form_id, 'form_id') url = "#{@protocol}#{@host}#{@base}/api/forms/#{encode_segment(form_id)}" response = request(method: :put, url: url, payload: attrs.to_json, headers: auth_headers.merge(content_type: :json)) JSON.parse(response.body) end |