Class: Formio::Client
- Inherits:
-
Object
- Object
- Formio::Client
- Defined in:
- lib/formio/client.rb
Instance Method Summary collapse
- #connection ⇒ Object
- #create(form:, values:) ⇒ Object
- #create_form(formio_form) ⇒ Object
- #current_user ⇒ Object
- #delete(form, submission_id) ⇒ Object
- #delete_form(form_name) ⇒ Object
- #find_by(form, values = []) ⇒ Object
- #find_by_id(form, submission_id) ⇒ Object
- #form_meta_data(form = 'form') ⇒ Object
- #index(form) ⇒ Object
-
#initialize(project_url, email: nil, password: nil, auth_token: nil) ⇒ Client
constructor
A new instance of Client.
- #login ⇒ Object (also: #auth_token)
- #parse_response(response) ⇒ Object
- #set_headers(req) ⇒ Object
- #update(record) ⇒ Object
Constructor Details
#initialize(project_url, email: nil, password: nil, auth_token: nil) ⇒ Client
Returns a new instance of Client.
3 4 5 6 7 8 9 |
# File 'lib/formio/client.rb', line 3 def initialize(project_url, email: nil, password: nil, auth_token: nil) @project_url = project_url @email = email @password = password @auth_token = auth_token login if email.present? && password.present? end |
Instance Method Details
#connection ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/formio/client.rb', line 162 def connection require 'faraday/detailed_logger' @connection ||= Faraday::Connection.new(project_url, ssl: { verify: true } ) do |builder| # builder.request :multipart # builder.use ::FaradayMiddleware::ParseJson, content_type: 'application/json' # builder.request :url_encoded builder.request :curl builder.adapter :net_http # builder.response :detailed_logger # <-- Inserts the logger into the connection. end end |
#create(form:, values:) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/formio/client.rb', line 21 def create(form:, values:) values.each do |(k,_)| values[k] ||= "" end response = connection.post do |req| req.url "/#{form}/submission/" set_headers(req) req.body = { data: values }.to_json end if response.status >= 200 && response.status < 300 Record.new(parse_response(response.body)) else parse_response(response.body)['details'].map { |x| x['message'] } end end |
#create_form(formio_form) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/formio/client.rb', line 108 def create_form(formio_form) raise "Must supply a formio form" unless formio_form.is_a?(FormioForm) response = connection.post do |req| req.url "/form" set_headers(req) req.body = formio_form.to_json end if response.status != 201 raise (JSON.parse(response.body)['message']) end true end |
#current_user ⇒ Object
176 177 178 179 180 181 182 |
# File 'lib/formio/client.rb', line 176 def current_user response = connection.get do |req| req.url '/current' set_headers(req) end Record.new(parse_response(response.body)) end |
#delete(form, submission_id) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/formio/client.rb', line 86 def delete(form, submission_id) response = connection.delete do |req| req.url "/#{form}/submission/#{submission_id}" set_headers(req) end response.status == 200 end |
#delete_form(form_name) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/formio/client.rb', line 121 def delete_form(form_name) response = connection.delete do |req| req.url "/#{form_name}" set_headers(req) end response.status == 200 || response.body == 'Invalid alias' end |
#find_by(form, values = []) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/formio/client.rb', line 69 def find_by(form, values=[]) response = connection.get do |req| req.url "/#{form}/exists" set_headers(req) values.each do |(k,v)| k = 'data.' + k.to_s unless k.to_s.start_with?('data.') req.params[k] = v end end return find_by(form, values) if response.status == 502 if response.status == 200 return find_by_id(form, JSON.parse(response.body)['_id']) else Record::Nil.new end end |
#find_by_id(form, submission_id) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/formio/client.rb', line 56 def find_by_id(form, submission_id) response = connection.get do |req| req.url "/#{form}/submission/#{submission_id}" set_headers(req) end return find_by_id form, submission_id if response.status == 502 if response.status == 200 Record.new(parse_response(response.body)) else Record::Nil.new end end |
#form_meta_data(form = 'form') ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/formio/client.rb', line 94 def (form = 'form') response = connection.get do |req| req.url "/#{form}" set_headers(req) end response = parse_response(response.body) if response.is_a?(Array) return response.map do |f| FormioForm.new f end end FormioForm.new(response) end |
#index(form) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/formio/client.rb', line 11 def index(form) response = connection.get do |req| req.url "/#{form}/submission?limit=1000000&skip=0&sort=-created" set_headers(req) end parse_response(response.body).map do |formio_hash| Record.new(formio_hash) end end |
#login ⇒ Object Also known as: auth_token
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/formio/client.rb', line 184 def login @auth_token ||= begin Rails.cache.fetch("formio_login_" + email, expires_in: 12.hours) do formio_conn = Faraday::Connection.new("https://formio.form.io", ssl: { verify: true }) login_response = formio_conn.post do |req| req.url "/user/login" req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' req.body = { data: { email: email, password: password } }.to_json end login_response.headers['x-jwt-token'] end end end |
#parse_response(response) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/formio/client.rb', line 135 def parse_response(response) # return early if it's already valid json # Some requests simply return json, some # have to be decoded begin return JSON.parse(response) rescue JSON::ParserError end read_gzip_data = -> (stringio) { begin Zlib::GzipReader.new(stringio, encoding: 'ASCII-8BIT') rescue Zlib::GzipFile::Error puts "An issue occured with formio: #{response}" rescue puts "An issue occured with formio" end } response .try { |it| StringIO.new(it) } .try { |stringio| read_gzip_data.call(stringio) } .try { |reader| reader.read } .try { |json_string| JSON.parse(json_string) } .yield_self { |parsed_json| parsed_json || [] } end |
#set_headers(req) ⇒ Object
129 130 131 132 133 |
# File 'lib/formio/client.rb', line 129 def set_headers(req) req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' req.headers['x-jwt-token'] = auth_token end |
#update(record) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/formio/client.rb', line 39 def update(record) raise "Must supply a formio form" unless record.is_a?(Record) response = connection.put do |req| req.url "/#{record.form_name}/submission/#{record.id}" req.url "/form/#{record.form_id}/submission/#{record.id}" if record.form_id set_headers(req) req.body = record.to_json end return update(record) if response.status == 502 if response.status >= 200 && response.status < 300 Record.new(parse_response(response.body)) else parse_response(response.body)['details'].map { |x| x['message'] } end end |