module PinPress

The PinPress module, which wraps everything in this gem.

The PinPress module, which wraps everything in this gem.

Constants

CONFIG_FILEPATH

The default local filepath of the Siftter Redux config file

DESCRIPTION

The Gem’s description

LOG_FILEPATH

The default local filepath of the Siftter Redux log file

NEWEST_CONFIG_VERSION

The last version to require a config update

PREF_FILES

Hash of preference files

SUMMARY

The Gem’s summary

VERSION

The Gem’s version

Attributes

initialized[R]

Stores whether initalization has completed. @return [Boolean]

verbose[RW]

Stores whether verbose output is turned on. @return [Boolean]

Public Instance Methods

execute_template(pinboard_opts) { |pins| ... } click to toggle source

Grabs Pinboard data (passed on passed options) and yields a block that allows the user to act upon that returned data. @param [Hash] pinboard_opts @yield pins @raise StandardError if Pinboard client fails

# File lib/pinpress.rb, line 56
def execute_template(pinboard_opts)
  begin
    client = Pinboard::Client.new(token: configuration.pinpress.api_token)
    pins = client.posts(pinboard_opts)
    if pins.empty?
      messenger.warn('No data matching your arguments...')
    else
      yield pins
    end
  rescue StandardError => e
    messenger.debug(e.to_s)
    puts e.to_s
    raise "Pinboard API failed; are you sure you've run " \
      " `pinpress init` (and that your API key is correct)?"
  end
end
get_template(template_name, template_type) click to toggle source

Returns a template Hash from the configuration file. @param [String] template_name The name of the template to get @param [Fixnum] template_type The template type @return [Hash]

# File lib/pinpress.rb, line 77
def get_template(template_name, template_type)
  case template_type
  when PinPress::Template::TYPE_PIN
    configuration.pin_templates.find { |t| t.keys[0] == template_name.to_sym}.values[0]
  when PinPress::Template::TYPE_TAG
    configuration.tag_templates.find { |t| t.keys[0] == template_name.to_sym}.values[0]
  end
end
init(from_scratch = false) click to toggle source

Initializes PinPress by downloading and collecting all necessary items and info. @param [Boolean] from_scratch @return [void]

# File lib/pinpress.rb, line 115
def init(from_scratch = false)
  messenger.section('INITIALIZING...')
  if from_scratch
    configuration.reset

    # Add initial configuration info.
    configuration.add_section(:pinpress)
    configuration.add_section(:links)
    configuration.pinpress = {
      config_location: configuration.config_path,
      default_pin_template: 'pinpress_default',
      default_tag_template: 'pinpress_default',
      log_level: 'WARN',
      version: PinPress::VERSION
    }

    # Add a single default pin and tag template
    default_pin_template = {
      pinpress_default: {
        opener: '<ul>',
        item: '<li><%= href %></li>',
        closer: '</ul>'
      }
    }
    default_tag_template = {
      pinpress_default: {
        item: '<%= tag %> (<%= count %>),'
      }
    }
    configuration.pin_templates = [default_pin_template]
    configuration.tag_templates = [default_tag_template]
  end

  # Run through installation prompts and ingest the results into
  # the configuration.
  pm = CLIUtils::Prefs.new(PinPress::PREF_FILES['INIT'], configuration)
  pm.ask
  configuration.ingest_prefs(pm)

  m = "Configuration values after pref collection: #{ configuration.data }"
  messenger.debug(m)
  configuration.save
  @initialized = true
end
init_template(explicit_template, template_type) click to toggle source

“Initializes” a passed template:

1. If the template exists, returns it.
2. If not, return a default template (if it exists).
3. Throw an exception if #1 and #2 fail.

@param [String] explicit_template A template name passed in via the CLI @param [Fixnum] template_type The template type @return [Hash] @raise StandardError if no explicit or default template is found

# File lib/pinpress.rb, line 94
def init_template(explicit_template, template_type)
  pin_t_sym = :default_pin_template
  tag_t_sym = :default_tag_template
  s = (template_type == PinPress::Template::TYPE_PIN ? pin_t_sym : tag_t_sym)
  default_template = configuration.pinpress[s]

  if explicit_template && PinPress.is_template?(explicit_template, template_type)
    messenger.debug("Using explicit template: #{ explicit_template }")
    return explicit_template, PinPress.get_template(explicit_template, template_type)
  elsif PinPress.is_template?(default_template, template_type)
    messenger.debug("Using default template: #{ default_template }")
    return default_template, PinPress.get_template(default_template, template_type)
  else
    fail 'Invalid template specified and/or no default template defined'
  end
end
is_template?(template_name, template_type) click to toggle source

Determines whether a template exists in the configuration file. @param [String] template_name The name of the template to search for @param [Fixnum] template_type The template type

