formio 0.0.2
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.
- checksums.yaml +7 -0
- data/README.md +1 -0
- data/lib/formio.rb +6 -0
- data/lib/formio/client.rb +209 -0
- data/lib/formio/form.rb +45 -0
- data/lib/formio/record.rb +48 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 381a07d7ab5966796ebd564c3dc997dbc0e0c1d109fcacd24d35d9e693e88f52
|
4
|
+
data.tar.gz: b371922c91953de976f9a2547e438caa154d522c5c61c4d7023baedf6766a753
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 77b94aa40862154e11b62198282041f11e40aeeeb2951ade5800deebf6b7c434aa7b9ce161dbe6d2e864188420bbbe44a46ce1bf2cf25837ccd78c7466832474
|
7
|
+
data.tar.gz: d29b5954e9520659406a56b2475e8fadf78af0e40bce8d8911ee66eb161956d8205c6e2ab13925ed27b4bfdae73f69b0e8068fe8ef0f65088337c8d2d2791fcc
|
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
This is a gem to integrate ruby the formio platform
|
data/lib/formio.rb
ADDED
@@ -0,0 +1,209 @@
|
|
1
|
+
module Formio
|
2
|
+
class Client
|
3
|
+
def initialize(project_url, email: nil, password: nil, auth_token: nil)
|
4
|
+
@project_url = project_url
|
5
|
+
@email = email
|
6
|
+
@password = password
|
7
|
+
@auth_token = auth_token
|
8
|
+
login if email.present? && password.present?
|
9
|
+
end
|
10
|
+
|
11
|
+
def index(form)
|
12
|
+
response = connection.get do |req|
|
13
|
+
req.url "/#{form}/submission?limit=1000000&skip=0&sort=-created"
|
14
|
+
set_headers(req)
|
15
|
+
end
|
16
|
+
parse_response(response.body).map do |formio_hash|
|
17
|
+
Record.new(formio_hash)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def create(form:, values:)
|
22
|
+
values.each do |(k,_)|
|
23
|
+
values[k] ||= ""
|
24
|
+
end
|
25
|
+
response = connection.post do |req|
|
26
|
+
req.url "/#{form}/submission/"
|
27
|
+
set_headers(req)
|
28
|
+
req.body = {
|
29
|
+
data: values
|
30
|
+
}.to_json
|
31
|
+
end
|
32
|
+
if response.status >= 200 && response.status < 300
|
33
|
+
Record.new(parse_response(response.body))
|
34
|
+
else
|
35
|
+
parse_response(response.body)['details'].map { |x| x['message'] }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def update(record)
|
40
|
+
raise "Must supply a formio form" unless record.is_a?(Record)
|
41
|
+
response = connection.put do |req|
|
42
|
+
req.url "/#{record.form_name}/submission/#{record.id}"
|
43
|
+
req.url "/form/#{record.form_id}/submission/#{record.id}" if record.form_id
|
44
|
+
set_headers(req)
|
45
|
+
req.body = record.to_json
|
46
|
+
end
|
47
|
+
|
48
|
+
return update(record) if response.status == 502
|
49
|
+
if response.status >= 200 && response.status < 300
|
50
|
+
Record.new(parse_response(response.body))
|
51
|
+
else
|
52
|
+
parse_response(response.body)['details'].map { |x| x['message'] }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def find_by_id(form, submission_id)
|
57
|
+
response = connection.get do |req|
|
58
|
+
req.url "/#{form}/submission/#{submission_id}"
|
59
|
+
set_headers(req)
|
60
|
+
end
|
61
|
+
return find_by_id form, submission_id if response.status == 502
|
62
|
+
if response.status == 200
|
63
|
+
Record.new(parse_response(response.body))
|
64
|
+
else
|
65
|
+
Record::Nil.new
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def find_by(form, values=[])
|
70
|
+
response = connection.get do |req|
|
71
|
+
req.url "/#{form}/exists"
|
72
|
+
set_headers(req)
|
73
|
+
values.each do |(k,v)|
|
74
|
+
k = 'data.' + k.to_s unless k.to_s.start_with?('data.')
|
75
|
+
req.params[k] = v
|
76
|
+
end
|
77
|
+
end
|
78
|
+
return find_by(form, values) if response.status == 502
|
79
|
+
if response.status == 200
|
80
|
+
return find_by_id(form, JSON.parse(response.body)['_id'])
|
81
|
+
else
|
82
|
+
Record::Nil.new
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def delete(form, submission_id)
|
87
|
+
response = connection.delete do |req|
|
88
|
+
req.url "/#{form}/submission/#{submission_id}"
|
89
|
+
set_headers(req)
|
90
|
+
end
|
91
|
+
response.status == 200
|
92
|
+
end
|
93
|
+
|
94
|
+
def form_meta_data(form = 'form')
|
95
|
+
response = connection.get do |req|
|
96
|
+
req.url "/#{form}"
|
97
|
+
set_headers(req)
|
98
|
+
end
|
99
|
+
response = parse_response(response.body)
|
100
|
+
if response.is_a?(Array)
|
101
|
+
return response.map do |f|
|
102
|
+
FormioForm.new f
|
103
|
+
end
|
104
|
+
end
|
105
|
+
FormioForm.new(response)
|
106
|
+
end
|
107
|
+
|
108
|
+
def create_form(formio_form)
|
109
|
+
raise "Must supply a formio form" unless formio_form.is_a?(FormioForm)
|
110
|
+
response = connection.post do |req|
|
111
|
+
req.url "/form"
|
112
|
+
set_headers(req)
|
113
|
+
req.body = formio_form.to_json
|
114
|
+
end
|
115
|
+
if response.status != 201
|
116
|
+
raise (JSON.parse(response.body)['message'])
|
117
|
+
end
|
118
|
+
true
|
119
|
+
end
|
120
|
+
|
121
|
+
def delete_form(form_name)
|
122
|
+
response = connection.delete do |req|
|
123
|
+
req.url "/#{form_name}"
|
124
|
+
set_headers(req)
|
125
|
+
end
|
126
|
+
response.status == 200 || response.body == 'Invalid alias'
|
127
|
+
end
|
128
|
+
|
129
|
+
def set_headers(req)
|
130
|
+
req.headers['Content-Type'] = 'application/json'
|
131
|
+
req.headers['Accept'] = 'application/json'
|
132
|
+
req.headers['x-jwt-token'] = auth_token
|
133
|
+
end
|
134
|
+
|
135
|
+
def parse_response(response)
|
136
|
+
# return early if it's already valid json
|
137
|
+
# Some requests simply return json, some
|
138
|
+
# have to be decoded
|
139
|
+
begin
|
140
|
+
return JSON.parse(response)
|
141
|
+
rescue JSON::ParserError
|
142
|
+
end
|
143
|
+
|
144
|
+
read_gzip_data = -> (stringio) {
|
145
|
+
begin
|
146
|
+
Zlib::GzipReader.new(stringio, encoding: 'ASCII-8BIT')
|
147
|
+
rescue Zlib::GzipFile::Error
|
148
|
+
puts "An issue occured with formio: #{response}"
|
149
|
+
rescue
|
150
|
+
puts "An issue occured with formio"
|
151
|
+
end
|
152
|
+
}
|
153
|
+
|
154
|
+
response
|
155
|
+
.try { |it| StringIO.new(it) }
|
156
|
+
.try { |stringio| read_gzip_data.call(stringio) }
|
157
|
+
.try { |reader| reader.read }
|
158
|
+
.try { |json_string| JSON.parse(json_string) }
|
159
|
+
.yield_self { |parsed_json| parsed_json || [] }
|
160
|
+
end
|
161
|
+
|
162
|
+
def connection
|
163
|
+
require 'faraday/detailed_logger'
|
164
|
+
@connection ||= Faraday::Connection.new(project_url,
|
165
|
+
ssl: { verify: true }
|
166
|
+
) do |builder|
|
167
|
+
# builder.request :multipart
|
168
|
+
# builder.use ::FaradayMiddleware::ParseJson, content_type: 'application/json'
|
169
|
+
# builder.request :url_encoded
|
170
|
+
builder.request :curl
|
171
|
+
builder.adapter :net_http
|
172
|
+
# builder.response :detailed_logger # <-- Inserts the logger into the connection.
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def current_user
|
177
|
+
response = connection.get do |req|
|
178
|
+
req.url '/current'
|
179
|
+
set_headers(req)
|
180
|
+
end
|
181
|
+
Record.new(parse_response(response.body))
|
182
|
+
end
|
183
|
+
|
184
|
+
def login
|
185
|
+
@auth_token ||= begin
|
186
|
+
Rails.cache.fetch("formio_login_" + email, expires_in: 12.hours) do
|
187
|
+
formio_conn = Faraday::Connection.new("https://formio.form.io", ssl: { verify: true })
|
188
|
+
|
189
|
+
login_response = formio_conn.post do |req|
|
190
|
+
req.url "/user/login"
|
191
|
+
req.headers['Content-Type'] = 'application/json'
|
192
|
+
req.headers['Accept'] = 'application/json'
|
193
|
+
req.body = {
|
194
|
+
data: {
|
195
|
+
email: email,
|
196
|
+
password: password
|
197
|
+
}
|
198
|
+
}.to_json
|
199
|
+
end
|
200
|
+
login_response.headers['x-jwt-token']
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
alias auth_token login
|
205
|
+
|
206
|
+
private
|
207
|
+
attr_reader :email, :password, :project_url
|
208
|
+
end
|
209
|
+
end
|
data/lib/formio/form.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Formio
|
2
|
+
class Form
|
3
|
+
attr_accessor(
|
4
|
+
:formio_hash,
|
5
|
+
:type,
|
6
|
+
:formio_id,
|
7
|
+
:components,
|
8
|
+
:name,
|
9
|
+
:title,
|
10
|
+
:path,
|
11
|
+
:created_at,
|
12
|
+
:updated_at
|
13
|
+
)
|
14
|
+
|
15
|
+
def initialize(formio_hash)
|
16
|
+
@formio_hash = formio_hash
|
17
|
+
@type = formio_hash['type']
|
18
|
+
@formio_id = formio_hash['_id']
|
19
|
+
@components = formio_hash['components']
|
20
|
+
@name = @title = formio_hash['title']
|
21
|
+
@path = formio_hash['path']
|
22
|
+
@created_at = DateTime.parse formio_hash['created']
|
23
|
+
@updated_at = DateTime.parse formio_hash['modified']
|
24
|
+
end
|
25
|
+
|
26
|
+
def name=(name)
|
27
|
+
@name = @title = name
|
28
|
+
end
|
29
|
+
|
30
|
+
def title=(title)
|
31
|
+
@name = @title = title
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_json
|
35
|
+
{
|
36
|
+
title: title,
|
37
|
+
display: 'form',
|
38
|
+
type: type,
|
39
|
+
name: name,
|
40
|
+
path: path,
|
41
|
+
components: components,
|
42
|
+
}.to_json
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Formio
|
2
|
+
class Record
|
3
|
+
def initialize(formio_hash)
|
4
|
+
if formio_hash.empty? || formio_hash.nil?
|
5
|
+
raise "cannot construct FormioRecord"
|
6
|
+
end
|
7
|
+
@_id = @id = formio_hash['_id']
|
8
|
+
@form_id = formio_hash['form'] if formio_hash['form']
|
9
|
+
@formio_hash = formio_hash
|
10
|
+
@form_name = formio_hash['form_name']
|
11
|
+
if formio_hash['created']
|
12
|
+
@created_at = Time.parse formio_hash['created']
|
13
|
+
end
|
14
|
+
if formio_hash['modified']
|
15
|
+
@updated_at = Time.parse formio_hash['modified']
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_json
|
20
|
+
formio_hash.to_json
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_h
|
24
|
+
formio_hash
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_reader(
|
28
|
+
:id,
|
29
|
+
:_id,
|
30
|
+
:form_id,
|
31
|
+
:form_name,
|
32
|
+
:created_at,
|
33
|
+
:updated_at,
|
34
|
+
:formio_hash
|
35
|
+
)
|
36
|
+
|
37
|
+
class Nil < Record
|
38
|
+
def initialize
|
39
|
+
@_id = @id = nil
|
40
|
+
@form_id = nil
|
41
|
+
@formio_hash = nil
|
42
|
+
@form_name = nil
|
43
|
+
@created_at = nil
|
44
|
+
@updated_at = nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: formio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Frias
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A Ruby adapter for the form.io platform
|
14
|
+
email: jonathan.frias1@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- README.md
|
20
|
+
- lib/formio.rb
|
21
|
+
- lib/formio/client.rb
|
22
|
+
- lib/formio/form.rb
|
23
|
+
- lib/formio/record.rb
|
24
|
+
homepage: http://rubygems.org/gems/formio
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubygems_version: 3.0.2
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: A Ruby adapter for the form.io platform
|
47
|
+
test_files: []
|