class Middleman::Tapirgo::Syncer

Attributes

options[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/middleman-tapirgo/syncer.rb, line 7
def initialize(options={})
  @options = options
end

Public Instance Methods

inst() click to toggle source
# File lib/middleman-tapirgo/syncer.rb, line 11
def inst
  ::Middleman::Application.server.inst
end
send_to_tapirgo(item) click to toggle source
# File lib/middleman-tapirgo/syncer.rb, line 38
def send_to_tapirgo(item)
  req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}", initheader = {'Content-Type' =>'application/json'})
  req.body = JSON.generate(item)
  Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
end
sync() click to toggle source
# File lib/middleman-tapirgo/syncer.rb, line 23
def sync
  return unless options[:api_key]

  syncable_items.each do |r|
    syncable_item = Middleman::Tapirgo::SyncableItem.new(r)
    response = send_to_tapirgo(syncable_item)
    if response.code != '200'
      puts "Failed sending #{syncable_item.link} to TapirGo"
      puts "Response #{response.code} #{response.message}"
      break
    end
  end
  puts "Synced #{syncable_items.length} items to TapirGo"
end
syncable_items() click to toggle source
# File lib/middleman-tapirgo/syncer.rb, line 19
def syncable_items
  @syncable_items ||= inst.sitemap.resources.select { |r| r.ext == '.html' }
end
uri() click to toggle source
# File lib/middleman-tapirgo/syncer.rb, line 15
def uri
  URI("http://tapirgo.com/api/1/push_article.json?secret=#{options[:api_key]}")
end