Class: Base::Endpoints::Forms

Inherits:
Base::Endpoint show all
Defined in:
lib/base/endpoints/forms.rb

Overview

This endpoint contains methods for forms.

Instance Attribute Summary

Attributes inherited from Base::Endpoint

#connection, #path

Instance Method Summary collapse

Methods inherited from Base::Endpoint

#io, #parse, #request

Constructor Details

#initialize(access_token:, url:) ⇒ Forms

Initializes this endpoint.



8
9
10
11
# File 'lib/base/endpoints/forms.rb', line 8

def initialize(access_token:, url:)
  @path = 'forms'
  super
end

Instance Method Details

#create(name:) ⇒ Object

Creates a form with the given name



24
25
26
27
28
29
30
31
# File 'lib/base/endpoints/forms.rb', line 24

def create(name:)
  request do
    response =
      connection.post('', 'name' => name)

    parse(response.body)
  end
end

#delete(id) ⇒ Object

Deletes the form with the given ID.



44
45
46
47
48
49
50
51
# File 'lib/base/endpoints/forms.rb', line 44

def delete(id)
  request do
    response =
      connection.delete id

    parse(response.body)
  end
end

#delete_submission(id, submission_id) ⇒ Object

Deletes the submission with the given ID of the form with the given ID.



126
127
128
129
130
131
132
133
# File 'lib/base/endpoints/forms.rb', line 126

def delete_submission(id, submission_id)
  request do
    response =
      connection.delete "#{id}/submissions/#{submission_id}"

    parse(response.body)
  end
end

#get(id) ⇒ Object

Returns the form with the given ID.



34
35
36
37
38
39
40
41
# File 'lib/base/endpoints/forms.rb', line 34

def get(id)
  request do
    response =
      connection.get id

    parse(response.body)
  end
end

#get_submission(id, submission_id) ⇒ Object

Returns the submission with the given ID of the form with the given ID.



91
92
93
94
95
96
97
98
# File 'lib/base/endpoints/forms.rb', line 91

def get_submission(id, submission_id)
  request do
    response =
      connection.get "#{id}/submissions/#{submission_id}"

    parse(response.body)
  end
end

#list(page: 1, per_page: 10) ⇒ Object

Lists the forms of a project



14
15
16
17
18
19
20
21
# File 'lib/base/endpoints/forms.rb', line 14

def list(page: 1, per_page: 10)
  request do
    response =
      connection.get('', per_page: per_page, page: page)

    parse(response.body)
  end
end

#submissions(id:, page: 1, per_page: 10) ⇒ Object

Returns the submission for the form with the given ID.



79
80
81
82
83
84
85
86
87
88
# File 'lib/base/endpoints/forms.rb', line 79

def submissions(id:,
                page: 1,
                per_page: 10)
  request do
    response =
      connection.get("#{id}/submissions", per_page: per_page, page: page)

    parse(response.body)
  end
end

#submit(id:, form:) ⇒ Object

Submits a new submission for the form with the given ID.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/base/endpoints/forms.rb', line 54

def submit(id:, form:)
  request do
    payload =
      form.each_with_object({}) do |(key, value), memo|
        memo[key] =
          case value
          when File, Tempfile
            Faraday::UploadIO.new(
              value.path,
              File.mime_type?(value),
              File.basename(value)
            )
          else
            value
          end
      end

    response =
      connection.post("#{id}/submit", payload)

    parse(response.body)
  end
end

#update_submission(id:, submission_id:, form:) ⇒ Object

Submits a new submission for the form with the given ID.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/base/endpoints/forms.rb', line 101

def update_submission(id:, submission_id:, form:)
  request do
    payload =
      form.each_with_object({}) do |(key, value), memo|
        memo[key] =
          case value
          when File, Tempfile
            Faraday::UploadIO.new(
              value.path,
              File.mime_type?(value),
              File.basename(value)
            )
          else
            value
          end
      end

    response =
      connection.put("#{id}/submit/#{submission_id}", payload)

    parse(response.body)
  end
end