Class: Rubytypeformio::Form

Inherits:
Base
  • Object
show all
Defined in:
lib/rubytypeformio/form.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#compact, #pretty

Constructor Details

#initialize(title, webhook, design_id, fields) ⇒ Form

Returns a new instance of Form.



28
29
30
31
32
33
# File 'lib/rubytypeformio/form.rb', line 28

def initialize (title, webhook, design_id, fields)
  @title = title
  @webhook_submit_url = webhook
  @design_id = design_id
  @fields = fields
end

Instance Attribute Details

#design_idObject

Returns the value of attribute design_id.



26
27
28
# File 'lib/rubytypeformio/form.rb', line 26

def design_id
  @design_id
end

#fieldsObject

Returns the value of attribute fields.



26
27
28
# File 'lib/rubytypeformio/form.rb', line 26

def fields
  @fields
end

#idObject

Returns the value of attribute id.



26
27
28
# File 'lib/rubytypeformio/form.rb', line 26

def id
  @id
end

Returns the value of attribute links.



26
27
28
# File 'lib/rubytypeformio/form.rb', line 26

def links
  @links
end

#titleObject

Returns the value of attribute title.



26
27
28
# File 'lib/rubytypeformio/form.rb', line 26

def title
  @title
end

#webhook_submit_urlObject

Returns the value of attribute webhook_submit_url.



26
27
28
# File 'lib/rubytypeformio/form.rb', line 26

def webhook_submit_url
  @webhook_submit_url
end

Class Method Details

.from_json(string) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rubytypeformio/form.rb', line 62

def self.from_json(string)
  data = JSON.load(string)
  fields = []
  data["fields"].each { |f|

    # @TODO: figure out a cleaner way to do this case statement
    case f["type"]
      when 'short_text'
        fields.push(Rubytypeformio::ShortTextField.from_json(f.to_json))
      when 'long_text_field'
        fields.push(Rubytypeformio::LongTextField.from_json(f.to_json))
      when 'statement'
        fields.push(Rubytypeformio::StatementField.from_json(f.to_json))
      when 'multiple_choice'
        fields.push(Rubytypeformio::MultipleChoiceField.from_json(f.to_json))
      when 'picture_choice'
        fields.push(Rubytypeformio::PictureChoiceField.from_json(f.to_json))
      when 'dropdown'
        fields.push(Rubytypeformio::DropdownField.from_json(f.to_json))
      when 'yes_no'
        fields.push(Rubytypeformio::YesNoField.from_json(f.to_json))
      when 'number'
        fields.push(Rubytypeformio::NumberField.from_json(f.to_json))
      when 'rating'
        fields.push(Rubytypeformio::RatingField.from_json(f.to_json))
      when 'opinion_scale'
        fields.push(Rubytypeformio::OpinionField.from_json(f.to_json))
      when 'email'
        fields.push(Rubytypeformio::EmailField.from_json(f.to_json))
      when 'website'
        fields.push(Rubytypeformio::WebsiteField.from_json(f.to_json))
      when 'legal'
        fields.push(Rubytypeformio::LegalField.from_json(f.to_json))
      else
        fields.push(Rubytypeformio::Field.from_json(f.to_json))
    end

  }

  obj = self.new(data["title"],
                 data["webhook_submit_url"],
                 data["design_id"],
                 fields)

  if (data["links"])
    links = []
    data["links"].each { |l|
      links.push(Rubytypeformio::Link.from_json(l.to_json))
    }
    obj.links = links
  end

  if (data["id"])
    obj.id = data["id"]
  end

  return obj
end

Instance Method Details

#postForm

Returns:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rubytypeformio/form.rb', line 122

def post

  if (ENV["TYPEFORM_KEY"].nil?)
    puts "no typeform key set"
    return nil
  end

  puts 'creating new form with API'

  conn = Faraday.new(:url => 'https://api.typeform.io') do |faraday|
    faraday.request :url_encoded
    faraday.response :logger
    faraday.adapter Faraday.default_adapter
  end

  resp = conn.post do |req|
    req.url 'https://api.typeform.io/v0.3/forms'
    req.headers['x-api-token'] = ENV["TYPEFORM_KEY"]
    req.body = self.compact(self.to_h).to_json.to_s

    puts "REQUEST: " + req.to_s
  end

  return Rubytypeformio::Form.from_json(resp.body)
end

#to_hObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubytypeformio/form.rb', line 35

def to_h
  hash = {
      :id => @id,
      :title => @title,
      :design_id => @design_id,
      :webhook_submit_url => @webhook_submit_url
  }

  hash[:fields] = []
  @fields.each { |f|
    hash[:fields].push(f.to_h)
  }
  hash[:links] = []
  if (!@links.nil?)
    @links.each { |l|
      hash[:links].push(l.to_h)
    }
  end

  return hash

end

#to_jsonObject



58
59
60
# File 'lib/rubytypeformio/form.rb', line 58

def to_json
  JSON.dump(self.to_h)
end