Module: Processors::Http

Includes:
LogAware
Defined in:
lib/processors/http.rb

Overview

utilities for handling http

Constant Summary collapse

USER_AGENT =
%{Mozilla/5.0 (compatible; tweetlr; +http://tweetlr.5v3n.com)}

Class Method Summary collapse

Methods included from LogAware

log=

Class Method Details

.http_get(request) ⇒ Object

convenience method for curl http get calls



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/processors/http.rb', line 16

def self.http_get(request)
  tries = 3
  curl = nil
  begin
    curl = Curl::Easy.new request
    curl.useragent = USER_AGENT
    curl.perform
  rescue Curl::Err::CurlError => err
    log.error "Failure in Curl call: #{err}" if log
    tries -= 1
    sleep 3
    if tries > 0
      retry
    end 
  end
  return curl
end

.http_get_json(request) ⇒ Object

convenience method for curl http get calls and parsing them to json.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/processors/http.rb', line 34

def self.http_get_json(request)
  curl = self.http_get(request)
  begin
    JSON.parse curl.body_str
  rescue JSON::ParserError => err
    begin
      log.warn "#{err}: Could not parse response for #{request} - this is probably not a json response: #{curl.body_str}"
      return nil
    rescue Encoding::CompatibilityError => err
      log.error "Trying to rescue a JSON::ParserError for '#{request}' we got stuck in a Encoding::CompatibilityError."
      return nil
    end
  end
end

.logObject



12
13
14
# File 'lib/processors/http.rb', line 12

def self.log
  LogAware.log #TODO why doesn't the include make the log method accessible?
end