Module: PinPress

Defined in:
lib/pinpress.rb,
lib/pinpress/constants.rb,
lib/pinpress/templates/template.rb,
lib/pinpress/templates/pin_template.rb,
lib/pinpress/templates/tag_template.rb

Overview

The PinPress module, which wraps everything in this gem.

Defined Under Namespace

Classes: PinTemplate, TagTemplate, Template

Constant Summary collapse

CONFIG_FILEPATH =

The default local filepath of the Siftter Redux config file

File.join(ENV['HOME'], '.pinpress')
LOG_FILEPATH =

The default local filepath of the Siftter Redux log file

File.join(ENV['HOME'], '.pinpress_log')
DESCRIPTION =

The Gem’s description

%q(A Pinboard application that allows for the creation of
pin and tag templates in almost any conceivable format.)
NEWEST_CONFIG_VERSION =

The last version to require a config update

'1.2.1'
PREF_FILES =

Hash of preference files

{
  'INIT' => File.join(
    File.dirname(__FILE__),
    '..',
    '..',
    'support/preference_prompts.yaml'
  )
}
SUMMARY =

The Gem’s summary

'A simple CLI to create HTML templates of Pinboard data.'
VERSION =

The Gem’s version

'1.2.3'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.initializedBoolean (readonly)

Stores whether initalization has completed.

Returns:

  • (Boolean)


12
13
14
# File 'lib/pinpress.rb', line 12

def initialized
  @initialized
end

.verboseBoolean

Stores whether verbose output is turned on.

Returns:

  • (Boolean)


16
17
18
# File 'lib/pinpress.rb', line 16

def verbose
  @verbose
end

Class Method Details

.execute_template(template_type, template_name) { ... } ⇒ void

This method returns an undefined value.

Establishes the template to use and yields a block with that template an a Pinboard client.

Parameters:

  • template_type (Fixnum)

    Either a Pin or Tag template

  • template_name (String)

    The neame of the template to use

Yields:



25
26
27
28
29
30
31
32
33
34
# File 'lib/pinpress.rb', line 25

def self.execute_template(template_type, template_name)
  template_hash = PinPress.get_template_by_name(template_type, template_name)
  if template_hash
    template = PinPress::TagTemplate.new(template_hash)
    client = Pinboard::Client.new(token: configuration.pinpress[:api_token])
    yield template, client
  else
    fail 'Invalid template provided and/or no default template'
  end
end

.generate_items(template_type, template, pins, opts) ⇒ String

Generates a string of items from pins.

Parameters:

Returns:

  • (String)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pinpress.rb', line 41

def self.generate_items(template_type, template, pins, opts)
  output = ''
  case template_type
  when PinPress::Template::TEMPLATE_TYPE_PIN
    pins.each do |p|
      href        = p[:href]
      description = p[:description]
      extended    = p[:extended]
      tag         = p[:tag]
      time        = p[:time]
      replace     = p[:replace]
      shared      = p[:shared]
      toread      = p[:toread]
      output += ERB.new(template.item).result(binding)
    end
    configuration.pinpress[:last_pins_run] = Date.today
  when PinPress::Template::TEMPLATE_TYPE_TAG
    tags = []
    pins.each { |p| tags += p[:tag] }
    tags = tags.uniq.map { |t| { tag: t, count: tags.count(t) } }
    tags.each do |t|
      unless t[:tag] == opts[:tag]
        tag   = t[:tag]
        count = t[:count]
        output += ERB.new(template.item).result(binding)
      end
    end
    configuration.pinpress[:last_tags_run] = Date.today
  end
  output
end

.get_data(template_type, args, options) ⇒ String

Generic function to get data from Pinboard.

Parameters:

  • template_type (Fixnum)
  • args (Array)
  • options (Hash)

Returns:

  • (String)


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
120
121
122
123
124
125
126
127
128
# File 'lib/pinpress.rb', line 78

