Class: Formstack::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/formstack/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/formstack/client.rb', line 7

def initialize(key)
  self.class.default_params(:api_key => key, :type => 'json')
end

Class Method Details

.get(*args) ⇒ Object



68
# File 'lib/formstack/client.rb', line 68

def self.get(*args); handle_response super end

.handle_response(response) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/formstack/client.rb', line 71

def self.handle_response(response)
  case response.code
  when 500...600; raise FormstackError.new(Hashie::Mash.new(response).error)
  else; response
  end
  
  Hashie::Mash.new(response).response
  
end

.post(*args) ⇒ Object



69
# File 'lib/formstack/client.rb', line 69

def self.post(*args); handle_response super end

Instance Method Details

#data(form_id, options = {}) ⇒ Object Also known as: submissions



19
20
21
22
23
# File 'lib/formstack/client.rb', line 19

def data(form_id, options={})
  api_response = self.class.get("/data", :query => {:id => form_id}.merge(options))
  api_response.submissions = api_response.submissions.map{|s| handle_submission(s) }
  api_response
end

#delete(submission_id) ⇒ Object



39
40
41
# File 'lib/formstack/client.rb', line 39

def delete(submission_id)
  self.class.post("/delete", :body => {:id => submission_id})['id']
end

#edit(submission_id, options = {}) ⇒ Object



35
36
37
# File 'lib/formstack/client.rb', line 35

def edit(submission_id, options={})
  self.class.post("/edit", :body => {:id => submission_id}.merge(prepare_params(submission_id, options)))['id']
end

#form(form_id) ⇒ Object



15
16
17
# File 'lib/formstack/client.rb', line 15

def form(form_id)
  handle_form(self.class.get("/form", :query => {:id => form_id}))
end

#formsObject



11
12
13
# File 'lib/formstack/client.rb', line 11

def forms
  self.class.get("/forms").forms.map{|f| handle_form(f)}
end

#handle_form(form) ⇒ Object



54
55
56
57
# File 'lib/formstack/client.rb', line 54

def handle_form(form)
  form.created = Time.parse(form.created) unless form.created.nil?
  form
end

#handle_submission(submission) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/formstack/client.rb', line 59

def handle_submission(submission)
  data = submission.data      
  data.each do |datum|
    submission[datum.field] = datum.value
  end
  submission.timestamp = Time.parse(submission.timestamp)
  submission
end

#prepare_params(form_id, params = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/formstack/client.rb', line 44

def prepare_params(form_id, params={})
  data = params.delete(:data)
  form = form(form_id)
  data.each do |field, value|
    field_id = form.fields.any? {|f| f['name'] == field.to_s} ? form.fields.find {|f| f['name'] == field.to_s}['id'] : field
    params["field_#{field_id}"] = value
  end
  params
end

#submission(submission_id, options = {}) ⇒ Object



26
27
28
# File 'lib/formstack/client.rb', line 26

def submission(submission_id, options={})
  handle_submission(self.class.get("/submission", :query => {:id => submission_id}.merge(options)))
end

#submit(form_id, options = {}) ⇒ Object



31
32
33
# File 'lib/formstack/client.rb', line 31

def submit(form_id, options={})
  self.class.post("/submit", :body => {:id => form_id}.merge(prepare_params(form_id, options)))['id']
end