lib/pinpress.rb in pinpress-1.2.0 vs lib/pinpress.rb in pinpress-1.2.1
- old
+ new
@@ -22,19 +22,113 @@
# @param [String] template_name The neame of the template to use
# @yield
# @return [void]
def self.execute_template(template_type, template_name)
template_hash = PinPress.get_template_by_name(template_type, template_name)
- if !template_hash.nil?
+ 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
+ # Generates a string of items from pins.
+ # @param [Fixnum] template_type
+ # @param [PinPress::Template] template
+ # @param [Array] pins
+ # @return [String]
+ 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
+
+ # Generic function to get data from Pinboard.
+ # @param [Fixnum] template_type
+ # @param [Array] args
+ # @param [Hash] options
+ # @return [String]
+ 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
+ p e.to_s
+ 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
+
# Determines whether a template exists.
# @param [Fixnum] template_type
# @param [<String, Symbol>] template_name
# @return [Template]
def self.get_template_by_name(template_type, template_name = nil)
@@ -108,19 +202,19 @@
def self.list_templates
pin_templates = configuration.pin_templates
tag_templates = configuration.tag_templates
messenger.section('AVAILABLE PIN TEMPLATES:')
- if !pin_templates.nil?
+ 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.nil?
+ if tag_templates
tag_templates.each_with_index do |template, index|
messenger.info("#{ index + 1 }. #{ template[:name] }")
end
else
messenger.warn('No templates defined...')