formstack_client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +20 -0
- data/bin/console +14 -0
- data/bin/rspec +17 -0
- data/bin/setup +8 -0
- data/circle.yml +6 -0
- data/formstack_client.gemspec +29 -0
- data/lib/formstack/client.rb +139 -0
- data/lib/formstack/collection.rb +15 -0
- data/lib/formstack/connection.rb +53 -0
- data/lib/formstack/form.rb +99 -0
- data/lib/formstack/model.rb +82 -0
- data/lib/formstack/version.rb +3 -0
- data/lib/formstack.rb +11 -0
- data/lib/formstack_client.rb +1 -0
- metadata +162 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2770a835cee7a6e24aed13966673cd37601f5a03
|
4
|
+
data.tar.gz: 0a235785524cf3e6920c7f5cf931c8261589d3e1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cd4b7017d8bed0726a2761e53eb39b90a663b2c7e88f664a9567a593643275caa3a11a58191449d91bf1b6a06f9bfdba6977a050e1d9d2a554623c384fcdf34a
|
7
|
+
data.tar.gz: 95333c4a7075426f5997165d2644555c682d77476c73f1f54d0d464db8931df2710a8c0eb09942b4c5a7025ff30ad66f190298a7f3d133227aa78c6048917360
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Travis Petticrew
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
[![CircleCI](https://circleci.com/gh/watermarkchurch/formstack.svg?style=svg)](https://circleci.com/gh/watermarkchurch/formstack)
|
2
|
+
|
3
|
+
# Formstack Ruby Gem
|
4
|
+
|
5
|
+
This is a Ruby client to v2 of the Formstack API. It has a low level
|
6
|
+
call-for-call functional style API and a higher level object style API.
|
7
|
+
Let's see some examples!
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# Fetch all forms at once
|
11
|
+
forms = Formstack::Form.all
|
12
|
+
|
13
|
+
# Fetch a single form by ID
|
14
|
+
form = Formstack::Form.find(123)
|
15
|
+
|
16
|
+
# Get collections for items on a form
|
17
|
+
form.fields
|
18
|
+
form.submissions
|
19
|
+
form.confirmation_emails
|
20
|
+
form.notification_emails
|
21
|
+
form.submissions
|
22
|
+
|
23
|
+
# Create a new item from the collection
|
24
|
+
form.fields.create(type: "string", label: "New Field") # => <Field...>
|
25
|
+
form.submissions.create => <Submission...>
|
26
|
+
|
27
|
+
# Update a form
|
28
|
+
form.update(active: false)
|
29
|
+
|
30
|
+
# Copy a form
|
31
|
+
form.copy # => Returns the newly copied form
|
32
|
+
|
33
|
+
# Reload a form (useful to convert a list version of an object to a show
|
34
|
+
# version--the API returns different object structures for each endpoint)
|
35
|
+
form.load
|
36
|
+
|
37
|
+
# Delete a form
|
38
|
+
form.delete
|
39
|
+
```
|
40
|
+
|
41
|
+
Each of the individual objects (fields, submissions, etc.) can be
|
42
|
+
loaded, updated, or deleted just like a form. There is also a `create`
|
43
|
+
class method on each of the underlying classes if you would rather say
|
44
|
+
`Formstack::Field.create(form_id, field_attributes)`.
|
45
|
+
|
46
|
+
There is also a `Client` class that defines each endpoint as a seperate
|
47
|
+
method. It is the low-level API that the above API uses. You can use it
|
48
|
+
as follows:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
client = Formstack::Client.new
|
52
|
+
client.forms
|
53
|
+
client.form(form_id)
|
54
|
+
client.create_form(form_attributes)
|
55
|
+
client.update_form(form_id, form_attributes)
|
56
|
+
client.delete_form(form_id)
|
57
|
+
client.copy_form(form_id)
|
58
|
+
|
59
|
+
client.fields(form_id)
|
60
|
+
#...
|
61
|
+
```
|
62
|
+
|
63
|
+
To see a full list of methods check out the [Client class].
|
64
|
+
|
65
|
+
## Installation
|
66
|
+
|
67
|
+
Add this line to your application's Gemfile:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
gem 'formstack_client'
|
71
|
+
```
|
72
|
+
|
73
|
+
And then execute:
|
74
|
+
|
75
|
+
$ bundle
|
76
|
+
|
77
|
+
Or install it yourself as:
|
78
|
+
|
79
|
+
$ gem install formstack_client
|
80
|
+
|
81
|
+
## Setup
|
82
|
+
|
83
|
+
In order to get rolling, follow the installation instructions above and
|
84
|
+
then set the `FORMSTACK_ACCESS_TOKEN` environment variable to a valid
|
85
|
+
access token for your account. You're good to go!
|
86
|
+
|
87
|
+
If you don't know your access token you can create a new one for your
|
88
|
+
account on the [Formstack API Applications] page.
|
89
|
+
|
90
|
+
|
91
|
+
## Development
|
92
|
+
|
93
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
94
|
+
Then, run `rake spec` to run the tests. You can also run `rake pry`
|
95
|
+
for an interactive prompt that will allow you to experiment.
|
96
|
+
|
97
|
+
To install this gem onto your local machine, run `bundle exec rake
|
98
|
+
install`. To release a new version, update the version number in
|
99
|
+
`version.rb`, and then run `bundle exec rake release`, which will create
|
100
|
+
a git tag for the version, push git commits and tags, and push the
|
101
|
+
`.gem` file to [rubygems.org](https://rubygems.org).
|
102
|
+
|
103
|
+
This project also uses the `dotenv` gem. You can create a `.env` file
|
104
|
+
and add your `FORMSTACK_ACCESS_TOKEN` environment variable so that when
|
105
|
+
you launch a console with `rake pry` your configuration will already be
|
106
|
+
set.
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
|
110
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/watermarkchurch/formstack.
|
111
|
+
|
112
|
+
|
113
|
+
## License
|
114
|
+
|
115
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
116
|
+
|
117
|
+
## Resources
|
118
|
+
|
119
|
+
The spec this library was built from: [Formstack v2 API Documentation]
|
120
|
+
|
121
|
+
[Formstack v2 API Documentation]: https://developers.formstack.com/v2.0
|
122
|
+
[Formstack API Applications]: https://www.formstack.com/admin/apiKey/main
|
123
|
+
[Client class]: https://github.com/watermarkchurch/formstack/blob/master/lib/formstack/client.rb
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
task :environment do
|
9
|
+
require 'dotenv'
|
10
|
+
Dotenv.load
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
13
|
+
require 'formstack'
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Launch a pry shell with Formstack library loaded"
|
17
|
+
task :pry => :environment do
|
18
|
+
require 'pry'
|
19
|
+
Pry.start
|
20
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "formstack"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/rspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'rspec' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
16
|
+
|
17
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/bin/setup
ADDED
data/circle.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'formstack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "formstack_client"
|
8
|
+
spec.version = Formstack::VERSION
|
9
|
+
spec.authors = ["Travis Petticrew and the Watermark Development team"]
|
10
|
+
spec.email = ["travis@petticrew.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{A client to the Formstack v2 API}
|
13
|
+
spec.description = %q{A client to the Formstack v2 API}
|
14
|
+
spec.homepage = "https://github.com/watermarkchurch/formstack"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "dotenv"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "rspec-mocks", "~> 3.0"
|
28
|
+
spec.add_development_dependency "webmock", "~> 2.1"
|
29
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'formstack/connection'
|
3
|
+
|
4
|
+
module Formstack
|
5
|
+
class Client
|
6
|
+
extend Forwardable
|
7
|
+
attr_reader :connection
|
8
|
+
|
9
|
+
def_delegators :connection, :get, :post, :put, :delete
|
10
|
+
|
11
|
+
def initialize(connection: Connection.new)
|
12
|
+
@connection = connection
|
13
|
+
end
|
14
|
+
|
15
|
+
def forms(args={})
|
16
|
+
get("form", args)
|
17
|
+
end
|
18
|
+
|
19
|
+
def form(form_id)
|
20
|
+
get("form/#{form_id}")
|
21
|
+
end
|
22
|
+
|
23
|
+
def create_form(args={})
|
24
|
+
post("form", args)
|
25
|
+
end
|
26
|
+
|
27
|
+
def update_form(form_id, args={})
|
28
|
+
put("form/#{form_id}", args)
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete_form(form_id)
|
32
|
+
delete("form/#{form_id}")
|
33
|
+
end
|
34
|
+
|
35
|
+
def copy_form(form_id)
|
36
|
+
post("form/#{form_id}/copy")
|
37
|
+
end
|
38
|
+
|
39
|
+
def fields(form_id)
|
40
|
+
get("form/#{form_id}/field")
|
41
|
+
end
|
42
|
+
|
43
|
+
def field(field_id)
|
44
|
+
get("field/#{field_id}")
|
45
|
+
end
|
46
|
+
|
47
|
+
def create_field(form_id, args={})
|
48
|
+
post("form/#{form_id}/field", args)
|
49
|
+
end
|
50
|
+
|
51
|
+
def update_field(field_id, args={})
|
52
|
+
put("field/#{field_id}", args)
|
53
|
+
end
|
54
|
+
|
55
|
+
def delete_field(field_id)
|
56
|
+
delete("field/#{field_id}")
|
57
|
+
end
|
58
|
+
|
59
|
+
def submissions(form_id)
|
60
|
+
get("form/#{form_id}/submission")
|
61
|
+
end
|
62
|
+
|
63
|
+
def submission(submission_id)
|
64
|
+
get("submission/#{submission_id}")
|
65
|
+
end
|
66
|
+
|
67
|
+
def create_submission(form_id, args={})
|
68
|
+
post("form/#{form_id}/submission", args)
|
69
|
+
end
|
70
|
+
|
71
|
+
def update_submission(submission_id, args={})
|
72
|
+
put("submission/#{submission_id}", args)
|
73
|
+
end
|
74
|
+
|
75
|
+
def delete_submission(submission_id)
|
76
|
+
delete("submission/#{submission_id}")
|
77
|
+
end
|
78
|
+
|
79
|
+
def confirmation_emails(form_id)
|
80
|
+
get("form/#{form_id}/confirmation")
|
81
|
+
end
|
82
|
+
|
83
|
+
def confirmation_email(confirmation_id)
|
84
|
+
get("confirmation/#{confirmation_id}")
|
85
|
+
end
|
86
|
+
|
87
|
+
def create_confirmation_email(form_id, args={})
|
88
|
+
post("form/#{form_id}/confirmation", args)
|
89
|
+
end
|
90
|
+
|
91
|
+
def update_confirmation_email(confirmation_id, args={})
|
92
|
+
put("confirmation/#{confirmation_id}", args)
|
93
|
+
end
|
94
|
+
|
95
|
+
def delete_confirmation_email(confirmation_id)
|
96
|
+
delete("confirmation/#{confirmation_id}")
|
97
|
+
end
|
98
|
+
|
99
|
+
def notification_emails(form_id)
|
100
|
+
get("form/#{form_id}/notification")
|
101
|
+
end
|
102
|
+
|
103
|
+
def notification_email(notification_id)
|
104
|
+
get("notification/#{notification_id}")
|
105
|
+
end
|
106
|
+
|
107
|
+
def create_notification_email(form_id, args={})
|
108
|
+
post("form/#{form_id}/notification", args)
|
109
|
+
end
|
110
|
+
|
111
|
+
def update_notification_email(notification_id, args={})
|
112
|
+
put("notification/#{notification_id}", args)
|
113
|
+
end
|
114
|
+
|
115
|
+
def delete_notification_email(notification_id)
|
116
|
+
delete("notification/#{notification_id}")
|
117
|
+
end
|
118
|
+
|
119
|
+
def webhooks(form_id)
|
120
|
+
get("form/#{form_id}/webhook")
|
121
|
+
end
|
122
|
+
|
123
|
+
def webhook(webhook_id)
|
124
|
+
get("webhook/#{webhook_id}")
|
125
|
+
end
|
126
|
+
|
127
|
+
def create_webhook(form_id, args={})
|
128
|
+
post("form/#{form_id}/webhook", args)
|
129
|
+
end
|
130
|
+
|
131
|
+
def update_webhook(webhook_id, args={})
|
132
|
+
put("webhook/#{webhook_id}", args)
|
133
|
+
end
|
134
|
+
|
135
|
+
def delete_webhook(webhook_id)
|
136
|
+
delete("webhook/#{webhook_id}")
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module Formstack
|
4
|
+
class Collection < SimpleDelegator
|
5
|
+
def initialize(array, klass:, context: [])
|
6
|
+
@klass = klass
|
7
|
+
@context = context
|
8
|
+
super(array)
|
9
|
+
end
|
10
|
+
|
11
|
+
def create(*args)
|
12
|
+
@klass.create(*@context, *args)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Formstack
|
6
|
+
class Connection
|
7
|
+
attr_reader :base_uri, :access_token
|
8
|
+
|
9
|
+
BASE_URI = URI('https://www.formstack.com/api/v2/').freeze
|
10
|
+
|
11
|
+
def initialize(base_uri: BASE_URI, access_token: ENV['FORMSTACK_ACCESS_TOKEN'])
|
12
|
+
@base_uri = URI(base_uri)
|
13
|
+
@access_token = access_token
|
14
|
+
end
|
15
|
+
|
16
|
+
def get(uri, args={})
|
17
|
+
request(uri: uri, request_factory: Net::HTTP::Get, args: args)
|
18
|
+
end
|
19
|
+
|
20
|
+
def post(uri, args={})
|
21
|
+
request(uri: uri, request_factory: Net::HTTP::Post, args: args)
|
22
|
+
end
|
23
|
+
|
24
|
+
def put(uri, args={})
|
25
|
+
request(uri: uri, request_factory: Net::HTTP::Put, args: args)
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete(uri, args={})
|
29
|
+
request(uri: uri, request_factory: Net::HTTP::Delete, args: args)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def request(uri:, request_factory:, args: {})
|
35
|
+
uri = base_uri + uri
|
36
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
37
|
+
request = request_factory.new uri
|
38
|
+
request["Authorization"] = "Bearer #{access_token}"
|
39
|
+
if !args.empty?
|
40
|
+
request["Content-Type"] = "application/json"
|
41
|
+
request.body = args.to_json
|
42
|
+
end
|
43
|
+
http.request request
|
44
|
+
end
|
45
|
+
|
46
|
+
parse_response(response)
|
47
|
+
end
|
48
|
+
|
49
|
+
def parse_response(response)
|
50
|
+
JSON.parse(response.body)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'formstack/model'
|
2
|
+
|
3
|
+
module Formstack
|
4
|
+
class Form
|
5
|
+
include Model
|
6
|
+
|
7
|
+
def copy
|
8
|
+
new_from_response client.copy_form self[:id]
|
9
|
+
end
|
10
|
+
|
11
|
+
def fields
|
12
|
+
@fields ||= if self[:fields]
|
13
|
+
self[:fields].map { |f| Field.new(f) }
|
14
|
+
else
|
15
|
+
Field.all(self[:id])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def submissions
|
20
|
+
@submissions ||= Submission.all(self[:id])
|
21
|
+
end
|
22
|
+
|
23
|
+
def confirmation_emails
|
24
|
+
@confirmation_emails ||= ConfirmationEmail.all(self[:id])
|
25
|
+
end
|
26
|
+
|
27
|
+
def notification_emails
|
28
|
+
@notification_emails ||= NotificationEmail.all(self[:id])
|
29
|
+
end
|
30
|
+
|
31
|
+
def webhooks
|
32
|
+
@webhooks ||= Webhook.all(self[:id])
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.client_method
|
36
|
+
:form
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.response_nesting
|
40
|
+
"forms"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Field
|
45
|
+
include Model
|
46
|
+
|
47
|
+
def self.client_method
|
48
|
+
:field
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class Submission
|
53
|
+
include Model
|
54
|
+
|
55
|
+
def self.client_method
|
56
|
+
:submission
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.response_nesting
|
60
|
+
"submissions"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class ConfirmationEmail
|
65
|
+
include Model
|
66
|
+
|
67
|
+
def self.client_method
|
68
|
+
:confirmation_email
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.response_nesting
|
72
|
+
"confirmations"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class NotificationEmail
|
77
|
+
include Model
|
78
|
+
|
79
|
+
def self.client_method
|
80
|
+
:notification_email
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.response_nesting
|
84
|
+
"notifications"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class Webhook
|
89
|
+
include Model
|
90
|
+
|
91
|
+
def self.client_method
|
92
|
+
:webhook
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.response_nesting
|
96
|
+
"webhooks"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'formstack/collection'
|
3
|
+
|
4
|
+
module Formstack
|
5
|
+
module Model
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.send :attr_reader, :attributes
|
9
|
+
base.extend ClassMethods
|
10
|
+
base.extend Forwardable
|
11
|
+
base.def_delegators :'self.class', :client, :client_method, :new_from_response
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(attributes={})
|
15
|
+
@attributes = attributes
|
16
|
+
end
|
17
|
+
|
18
|
+
def [](key)
|
19
|
+
attributes[key.to_s]
|
20
|
+
end
|
21
|
+
|
22
|
+
def load
|
23
|
+
@attributes = client.public_send(client_method, self[:id])
|
24
|
+
end
|
25
|
+
|
26
|
+
def update(new_attributes={})
|
27
|
+
attributes.merge!(stringify_hash_keys(new_attributes))
|
28
|
+
client.public_send("update_#{client_method}", self[:id], attributes)
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete
|
32
|
+
client.public_send("delete_#{client_method}", self[:id])
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def stringify_hash_keys(hash)
|
38
|
+
hash.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
|
39
|
+
end
|
40
|
+
|
41
|
+
module ClassMethods
|
42
|
+
|
43
|
+
def all(*args)
|
44
|
+
new_from_response client.public_send("#{client_method}s", *args), context: args
|
45
|
+
end
|
46
|
+
|
47
|
+
def find(*args)
|
48
|
+
new_from_response client.public_send(client_method, *args)
|
49
|
+
end
|
50
|
+
|
51
|
+
def create(*args)
|
52
|
+
new_from_response(client.public_send("create_#{client_method}", *args)).tap do |obj|
|
53
|
+
obj.load if obj[:id]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def new_from_response(response, context: [])
|
58
|
+
if response.respond_to?(:has_key?) && response.has_key?(response_nesting)
|
59
|
+
response = response[response_nesting]
|
60
|
+
end
|
61
|
+
if response.is_a?(Array)
|
62
|
+
Collection.new(response.map { |item| new item }, klass: self, context: context)
|
63
|
+
else
|
64
|
+
new response
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def client
|
69
|
+
Formstack.client
|
70
|
+
end
|
71
|
+
|
72
|
+
def client_method
|
73
|
+
raise NotImplementedError
|
74
|
+
end
|
75
|
+
|
76
|
+
def response_nesting
|
77
|
+
""
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/lib/formstack.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'formstack'
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: formstack_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Travis Petticrew and the Watermark Development team
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dotenv
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-mocks
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: webmock
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.1'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.1'
|
111
|
+
description: A client to the Formstack v2 API
|
112
|
+
email:
|
113
|
+
- travis@petticrew.net
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- bin/console
|
126
|
+
- bin/rspec
|
127
|
+
- bin/setup
|
128
|
+
- circle.yml
|
129
|
+
- formstack_client.gemspec
|
130
|
+
- lib/formstack.rb
|
131
|
+
- lib/formstack/client.rb
|
132
|
+
- lib/formstack/collection.rb
|
133
|
+
- lib/formstack/connection.rb
|
134
|
+
- lib/formstack/form.rb
|
135
|
+
- lib/formstack/model.rb
|
136
|
+
- lib/formstack/version.rb
|
137
|
+
- lib/formstack_client.rb
|
138
|
+
homepage: https://github.com/watermarkchurch/formstack
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata: {}
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.4.2
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: A client to the Formstack v2 API
|
162
|
+
test_files: []
|