Module: ZencoderFetcher

Defined in:
lib/zencoder_fetcher.rb

Constant Summary collapse

FETCHER_VERSION =
[0,2,8]

Class Method Summary collapse

Class Method Details

.request(options = {}) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
# File 'lib/zencoder_fetcher.rb', line 18

def self.request(options={})
  query = {
    "api_key"  => options[:api_key],
    "per_page" => options[:count] || 50,
    "page"     => options[:page] || 1,
    "version"  => "latest"
  }
  query["since"] = options[:since].iso8601 if options[:since]
  query = query.map{|k,v| "#{k}=#{v}" }.join("&")

  local_url = options[:url] || "http://localhost:3000/"
  auth = local_url.match(/^https?:\/\/([^\/]+):([^\/]+)@/) ? {:username=>$1, :password=>$2} : {}

  response = HTTParty.get("https://#{options[:endpoint] || 'app'}.zencoder.com/api/#{options[:api_version] || 'v2'}/notifications.json?#{query}",
                          :headers => { "HTTP_X_FETCHER_VERSION" => version })

  if response["errors"]
    puts "There was an error fetching notifications:"
    puts response.body.to_s
    raise FetcherError
  else
    puts "Notifications retrieved: #{response["notifications"].size}"
    puts "Posting to #{local_url}" if response["notifications"].size > 0
    response["notifications"].each do |notification|
      format = notification.delete("format")
      if format == "xml"
        begin
          options = {:body => notification.to_xml}
        rescue
          puts "Unable to build notification."
        end
      else
        begin
          options = {:body => notification.to_json}
        rescue
          puts "Unable to build notification."
        end
      end
      options = options.merge({:headers => {"Content-Type" => "application/#{format}"}}) if format
      options = options.merge({:basic_auth => auth}) if !auth.empty?
      begin
        HTTParty.post(local_url, options)
      rescue Errno::ECONNREFUSED => e
        puts "Unable to connect to your local server at #{local_url}. Is it running?"
        raise FetcherLocalConnectionError
      end
    end
    puts "Finished Posting" if response["notifications"].size > 0
    puts

    Time.parse(response["sent_at"].to_s)
  end
end

.versionObject



14
15
16
# File 'lib/zencoder_fetcher.rb', line 14

def self.version
  FETCHER_VERSION.join(".")
end