ask_awesomely 0.1.4 → 0.1.5

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: 7d1822232d0b2c24ecdc6cef57fee6af17ba2f64
4
- data.tar.gz: 31dba1d91b42808d5b5162ba725abb5f9c208d90
3
+ metadata.gz: fef8189f02630e738799f66750c04c1735839c6a
4
+ data.tar.gz: 28f428f626cf0297cd6544a946429716b47f353a
5
5
  SHA512:
6
- metadata.gz: 0f83e055124bbe75c7a4e1417773a1bc1ea95c4d399563655e089fa81881932499964923c7119181a23795b96d9040e5506063ce5621d1a3e4ba9eb2d6970e88
7
- data.tar.gz: d3a9def670b3ad1011948c96d9ec3e32298ac968056a7422074dbabf004131cb4262b3648cda428c596746e47028f63d3482109e98aaf714a848ddbb7e49d5c2
6
+ metadata.gz: 6b3cdd6d5b76a9e02d82e11380b3ff8cf1a63be9f23257092f9ad8e7be736caea26ad17c492f233e8ffa1bc4387363b600370445619b9b69e5f78cb52a1df6b8
7
+ data.tar.gz: b7dfdc73f7e60e9c4225bea064a70b5a9bc6faa299c233ec6b04fd4b7398b8818211e2e2041a531bf52b6f2bfc1ae041e76128091287700735b0700d5579fae9
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.2.2
1
+ 2.2.3
data/README.md CHANGED
@@ -134,7 +134,7 @@ class MyNewTypeform
134
134
  tags "awesome", "hehe"
135
135
 
136
136
  field :statement do
137
- say "Hello, Rubyists!"
137
+ say ->(user) { "Hello, #{user.name}!" }
138
138
  end
139
139
 
140
140
  field :multiple_choice do
@@ -153,7 +153,8 @@ end
153
153
  After that, it's simply a matter of calling `build` on the class:
154
154
 
155
155
  ```ruby
156
- typeform = MyNewTypeform.build
156
+ user = OpenStruct.new(name: "Rubyist")
157
+ typeform = MyNewTypeform.build(user)
157
158
  ```
158
159
 
159
160
  Check out [Typeform I/O](https://typeform.io) for detailed information about the API, and how to get your API key.
@@ -5,7 +5,8 @@ module AskAwesomely
5
5
  BASE_URL = "https://api.typeform.io/latest"
6
6
  ENDPOINTS = {
7
7
  root: "/",
8
- create_typeform: "/forms"
8
+ create_typeform: "/forms",
9
+ create_picture: "/images"
9
10
  }
10
11
 
11
12
  def initialize
@@ -34,6 +35,16 @@ module AskAwesomely
34
35
  end
35
36
  end
36
37
 
38
+ def submit_picture(picture)
39
+ response = request.post(
40
+ url_for(:create_picture),
41
+ headers: headers,
42
+ body: picture.to_json
43
+ )
44
+
45
+ JSON.parse(response.body)
46
+ end
47
+
37
48
  private
38
49
 
39
50
  def request
@@ -1,10 +1,21 @@
1
1
  module AskAwesomely
2
2
  class Choice
3
3
 
4
+ attr_reader :state, :picture
5
+
4
6
  def initialize(label:, picture: nil)
5
- @label = label
7
+ @state = OpenStruct.new(label: label.to_s)
6
8
  @picture = picture && Picture.new(picture)
7
9
  end
8
-
10
+
11
+ def build_json(context = nil)
12
+ @state.image_id = picture && picture.typeform_id
13
+
14
+ state.to_h.reduce({}) do |json, (k, v)|
15
+ json[k] = v.respond_to?(:call) ? v.call(context) : v
16
+ json
17
+ end
18
+ end
19
+
9
20
  end
10
21
  end
@@ -50,13 +50,12 @@ module AskAwesomely
50
50
  warn_if_no_webhook_set!
51
51
 
52
52
  state.to_h.reduce({}) do |json, (k, v)|
53
- json[k] = case k
54
- when :fields
55
- v.map {|f| f.build_json(@context) }
56
- when :webhook_submit_url
57
- v
53
+ json[k] = case
54
+ when v.respond_to?(:to_ary) then v.map {|f| f.respond_to?(:build_json) ? f.build_json(context) : f }
55
+ when v.respond_to?(:call) then v.call(context)
56
+ when v.respond_to?(:build_json) then v.build_json(context)
58
57
  else
59
- v.respond_to?(:call) ? v.call(@context) : v
58
+ v
60
59
  end
61
60
 
62
61
  json
@@ -63,7 +63,13 @@ module AskAwesomely
63
63
 
64
64
  def build_json(context = nil)
65
65
  state.to_h.reduce({}) do |json, (k, v)|
66
- json[k] = v.respond_to?(:call) ? v.call(context) : v
66
+ json[k] = case
67
+ when v.respond_to?(:to_ary) then v.map {|f| f.respond_to?(:build_json) ? f.build_json(context) : f }
68
+ when v.respond_to?(:call) then v.call(context)
69
+ when v.respond_to?(:build_json) then v.build_json(context)
70
+ else
71
+ v
72
+ end
67
73
  json
68
74
  end
69
75
  end
@@ -3,10 +3,10 @@ module AskAwesomely
3
3
 
4
4
  def initialize(*)
5
5
  super
6
- @state.choices = []
7
6
  end
8
7
 
9
8
  def choice(label, picture:)
9
+ @state.choices ||= []
10
10
  @state.choices << Choice.new(label: label, picture: picture)
11
11
  end
12
12
 
@@ -23,6 +23,16 @@ module AskAwesomely
23
23
  end
24
24
  end
25
25
 
26
+ def typeform_id
27
+ @id ||= upload_to_typeform["id"]
28
+ end
29
+
30
+ def to_json(*)
31
+ {
32
+ url: public_url,
33
+ }.to_json
34
+ end
35
+
26
36
  private
27
37
 
28
38
  def url?
@@ -37,5 +47,9 @@ module AskAwesomely
37
47
  S3.upload(Pathname.new(file_or_url))
38
48
  end
39
49
 
50
+ def upload_to_typeform
51
+ ApiClient.new.submit_picture(self)
52
+ end
53
+
40
54
  end
41
55
  end
@@ -1,3 +1,3 @@
1
1
  module AskAwesomely
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/ask_awesomely.rb CHANGED
@@ -1,3 +1,10 @@
1
+ require "forwardable"
2
+ require "json"
3
+ require "uri"
4
+ require "erubis"
5
+ require "aws-sdk"
6
+ require "typhoeus"
7
+
1
8
  require "ask_awesomely/version"
2
9
  require "ask_awesomely/configuration"
3
10
  require "ask_awesomely/dsl"
@@ -9,12 +16,6 @@ require "ask_awesomely/api_client"
9
16
  require "ask_awesomely/typeform"
10
17
  require "ask_awesomely/embeddable"
11
18
 
12
- require "json"
13
- require "uri"
14
- require "erubis"
15
- require "aws-sdk"
16
- require "typhoeus"
17
-
18
19
  module AskAwesomely
19
20
 
20
21
  ConfigurationError = Class.new(ArgumentError)
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.4
4
+ version: 0.1.5
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-10-02 00:00:00.000000000 Z
11
+ date: 2015-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -174,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  version: '0'
175
175
  requirements: []
176
176
  rubyforge_project:
177
- rubygems_version: 2.4.5
177
+ rubygems_version: 2.4.5.1
178
178
  signing_key:
179
179
  specification_version: 4
180
180
  summary: Create Typeforms on the fly, the Typeform way.