Module: ESS::Pusher

Defined in:
lib/ess/pusher.rb

Class Method Summary collapse

Class Method Details

.aggregatorsObject



13
14
15
# File 'lib/ess/pusher.rb', line 13

def self.aggregators
  @@aggregators ||= ["http://api.hypecal.com/v1/ess/aggregator.json"]
end

.aggregators=(aggs) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
# File 'lib/ess/pusher.rb', line 8

def self.aggregators= aggs
  raise ArgumentError, "this method requires a list of links" if aggs.class != Array
  @@aggregators = aggs
end

.push_to_aggregators(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ess/pusher.rb', line 17

def self.push_to_aggregators options={}
  options = { :aggregators => Pusher::aggregators,
              :feed => nil,
              :data => nil,
              :request => nil,
              :ignore_errors => false }.merge options
  options[:aggregators].each do |aggregator|
    url = URI.parse(aggregator)
    request = Net::HTTP::Post.new(url.path)

    form_data = { "output" => "json",
                  "LIBRARY_VERSION" => VERSION }
    form_data["SERVER_ADMIN"] = ENV["SERVER_ADMIN"] if ENV["SERVER_ADMIN"]
    if ENV["SERVER_PROTOCOL"]
      if ENV["SERVER_PROTOCOL"].include? "HTTPS"
        form_data["SERVER_PROTOCOL"] = "https://"
      else
        form_data["SERVER_PROTOCOL"] = "http://"
      end
    end
    form_data["HTTP_HOST"] = ENV["HTTP_HOST"] if ENV["HTTP_HOST"]
    if options[:request]
      form_data["REMOTE_ADDR"] = options[:request].remote_ip
      form_data["REQUEST_URI"] = options[:request].fullpath
    end
    form_data["GEOIP_LATITUDE"] = ENV["GEOIP_LATITUDE"] if ENV["GEOIP_LATITUDE"]
    form_data["GEOIP_LONGITUDE"] = ENV["GEOIP_LONGITUDE"] if ENV["GEOIP_LONGITUDE"]

    if options[:feed].nil?
      form_data["feed_file"] = options[:data]
    else
      form_data["feed"] = options[:feed]
    end
    request.form_data = form_data
    response = Net::HTTP.start(url.host, url.port) { |http| http.request(request) }
    response = JSON.parse response.body
    unless options[:ignore_errors]
      errors = response["result"]["error"]
      unless errors.nil?
        raise RuntimeError, "errors while submitting feed: #{errors}"
      end
    end
  end
end