Class: Formkeep::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/formkeep.rb

Overview

Helper methods for accessing Formkeep API

Author:

  • Brandon Pittman

Instance Attribute Summary collapse

Getters collapse

Parsers collapse

Process collapse

Instance Method Summary collapse

Constructor Details

#initialize(form) ⇒ Formkeep::Form

Creates a Formkeep::Form object to make calling of other methods simpler.

Since:

  • 0.0.4



23
24
25
# File 'lib/formkeep.rb', line 23

def initialize(form)
  @form = form
end

Instance Attribute Details

#formString



17
18
19
# File 'lib/formkeep.rb', line 17

def form
  @form
end

Instance Method Details

#apiString

Sets API endpoint

Since:

  • 0.0.4



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

def api
  get_key(form)
end

#configPstore

Returns YAML::Store object.

Since:

  • 0.0.4



30
31
32
# File 'lib/formkeep.rb', line 30

def config
  YAML::Store.new("#{Dir.home}/.formkeep.yaml")
end

#get_key(key) ⇒ String

Returns value of key.

Since:

  • 0.0.4



37
38
39
40
41
42
# File 'lib/formkeep.rb', line 37

def get_key(key)
  store = config
  store.transaction do
    store[key]
  end
end

#latest_emailString

Returns email of latest submission.

Since:

  • 0.0.4



90
91
92
# File 'lib/formkeep.rb', line 90

def latest_email
  latest_submission.fetch('email')
end

#latest_nameString

Returns name of latest submission.

Since:

  • 0.0.4



84
85
86
# File 'lib/formkeep.rb', line 84

def latest_name
  latest_submission.fetch('name')
end

#latest_submissionHash

Latest submission info

Since:

  • 0.0.4



78
79
80
# File 'lib/formkeep.rb', line 78

def latest_submission
  submissions[0]
end

#read_submissionsArray

Returns all read submissions.

Since:

  • 0.0.4



106
107
108
109
110
# File 'lib/formkeep.rb', line 106

def read_submissions
  submissions.select do |submission|
    submission.fetch('read_at')
  end
end

#responseString

Returns text of API call.

Since:

  • 0.0.4



64
65
66
# File 'lib/formkeep.rb', line 64

def response
  Net::HTTP.get_response(URI(api)).body
end

#set_key(key, value) ⇒ String

Returns value that was added/modified.

Since:

  • 0.0.4



48
49
50
51
52
53
# File 'lib/formkeep.rb', line 48

def set_key(key, value)
  store = config
  store.transaction do
    store[key] = value
  end
end

#sticky(text) ⇒ Nil

Note:

This requires the OS X app, Sticky Notifications.

Since:

  • 0.0.6



118
119
120
# File 'lib/formkeep.rb', line 118

def sticky(text)
  StickyNotifications::Note.new.create(text, 'Formkeep')
end

#submissionsArray

Returns all submissions.

Since:

  • 0.0.4



70
71
72
73
# File 'lib/formkeep.rb', line 70

def submissions
  all = JSON.parse(response)['submissions']
  all.reject { |sub| sub['spam'] }
end

#unread_submissionsArray

Returns all unread submissions.

Since:

  • 0.0.4



98
99
100
101
102
# File 'lib/formkeep.rb', line 98

def unread_submissions
  submissions.reject do |submission|
    submission.fetch('read_at')
  end
end