def self.get_data(template_type, args, options)
  output = ''

  # Two scenarios covered here:
  #   1. If the user passes a valid name, grab that template.
  #   2. If no name is passed, grabbed the default template
  # If both of these conditions fail, an error message is shown.
  # t_type = PinPress::Template::TEMPLATE_TYPE_PIN
  # t_name = args.empty? ? nil : args[0]
  t_name = args.empty? ? nil : args[0]

  PinPress.execute_template(template_type, t_name) do |template, client|
    opts = {}
    opts.merge!(todt: Chronic.parse(options[:e])) if options[:e]
    opts.merge!(fromdt: Chronic.parse(options[:s])) if options[:s]

    if options[:n]
      opts.merge!(results: options[:n])
    elsif configuration.pinpress[:default_num_results]
      opts.merge!(results: configuration.pinpress[:default_num_results])
    end

    if options[:t]
      tags = options[:t].split(',')
    elsif configuration.pinpress[:default_tags]
      tags = configuration.pinpress[:default_tags]
    end

    ignored_tags = configuration.pinpress[:ignored_tags]
    tags -= ignored_tags if ignored_tags
    opts.merge!(tag: tags) if tags

    begin
      pins = client.posts(opts)
      if pins.empty?
        messenger.warn('No matching pins...')
      else
        output += template.opener if template.opener
        output += PinPress.generate_items(template_type, template, pins, opts)
        output += template.closer if template.closer
      end

      configuration.save
      return output
    rescue StandardError => e
      messenger.debug(e.to_s)
      raise "Pinboard API failed; are you sure you've run " \
           " `pinpress init` (and that your API key is correct)?"
    end
  end
end

.get_template_by_name(template_type, template_name = nil) ⇒ Template

Determines whether a template exists.

Parameters:

  • template_type (Fixnum)
  • template_name (<String, Symbol>) (defaults to: nil)

Returns:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/pinpress.rb', line 134

def self.get_template_by_name(template_type, template_name = nil)
  case template_type
  when Template::TEMPLATE_TYPE_PIN
    default_t_name = configuration.pinpress[:default_pin_template]
    templates = configuration.pin_templates
  when Template::TEMPLATE_TYPE_TAG
    default_t_name = configuration.pinpress[:default_tag_template]
    templates = configuration.tag_templates
  else
    fail 'Invalid template type given'
  end

  if template_name.nil?
    return templates.find { |t| t[:name] == default_t_name }
  else
    return templates.find { |t| t[:name] == template_name }
  end
end

.init(from_scratch = false) ⇒ void

This method returns an undefined value.

Initializes PinPress by downloading and collecting all necessary items and info.

Parameters:

  • from_scratch (Boolean) (defaults to: false)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pinpress.rb', line 157

def self.init(from_scratch = false)
  messenger.section('INITIALIZING...')
  if from_scratch
    configuration.reset
    configuration.add_section(:pinpress)
    configuration.add_section(:pin_templates)
    configuration.add_section(:tag_templates)

    configuration.pinpress.merge!(
      config_location: configuration.config_path,
      default_pin_template: 'pinpress_default',
      default_tag_template: 'pinpress_default',
      log_level: 'WARN',
      version: PinPress::VERSION
    )

    default_pin_template = {
      name: 'pinpress_default',
      opener: '<ul>',
      item: '<li><b><a title="<%= description %>" href="<%= href %>" target="_blank"><%= description %></a>.</b> <%= extended %></li>',
      closer: '</ul>'
    }

    default_tag_template = {
      name: 'pinpress_default',
      item: '<%= tag %> (<%= count %>),'
    }

    configuration.data['pin_templates'] = [default_pin_template]
    configuration.data['tag_templates'] = [default_tag_template]
  end

  pm = CLIUtils::Prefs.new(PinPress::PREF_FILES['INIT'], configuration)
  pm.ask
  configuration.ingest_prefs(pm)

  messenger.debug("Configuration values after pref collection: #{ configuration.data }")

  configuration.save
  @initialized = true
end

.list_templatesvoid

This method returns an undefined value.

Present a list of installed templates to the user



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/pinpress.rb', line 201

def self.list_templates
  pin_templates = configuration.pin_templates
  tag_templates = configuration.tag_templates

  messenger.section('AVAILABLE PIN TEMPLATES:')
  if pin_templates
    pin_templates.each_with_index do |template, index|
      messenger.info("#{ index + 1 }. #{ template[:name] }")
    end
  else
    messenger.warn('No templates defined...')
  end

  messenger.section('AVAILABLE TAG TEMPLATES:')
  if tag_templates
    tag_templates.each_with_index do |template, index|
      messenger.info("#{ index + 1 }. #{ template[:name] }")
    end
  else
    messenger.warn('No templates defined...')
  end
end

.update_config_filevoid

This method returns an undefined value.

Notifies the user that the config file needs to be re-done and does it.



227
228
229
230
231
232
233
234
# File 'lib/pinpress.rb', line 227

def self.update_config_file
  m = "This version needs to make some config changes. Don't worry; " \
      "when prompted, your current values for existing config options " \
      "will be presented (so it'll be easier to fly through the upgrade)."
  messenger.info(m)
  messenger.prompt('Press enter to continue')
  PinPress.init(true)
end