# File lib/pinpress.rb, line 163
def is_template?(template_name, template_type)
  case template_type
  when PinPress::Template::TYPE_PIN
    templates = configuration.pin_templates
  when PinPress::Template::TYPE_TAG
    templates = configuration.tag_templates
  end
  !templates.find { |t| t.keys[0] == template_name.to_sym }.nil?
end
list_templates() click to toggle source

Present a list of installed templates to the user @return [void]

# File lib/pinpress.rb, line 223
def list_templates
  %w(pin tag).each do |type|
    templates = configuration.send("#{ type }_templates")
    messenger.section("AVAILABLE #{ type.upcase } TEMPLATES:")
    if templates
      templates.each_with_index do |template, index|
        template_name, template = template.first
        puts "#{ index + 1 }.\tName:   ".blue + "#{ template_name }"
        puts "Opener:".blue.rjust(22) + "\t#{ template[:opener] }".truncate(80)
        puts "Item:".blue.rjust(22) + "\t#{ template[:item] }".truncate(80)
        puts "Closer:".blue.rjust(22) + "\t#{ template[:closer] }".truncate(80)
      end
    else
      messenger.warn('No templates defined...')
    end
  end
end
merge_common_options(options, template_name, template_type) click to toggle source

Helper method to merge command line options that are relevant for both pin and tag requests. @param [Hash] options @raise StandardError if an invalid combo of linking options is given @return [Hash]

# File lib/pinpress.rb, line 246
def merge_common_options(options, template_name, template_type)
  case template_type
  when PinPress::Template::TYPE_PIN
    section = configuration.pin_templates.find { |t| t.keys[0] == template_name.to_sym}.values[0]
  when PinPress::Template::TYPE_TAG
    section = configuration.tag_templates.find { |t| t.keys[0] == template_name.to_sym}.values[0]
  end

  opts = {}
  if options[:n]
    opts.merge!(results: options[:n])
  elsif section.default_num_results
    opts.merge!(results: section.default_num_results)
  end

  if options[:t]
    opts.merge!(tag: options[:t])
  elsif section.default_tags
    opts.merge!(tag: section.default_tags.join(','))
  end

  # These options are PinPress-related, not necessarily Pinboard-related;
  # for the sake of convenience, they're included here.

  # Auto-linking and prompting for link text don't go together, so make
  # sure to let the user know if they include both.
  if conflicting_link_opts?(options)
    fail "You can't specify (a) both the `-a` and `-l` switches or " \
      "(b) both the `auto_link` and `manual_link` configuration options."
  else
    link_opts = determine_link_opts({
      a_switch: options[:a],
      m_switch: options[:l],
      a_config: configuration.pinpress.auto_link,
      m_config: configuration.pinpress.manual_link
    })
    opts.merge!(link_opts) if link_opts
  end
  opts
end
pin_yield(template, opts) click to toggle source

Creates text output from pin data (based upon a passed template). @param [Hash] template The template to use @param [Hash] opts CLI options to use @return [String]

# File lib/pinpress.rb, line 291
def pin_yield(template, opts)
  output = ''
  PinPress.execute_template(opts) do |data|
    output += template[:opener] if template[:opener]
    data.each do |i|
      name = HTMLEntities.new.encode(i[:description])

      if opts[:link]
        desc = link_urls_in_desc(name, i[:extended], :MANUAL)
      elsif opts[:auto_link]
        desc = link_urls_in_desc(name, i[:extended], :AUTO)
      else
        desc = i[:extended]
      end

      href        = i[:href]
      description = name
      extended    = desc
      tag         = i[:tag]
      time        = i[:time]
      replace     = i[:replace]
      shared      = i[:shared]
      toread      = i[:toread]
      output += ERB.new(template[:item]).result(binding)
    end
    output += template[:closer] if template[:closer]
  end
  output
end
tag_yield(template, opts) click to toggle source

Creates text output from tag data (based upon a passed template). @param [Hash] template The template to use @param [Hash] opts CLI options to use @return [String]

# File lib/pinpress.rb, line 325
def tag_yield(template, opts)
  output = ''
  PinPress.execute_template(opts) do |data|
    tags = []
    data.each { |i| tags += i[:tag] }
    tags = tags.uniq.map do |t|
      { tag: t, count: tags.count(t) }
    end

    output += template[:opener] if template[:opener]
    tags.each do |t|
      unless t[:tag] == opts[:tag]
        tag   = t[:tag]
        count = t[:count]
        output += ERB.new(template.item).result(binding)
      end
    end
    output += template[:closer] if template[:closer]
  end
  output
end
update_config_file() click to toggle source

Notifies the user that the config file needs to be re-done and does it. @return [void]

# File lib/pinpress.rb, line 350
def 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