Class: Formkeep::Form

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

Overview

Helper methods for accessing Formkeep API

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.

Parameters:

  • form (String)

    name from ‘~/.formkeep.yaml`

Since:

  • 0.0.4



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

def initialize(form)
  @form = form
end

Instance Attribute Details

#formString

Returns name of Form to be used.

Returns:

  • (String)

    name of Form to be used



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

def form
  @form
end

Instance Method Details

#apiString

Sets API endpoint

Returns:

  • (String)

    API endpoint URL

Since:

  • 0.0.4



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

def api
  get_key(form)
end

#configPstore

Returns YAML::Store object.

Returns:

  • (Pstore)

    YAML::Store object

Since:

  • 0.0.4



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

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

#get_key(key) ⇒ String

Returns value of key.

Parameters:

  • key (String)

    to look up

Returns:

  • (String)

    value of key

Since:

  • 0.0.4



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

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

#latest_emailString

Returns email of latest submission.

Returns:

  • (String)

    email of latest submission

Since:

  • 0.0.4



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

def latest_email
  latest_submission.fetch("email")
end

#latest_nameString

Returns name of latest submission.

Returns:

  • (String)

    name of latest submission

Since:

  • 0.0.4



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

def latest_name
  latest_submission.fetch("name")
end

#latest_submissionHash

Latest submission info

Returns:

  • (Hash)

    first submission from submissions array

Since:

  • 0.0.4



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

def latest_submission
  submissions[0]
end

#read_submissionsArray

Returns all read submissions.

Returns:

  • (Array)

    all read submissions

Since:

  • 0.0.4



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

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

#responseString

Returns text of API call.

Returns:

  • (String)

    text of API call

Since:

  • 0.0.4



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

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

#set_key(key, value) ⇒ String

Returns value that was added/modified.

Parameters:

  • key (String)

    to add/modify

  • value (String)

    to add/modify

Returns:

  • (String)

    value that was added/modified

Since:

  • 0.0.4



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

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

#sticky(text) ⇒ Object



115
116
117
# File 'lib/formkeep.rb', line 115

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

#submissionsArray

Returns all submissions.

Returns:

  • (Array)

    all submissions

Since:

  • 0.0.4



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

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

#unread_submissionsArray

Returns all unread submissions.

Returns:

  • (Array)

    all unread submissions

Since:

  • 0.0.4



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

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