Class: ProntoForms::Client

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

Overview

Allows you to retrieve resources from ProntoForms and perform other functions with the API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key_id, api_key_secret) ⇒ Client

Create a client and use provided API credentials

Parameters:

  • api_key_id

    Your ProntoForms REST API key

  • api_key_secret

    Your ProntoForms REST API secret



22
23
24
25
# File 'lib/prontoforms/client.rb', line 22

def initialize(api_key_id, api_key_secret)
  @api_key_id = api_key_id
  @api_key_secret = api_key_secret
end

Instance Attribute Details

#api_key_idString (readonly)

Returns ProntoForms API key ID.

Returns:

  • (String)

    ProntoForms API key ID



15
16
17
# File 'lib/prontoforms/client.rb', line 15

def api_key_id
  @api_key_id
end

#api_key_secretString (readonly)

Returns ProntoForms API key secret.

Returns:

  • (String)

    ProntoForms API key secret



17
18
19
# File 'lib/prontoforms/client.rb', line 17

def api_key_secret
  @api_key_secret
end

Class Method Details

.resource_list(method, resource, url = resource.resource_name) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines a resource that can be retrieved in a list

Returns:

  • (nil)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/prontoforms/client.rb', line 34

def self.resource_list(method, resource, url = resource.resource_name)
  define_method(method) do |query: {}|
    res = connection.get do |req|
      req.url url
      query.each { |k, v| req.params[k] = v }
    end

    data = JSON.parse(res.body)

    return nil if data.fetch('pageData').size.zero?

    ResourceList.new(data, { 'p' => 0, 's' => 100 }.merge(query), method,
                     resource, self)
  end
end

Instance Method Details

#connectionFaraday::Connection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a connection that can be used to execute a request against the ProntoForms API.

Returns:

  • (Faraday::Connection)


96
97
98
99
100
101
# File 'lib/prontoforms/client.rb', line 96

def connection
  Faraday.new(url: 'https://api.prontoforms.com/api/1.1') do |conn|
    conn.basic_auth(api_key_id, api_key_secret)
    conn.use Faraday::Response::RaiseError
  end
end

#form_space(id) ⇒ FormSpace

Retrieve a form space by its identifier

Parameters:

  • id (String)

    The form space identifier

Returns:

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
# File 'lib/prontoforms/client.rb', line 69

def form_space(id)
  raise ArgumentError, 'id must be provided' if id.nil?

  res = connection.get do |req|
    req.url "formspaces/#{id}"
  end

  FormSpace.new(JSON.parse(res.body), self)
end

#form_spacesResourceList

Retrieve a list of FormSpace resources

Returns:

  • (ResourceList)

    A ResourceList containing FormSpace results



50
# File 'lib/prontoforms/client.rb', line 50

resource_list :form_spaces, FormSpace

#form_submission(id) ⇒ FormSubmission

Retrieve a form submission by identifier

Parameters:

  • id (String)

    The form submission identifier

Returns:



82
83
84
85
86
87
88
89
90
# File 'lib/prontoforms/client.rb', line 82

def form_submission(id)
  return nil if id.nil?

  res = connection.get do |req|
    req.url "data/#{id}"
  end

  FormSubmission.new(JSON.parse(res.body), self)
end

#form_submissionsResourceList

Retrieve a list of FormSubmission resources

Returns:

  • (ResourceList)

    A ResourceList containing FormSubmission results



51
# File 'lib/prontoforms/client.rb', line 51

resource_list :form_submissions, FormSubmission

#user(id) ⇒ User

Retrieve a user by identifier

Parameters:

  • id (String)

    The user identifier

Returns:

  • (User)

    A User object for the requested user

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
# File 'lib/prontoforms/client.rb', line 56

def user(id)
  raise ArgumentError, 'id must be provided' if id.nil?

  res = connection.get do |req|
    req.url "users/#{id}"
  end

  User.new(JSON.parse(res.body), self)
end