Class: ProntoForms::Client
- Inherits:
-
Object
- Object
- ProntoForms::Client
- 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
-
#api_key_id ⇒ String
readonly
ProntoForms API key ID.
-
#api_key_secret ⇒ String
readonly
ProntoForms API key secret.
Class Method Summary collapse
-
.resource_list(method, resource, url = resource.resource_name) ⇒ nil
private
Defines a resource that can be retrieved in a list.
Instance Method Summary collapse
-
#connection ⇒ Faraday::Connection
private
Create a connection that can be used to execute a request against the ProntoForms API.
-
#form_space(id) ⇒ FormSpace
Retrieve a form space by its identifier.
-
#form_spaces ⇒ ResourceList
Retrieve a list of FormSpace resources.
-
#form_submission(id) ⇒ FormSubmission
Retrieve a form submission by identifier.
-
#form_submissions ⇒ ResourceList
Retrieve a list of FormSubmission resources.
-
#initialize(api_key_id, api_key_secret) ⇒ Client
constructor
Create a client and use provided API credentials.
-
#user(id) ⇒ User
Retrieve a user by identifier.
Constructor Details
#initialize(api_key_id, api_key_secret) ⇒ Client
Create a client and use provided API credentials
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_id ⇒ String (readonly)
Returns ProntoForms API key ID.
15 16 17 |
# File 'lib/prontoforms/client.rb', line 15 def api_key_id @api_key_id end |
#api_key_secret ⇒ String (readonly)
Returns 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
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
#connection ⇒ Faraday::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.
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
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_spaces ⇒ ResourceList
Retrieve a list of FormSpace resources
50 |
# File 'lib/prontoforms/client.rb', line 50 resource_list :form_spaces, FormSpace |
#form_submission(id) ⇒ FormSubmission
Retrieve a form submission by identifier
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_submissions ⇒ ResourceList
Retrieve a list of FormSubmission resources
51 |
# File 'lib/prontoforms/client.rb', line 51 resource_list :form_submissions, FormSubmission |