Class: Formio::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/formio/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
   if email.present? && password.present?
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



225
226
227
# File 'lib/formio/client.rb', line 225

def email
  @email
end

#passwordObject (readonly)

Returns the value of attribute password.



225
226
227
# File 'lib/formio/client.rb', line 225

def password
  @password
end

#project_urlObject (readonly)

Returns the value of attribute project_url.



225
226
227
# File 'lib/formio/client.rb', line 225

def project_url
  @project_url
end

Instance Method Details

#compact_formio_hash(data, max_level = 2) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/formio/client.rb', line 227

def compact_formio_hash(data, max_level=2)
  return unless data.is_a?(Hash) || data.is_a?(Array)
  data.each do |k,v|
    if max_level <= 0
      data.delete k
    else
      if(v.is_a?(Hash))
        compact_formio_hash(v, max_level - 1)
      end
      if(v.is_a?(Array))
        v.each { |elem| compact_formio_hash(elem, max_level) }
      end
    end
  end.compact
end

#connectionObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/formio/client.rb', line 178

def connection
  require 'faraday/detailed_logger'
  @connection ||= Faraday::Connection.new(project_url,
    ssl: { verify: true }
  ) do |builder|
    require 'faraday_curl'
    # require 'faraday-detailed_logger'
    require 'faraday-cookie_jar'
    # 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



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/formio/client.rb', line 112

def create_form(formio_form)
  raise "Must supply a formio form" unless formio_form.is_a?(::Formio::Form)
  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_userObject



195
196
197
198
199
200
201
202
# File 'lib/formio/client.rb', line 195

def current_user
  response = connection.get do |req|
    req.url '/current'
    set_headers(req)
  end
  return nil unless response.status >= 200 && response.status < 300
  Record.new(parse_response(response.body))
end

#delete(form, submission_id) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/formio/client.rb', line 90

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



137
138
139
140
141
142
143
# File 'lib/formio/client.rb', line 137

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/formio/client.rb', line 73

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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/formio/client.rb', line 60

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



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/formio/client.rb', line 98

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|
      ::Formio::Form.new f
    end
  end
  ::Formio::Form.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

#loginObject Also known as: auth_token



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/formio/client.rb', line 204

def 
  @auth_token ||= begin
    return unless email
    formio_conn = Faraday::Connection.new("https://formio.form.io", ssl: { verify: true })

     = 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
    .headers['x-jwt-token']
  end
end

#parse_response(response) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/formio/client.rb', line 151

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 e
      puts "An issue occured with formio #{response}"
    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



145
146
147
148
149
# File 'lib/formio/client.rb', line 145

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, max_depth: 2) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/formio/client.rb', line 39

def update(record, max_depth: 2)
  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 = compact_formio_hash(record.formio_hash, max_depth).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

rescue Net::OpenTimeout
  binding.pry
  retry
end

#update_form(formio_form) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/formio/client.rb', line 125

def update_form(formio_form)
  raise "Must supply a formio form" unless formio_form.is_a?(::Formio::Form)
  response = connection.put do |req|
    req.url "/form/#{formio_form.id}"
    set_headers(req)
    req.body = formio_form.to_json
  end
  if response.status >= 200 && response.status < 300
    parse_response(response.body)
  end
end