ask_awesomely 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70509a7f4e01e364e2e6177430566f5853fb4bb9
4
- data.tar.gz: 5348d34668d7a5ad781707d297ad08c7579f245d
3
+ metadata.gz: 7d1822232d0b2c24ecdc6cef57fee6af17ba2f64
4
+ data.tar.gz: 31dba1d91b42808d5b5162ba725abb5f9c208d90
5
5
  SHA512:
6
- metadata.gz: 2ba0944f97f24d5d291704e381e00f6539b85d5d1056e8cd13d1cca2da9d3fbb22d59a29ee9c5c4c14c9fb3fd70cb882781ebfc8cf6ec9ec46ad03af423ed928
7
- data.tar.gz: 5016b441a5fc79fdd3dbff2682e7c6a44915798c54ea8a910530633783cdff1a856b69ccdc533b86c3ac39facb3c789a64dfd59835933e0a88ac3166f2c4de2b
6
+ metadata.gz: 0f83e055124bbe75c7a4e1417773a1bc1ea95c4d399563655e089fa81881932499964923c7119181a23795b96d9040e5506063ce5621d1a3e4ba9eb2d6970e88
7
+ data.tar.gz: d3a9def670b3ad1011948c96d9ec3e32298ac968056a7422074dbabf004131cb4262b3648cda428c596746e47028f63d3482109e98aaf714a848ddbb7e49d5c2
data/README.md CHANGED
@@ -13,6 +13,7 @@ Build Typeforms awesomely. In Ruby.
13
13
  - [Usage](#usage)
14
14
  - [Authentication](#authentication)
15
15
  - [If you're using images](#if-youre-using-images)
16
+ - [Logging](#logging)
16
17
  - [Basic example](#basic-example)
17
18
  - [Available fields and options](#available-fields-and-options)
18
19
  - [Statement](#statement)
@@ -37,6 +38,7 @@ Build Typeforms awesomely. In Ruby.
37
38
  - [Widget](#widget)
38
39
  - [Drawer](#drawer)
39
40
  - [Fullscreen](#fullscreen)
41
+ - [Getting Results](#getting-results)
40
42
  - [Development](#development)
41
43
  - [Feedback and Contributions](#feedback-and-contributions)
42
44
 
@@ -83,6 +85,9 @@ AskAwesomely.configure do |config|
83
85
  end
84
86
  ```
85
87
 
88
+ If you don't already have a key, [sign up for one here](https://io1.typeform.com/to/HMLOBl?v=1).
89
+
90
+
86
91
  Your API Keys are **super secret** so don't commit them in your code. Use `ENV` or
87
92
  something like [dotenv](https://github.com/bkeepers/dotenv) so you can keep the credentials out of the repository. This stops bad people from stealing the key and hijacking your Typeform I/O account.
88
93
 
@@ -105,6 +110,16 @@ end
105
110
 
106
111
  As before, don't commit these keys to your repo unless you want [bad things to happen](http://vertis.io/2013/12/16/unauthorised-litecoin-mining.html). Check up on the [AWS Best Practices](http://docs.aws.amazon.com/general/latest/gr/aws-access-keys-best-practices.html) if you want to know more.
107
112
 
113
+ ### Logging
114
+
115
+ `AskAwesomely` might warn you if you miss something out, just to make sure. By default this will go straight to `STDOUT`, but you can tell it to use your own logging:
116
+
117
+ ```ruby
118
+ AskAwesomely.configure do |config|
119
+ config.logger = your_new_logger
120
+ end
121
+ ```
122
+
108
123
  ### Basic example
109
124
 
110
125
  You will want to create a class that represents a specific form to be built:
@@ -351,7 +366,6 @@ field :legal do
351
366
  end
352
367
  ```
353
368
 
354
-
355
369
  ## Passing Context
356
370
 
357
371
  Building a form full of hard-coded data is all well and good, but it doesn't offer much benefit over using a web interface. What if you want to build personalised forms based on, say, an `ActiveRecord` model?
@@ -410,7 +424,7 @@ end
410
424
 
411
425
  ### Embedding
412
426
 
413
- You can also embed a form straight away if you prefer. `AskAwesomely` generates the correct embed code for you, with the correct URL and Typeform title. The style can be customised with CSS but some aspects of the embed itself are not currently customisable (e.g. changing button text and widget size).
427
+ You can also embed a form straight away if you prefer. `AskAwesomely` generates the correct embed code for you, with the correct URL and Typeform title. The style can be customised with CSS, and you can also tweak some of the output.
414
428
 
415
429
  To see what each embedding option looks like, check out the [`Embedding Modes`](http://docs.typeform.io/docs/embedding-introduction) documentation at Typeform I/O. It has pictures and everything.
416
430
 
@@ -421,7 +435,7 @@ Assuming you have built a Typeform as in the other examples, rendering the embed
421
435
  Pops up over the page content and fills most of the screen.
422
436
 
423
437
  ```ruby
424
- typeform.embed_as(:modal)
438
+ typeform.embed_as(:modal, button_text: "Launch me!")
425
439
  ```
426
440
 
427
441
  #### Widget
@@ -429,7 +443,7 @@ typeform.embed_as(:modal)
429
443
  Allows you more control over where the form is embedded and how it appears. Just a box on the page.
430
444
 
431
445
  ```ruby
432
- typeform.embed_as(:widget)
446
+ typeform.embed_as(:widget, width: "1024px", height: "768px")
433
447
  ```
434
448
 
435
449
  #### Drawer
@@ -437,7 +451,7 @@ typeform.embed_as(:widget)
437
451
  Makes the form slide in from the side of the page, hamburger menu style, and fills at least half of the screen.
438
452
 
439
453
  ```ruby
440
- typeform.embed_as(:drawer)
454
+ typeform.embed_as(:drawer, button_text: "Launch me!")
441
455
  ```
442
456
 
443
457
  #### Fullscreen
@@ -450,6 +464,23 @@ Note that this outputs a **complete** HTML document, CSS and all. If you're work
450
464
  typeform.embed_as(:fullscreen)
451
465
  ```
452
466
 
467
+ ## Getting Results
468
+
469
+ Typeform I/O uses webhooks to send you the responses to your Typeforms. You can configure the URL by telling it where to send responses to, like this:
470
+
471
+ ```ruby
472
+ class UserTypeform
473
+
474
+ # all the fields ...
475
+
476
+ send_responses_to "https://www.my-awesome-website.com/webhooks"
477
+ end
478
+ ```
479
+
480
+ `AskAwesomely` will warn you if you don't configure this, as Typeform I/O doesn't store the responses for you and they'll be lost in the either.
481
+
482
+ Check the documentation on [results and webhooks](http://docs.typeform.io/docs/results-introduction) to find out more about how this works, what happens when a webhook submission request fails, and how you can deduplicate your submissions.
483
+
453
484
  ## Development
454
485
 
455
486
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_dependency "aws-sdk", "~> 2.0"
21
21
  spec.add_dependency "typhoeus", "~> 0.8"
22
+ spec.add_dependency "erubis", "~> 2.7"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.9"
24
25
  spec.add_development_dependency "rake", "~> 10.0"
data/lib/ask_awesomely.rb CHANGED
@@ -9,8 +9,9 @@ require "ask_awesomely/api_client"
9
9
  require "ask_awesomely/typeform"
10
10
  require "ask_awesomely/embeddable"
11
11
 
12
- require "erb"
13
12
  require "json"
13
+ require "uri"
14
+ require "erubis"
14
15
  require "aws-sdk"
15
16
  require "typhoeus"
16
17
 
@@ -19,6 +20,7 @@ module AskAwesomely
19
20
  ConfigurationError = Class.new(ArgumentError)
20
21
  FieldTypeError = Class.new(ArgumentError)
21
22
  EmbedTypeError = Class.new(ArgumentError)
23
+ InvalidUrlError = Class.new(TypeError)
22
24
 
23
25
  def self.configuration
24
26
  @configuration ||= Configuration.new
@@ -8,7 +8,8 @@ module AskAwesomely
8
8
  :aws_access_key_id,
9
9
  :aws_access_key_secret,
10
10
  :aws_region,
11
- :aws_bucket
11
+ :aws_bucket,
12
+ :logger
12
13
 
13
14
  def typeform_api_key
14
15
  unless @typeform_api_key
@@ -26,5 +27,11 @@ module AskAwesomely
26
27
  @aws_bucket || DEFAULT_AWS_BUCKET
27
28
  end
28
29
 
30
+ def logger
31
+ @logger ||= Logger.new(STDOUT).tap do |logger|
32
+ logger.level = Logger::WARN
33
+ end
34
+ end
35
+
29
36
  end
30
37
  end
@@ -34,17 +34,29 @@ module AskAwesomely
34
34
  def field(type, &block)
35
35
  _state.fields << Field::Field.of_type(type, &block)
36
36
  end
37
+
38
+ def send_responses_to(url)
39
+ unless url =~ /\A#{URI::regexp(['http', 'https'])}\z/
40
+ raise AskAwesomely::InvalidUrlError, "you must use a valid URL for webhooks, e.g https://example.com/webhook"
41
+ end
42
+
43
+ _state.webhook_submit_url = url
44
+ end
37
45
  end
38
46
 
39
47
  attr_reader :context, :json
40
48
 
41
49
  def build_json
50
+ warn_if_no_webhook_set!
51
+
42
52
  state.to_h.reduce({}) do |json, (k, v)|
43
53
  json[k] = case k
44
- when :title, :tags
45
- v.respond_to?(:call) ? v.call(@context) : v
46
54
  when :fields
47
55
  v.map {|f| f.build_json(@context) }
56
+ when :webhook_submit_url
57
+ v
58
+ else
59
+ v.respond_to?(:call) ? v.call(@context) : v
48
60
  end
49
61
 
50
62
  json
@@ -61,5 +73,12 @@ module AskAwesomely
61
73
  self.class._state
62
74
  end
63
75
 
76
+ def warn_if_no_webhook_set!
77
+ return if state.webhook_submit_url
78
+ AskAwesomely.configuration.logger.warn(<<-STR.gsub(/^\s*/, ''))
79
+ Your Typeform has no webhook URL! The responses to this form **will NOT** be saved!
80
+ To set one, add `send_responses_to "https://example.com/webhook"` to #{self.class.name}
81
+ STR
82
+ end
64
83
  end
65
84
  end
@@ -8,6 +8,12 @@ module AskAwesomely
8
8
  fullscreen
9
9
  )
10
10
 
11
+ DEFAULT_OPTIONS = {
12
+ width: 800,
13
+ height: 600,
14
+ button_text: "Launch me!"
15
+ }
16
+
11
17
  def initialize(embed_type)
12
18
  unless EMBED_TYPES.include?(embed_type)
13
19
  raise AskAwesomely::EmbedTypeError, "embed type must be one of: #{EMBED_TYPES.join(', ')}"
@@ -16,8 +22,11 @@ module AskAwesomely
16
22
  @embed_type = embed_type
17
23
  end
18
24
 
19
- def render(typeform)
20
- ERB.new(template, 0, '>').result(typeform.get_binding)
25
+ def render(typeform, options = {})
26
+ locals = DEFAULT_OPTIONS
27
+ .merge(options)
28
+ .merge(public_url: typeform.public_url, title: typeform.title)
29
+ Erubis::Eruby.new(template).result(locals)
21
30
  end
22
31
 
23
32
  private
@@ -1,3 +1,3 @@
1
- <a class="typeform-share link" href="<%= public_url %>" data-mode="2" target="_blank">Launch me!</a>
1
+ <a class="typeform-share link" href="<%= public_url -%>" data-mode="2" target="_blank"><%= button_text -%></a>
2
2
 
3
3
  <script>(function(){var qs,js,q,s,d=document,gi=d.getElementById,ce=d.createElement,gt=d.getElementsByTagName,id='typef_orm',b='https://s3-eu-west-1.amazonaws.com/share.typeform.com/';if(!gi.call(d,id)){js=ce.call(d,'script');js.id=id;js.src=b+'share.js';q=gt.call(d,'script')[0];q.parentNode.insertBefore(js,q)}})()</script>
@@ -1,3 +1,3 @@
1
- <a class="typeform-share link" href="<%= public_url %>" data-mode="1" target="_blank">Launch me!</a>
1
+ <a class="typeform-share link" href="<%= public_url -%>" data-mode="1" target="_blank"><%= button_text -%></a>
2
2
 
3
3
  <script>(function(){var qs,js,q,s,d=document,gi=d.getElementById,ce=d.createElement,gt=d.getElementsByTagName,id='typef_orm',b='https://s3-eu-west-1.amazonaws.com/share.typeform.com/';if(!gi.call(d,id)){js=ce.call(d,'script');js.id=id;js.src=b+'share.js';q=gt.call(d,'script')[0];q.parentNode.insertBefore(js,q)}})()</script>
@@ -1,3 +1,3 @@
1
- <div class="typeform-widget" data-url="<%= public_url %>" data-text="<%= title %>" style="width:100%;height:500px;"></div>
1
+ <div class="typeform-widget" data-url="<%= public_url -%>" data-text="<%= title -%>" style="width:<%= width -%>;height: <%= height -%>;"></div>
2
2
 
3
3
  <script>(function(){var qs,js,q,s,d=document,gi=d.getElementById,ce=d.createElement,gt=d.getElementsByTagName,id='typef_orm',b='https://s3-eu-west-1.amazonaws.com/share.typeform.com/';if(!gi.call(d,id)){js=ce.call(d,'script');js.id=id;js.src=b+'widget.js';q=gt.call(d,'script')[0];q.parentNode.insertBefore(js,q)}})()</script>
@@ -23,8 +23,8 @@ module AskAwesomely
23
23
  }.fetch("href")
24
24
  end
25
25
 
26
- def embed_as(type)
27
- Embeddable.new(type, options).render(binding)
26
+ def embed_as(type, options = {})
27
+ Embeddable.new(type, options).render(self, options)
28
28
  end
29
29
 
30
30
  def update_with_api_response(response)
@@ -36,8 +36,5 @@ module AskAwesomely
36
36
  @structure.build_json
37
37
  end
38
38
 
39
- def get_binding
40
- binding
41
- end
42
39
  end
43
40
  end
@@ -1,3 +1,3 @@
1
1
  module AskAwesomely
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ask_awesomely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Machin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-29 00:00:00.000000000 Z
11
+ date: 2015-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: erubis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement