Class: IPlayer::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/iplayer/browser.rb

Constant Summary collapse

IPHONE_UA =

Used by Safari Mobile

'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us)'+
'AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16'
QT_UA =

Used by Quicktime

'Apple iPhone v1.1.4 CoreMedia v1.0.0.4A102'
DESKTOP_UA =

Safari, for no good reason

'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_4; en-gb) '+
'AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1'
DEFAULT_HEADERS =
{
  'Accept'          => '*/*',
  'Accept-Language' => 'en',
  'Connection'      => 'keep-alive',
  'Pragma'          => 'no-cache'
}

Instance Method Summary collapse

Constructor Details

#initialize(http_class = Net::HTTP) ⇒ Browser

Returns a new instance of Browser.



42
43
44
# File 'lib/iplayer/browser.rb', line 42

def initialize(http_class = Net::HTTP)
  @http_class = http_class
end

Instance Method Details

#get(location, headers = {}, &blk) ⇒ Object



50
51
52
# File 'lib/iplayer/browser.rb', line 50

def get(location, headers={}, &blk)
  request(:get, location, nil, headers, &blk)
end

#post(location, data, headers = {}, &blk) ⇒ Object



46
47
48
# File 'lib/iplayer/browser.rb', line 46

def post(location, data, headers={}, &blk)
  request(:post, location, data, headers, &blk)
end

#request(type, location, data, headers, &blk) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/iplayer/browser.rb', line 54

def request(type, location, data, headers, &blk)
  url = URI.parse(location)
  http = @http_class.new(url.host, url.port)
  path = url.path
  if url.query
    path << '?' << url.query
  end
  headers_to_send = DEFAULT_HEADERS.merge(headers)
  if defined? DEBUG
    puts path
    headers_to_send.each do |k,v|
      puts " -> #{k}: #{v}"
    end
  end
  response =
    case type
    when :get
      http.request_get(path, headers_to_send, &blk)
    when :post
      http.request_post(path, data, headers_to_send, &blk)
    end
  if defined? DEBUG
    response.each do |k,v|
      puts "<-  #{k}: #{v}"
    end
  end
  response
end