Class: Middleman::Tapirgo::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-tapirgo/syncer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Syncer

Returns a new instance of Syncer.



7
8
9
# File 'lib/middleman-tapirgo/syncer.rb', line 7

def initialize(options={})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/middleman-tapirgo/syncer.rb', line 6

def options
  @options
end

Instance Method Details

#instObject



11
12
13
# File 'lib/middleman-tapirgo/syncer.rb', line 11

def inst
  ::Middleman::Application.server.inst
end

#send_to_tapirgo(item) ⇒ Object



38
39
40
41
42
# File 'lib/middleman-tapirgo/syncer.rb', line 38

def send_to_tapirgo(item)
  req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
  req.body = JSON.generate(item)
  Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
end

#syncObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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_itemsObject



19
20
21
# File 'lib/middleman-tapirgo/syncer.rb', line 19

def syncable_items
  @syncable_items ||= inst.sitemap.resources.select { |r| r.ext == '.html' }
end

#uriObject



15
16
17
# File 'lib/middleman-tapirgo/syncer.rb', line 15

def uri
  URI("http://tapirgo.com/api/1/push_article.json?secret=#{options[:api_key]}")
end