Class: RemotePartial::ResourceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_partial/resource_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, criteria = nil, &output_modifier) ⇒ ResourceManager

Returns a new instance of ResourceManager.



40
41
42
43
44
# File 'lib/remote_partial/resource_manager.rb', line 40

def initialize(url, criteria = nil, &output_modifier)
  @url = url
  @criteria = criteria
  @output_modifier = output_modifier
end

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



6
7
8
# File 'lib/remote_partial/resource_manager.rb', line 6

def criteria
  @criteria
end

#output_modifierObject (readonly)

Returns the value of attribute output_modifier.



6
7
8
# File 'lib/remote_partial/resource_manager.rb', line 6

def output_modifier
  @output_modifier
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/remote_partial/resource_manager.rb', line 6

def url
  @url
end

Class Method Details

.get_page(url) ⇒ Object



8
9
10
# File 'lib/remote_partial/resource_manager.rb', line 8

def self.get_page(url)
  Nokogiri::HTML(get_raw(url))
end

.get_raw(url) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/remote_partial/resource_manager.rb', line 12

def self.get_raw(url)
  response = get_response(url)
  
  case response.code.to_i
  when ok_response_codes
    return response.body
  when redirect_response_codes
    get_raw(URI.parse(response['location']))
  else
    raise response.inspect
  end
rescue => exception # Do main exception raising outside of case statement so that SocketErrors are also handled
  raise RemotePartialRetrivalError.new(url, exception)
end

.get_response(url) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/remote_partial/resource_manager.rb', line 27

def self.get_response(url)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.port == 443
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  request = Net::HTTP::Get.new(uri.request_uri)
  http.request(request)
end

Instance Method Details

#htmlObject



52
53
54
55
# File 'lib/remote_partial/resource_manager.rb', line 52

def html
  text = criteria ? get_part_of_page : get_whole_page
  output_modifier ? output_modifier.call(text) : text
end

#output_to(path) ⇒ Object



46
47
48
49
50
# File 'lib/remote_partial/resource_manager.rb', line 46

def output_to(path)
  @path = path
  ensure_output_folder_exists
  File.write(path, html)
end