Module: Dolzenko::RemoteDownload

Defined in:
lib/dolzenko/remote_download.rb

Overview

Simple ‘Net::HTTP` mumbo jumbo we all have to use occasionally.

Defined Under Namespace

Classes: MyStringIO

Class Method Summary collapse

Class Method Details

.download_page(url, follow_redirect = 2) ⇒ Object

Returns page content, if retrieval failed - just return empty string



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
71
72
73
# File 'lib/dolzenko/remote_download.rb', line 45

def download_page(url, follow_redirect = 2)
  uri = URI.parse(url)

  defined?(logger) && logger.debug(%{Trying to retrieve page #{uri}})

  response = nil
  Net::HTTP.start(uri.host, uri.port) do |http|
    response = http.request_get(uri.path + (uri.query ? "?" + uri.query : ""), {
            "User-Agent" => "curl/7.14.0 (i586-pc-mingw32msvc) libcurl/7.14.0 zlib/1.2.2",
            "Host" => uri.host,
            "Accept" => "text/html",
            "Referer" => "",
            }
    )
  end

  case response
    when Net::HTTPSuccess
      response.body
    when Net::HTTPRedirection
      return "" if follow_redirect <= 0

      defined?(logger) && logger.debug(%{Following redirect from #{uri} to #{response['location']}})

      download_page(response['location'], follow_redirect - 1)
    else
      ""
  end
end

.get_uploaded_data(url, follow_redirect = 2) ⇒ Object

Returns IO object to be used with attachment_fu models, if retrieval fails - return nil



7
8
9
10
11
12
13
14
15
16
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
# File 'lib/dolzenko/remote_download.rb', line 7

def get_uploaded_data(url, follow_redirect = 2)
  uri = URI.parse(url)
  
  dputs(%{Trying to retrieve image from #{ uri } })

  response = nil
  Net::HTTP.start(uri.host, uri.port) do |http|
    # http.set_debug_output $stderr
    dputs(%{In Net::HTTP.start})
    
    response = http.request_get(uri.path + (uri.query ? "?" + uri.query : ""), {
            "User-Agent" => "curl/7.14.0 (i586-pc-mingw32msvc) libcurl/7.14.0 zlib/1.2.2",
            "Host" => uri.host,
            "Accept" => "*/*",
            "Referer" => "",
            })
    
    dputs("got response: #{ response }")
  end

  case response
    when Net::HTTPSuccess
      MyStringIO.from_http_response(response, url)

    when Net::HTTPRedirection
      return nil if follow_redirect <= 0

      defined?(logger) && logger.debug(%{Following redirect from #{uri} to #{response['location']}})

      get_uploaded_data(response['location'], follow_redirect - 1)
    
    else
      nil
  end
end