markbates-content_o_matic 0.0.2.20090721162019 → 0.1.0.20090721175910

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2009 markbates
3
+ Copyright (c) 2009 Mark Bates
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,10 @@
1
1
  module ContentOMatic
2
+ # This class is used to cache results of succesful retrieval of a web page.
3
+ # It uses Cachetastic to do the caching. You can expect to receive
4
+ # ContentOMatic::Response objects, if they are in the cache.
5
+ #
6
+ # Caching can be turned on (it's off by default) with the following configatron setting:
7
+ # configatron.content_o_matic.cache_results = true
2
8
  class ContentCache < Cachetastic::Cache
3
9
 
4
10
  end # ContentCache
@@ -2,10 +2,37 @@ module ContentOMatic
2
2
 
3
3
  class << self
4
4
 
5
+ # Returns the logger being used by ContentOMatic.
6
+ # The logger can be set with the following configatron setting:
7
+ # configatron.content_o_matic.logger = ::Logger.new(STDOUT)
8
+ #
9
+ # In a Rails environment this defaults to the <tt>RAILS_DEFAULT_LOGGER</tt>.
10
+ # The non-Rails default is:
11
+ # <pwd>/log/content_o_matic.log
5
12
  def logger
6
13
  configatron.content_o_matic.logger
7
14
  end
8
15
 
16
+ # This method does pretty much all of the heavy work. You pass it a url
17
+ # and it will fetch the contents of that page and return a ContentOMatic::Response
18
+ # object to you.
19
+ #
20
+ # If you pass in a 'relative' url it will be joined with a default url that can be
21
+ # set with the following configatron setting:
22
+ # configatron.content_o_matic.url = 'http://www.example.com'
23
+ #
24
+ # url examples:
25
+ # 'foo.html' => 'http://www.example.com/foo.html'
26
+ # '/foo.html' => 'http://www.example.com/foo.html'
27
+ # '/foo/bar.html' => 'http://www.example.com/foo/bar.html'
28
+ # 'http://www.example.com/index.html' => 'http://www.example.com/index.html'
29
+ #
30
+ # Any options passed into will become query string parameters passed to the
31
+ # requested url.
32
+ #
33
+ # If you have caching enabled it will look first in the cache, if it doesn't find
34
+ # it there, it will do a fetch of the page and store it in the cache, if it was successful.
35
+ # See ContentOMatic::ContentCache for more details on caching.
9
36
  def get(slug_url, options = {})
10
37
  if configatron.content_o_matic.retrieve(:cache_results, false)
11
38
  url = url_from_slug(slug_url, options)
@@ -21,7 +48,7 @@ module ContentOMatic
21
48
  end
22
49
  end
23
50
 
24
- def get_without_cache(slug_url, options = {})
51
+ def get_without_cache(slug_url, options = {}) # :nodoc:
25
52
  url = url_from_slug(slug_url, options)
26
53
  begin
27
54
  Timeout::timeout(configatron.content_o_matic.response.time_out) do
@@ -34,7 +61,7 @@ module ContentOMatic
34
61
  end
35
62
  end # get_without_cache
36
63
 
37
- def url_from_slug(slug_url, options = {})
64
+ def url_from_slug(slug_url, options = {}) # :nodoc:
38
65
  return slug_url if slug_url.nil?
39
66
  url = slug_url
40
67
  if !slug_url.match(/^([a-zA-Z]+):\/\//)
@@ -1,5 +1,5 @@
1
1
  module ContentOMatic
2
-
2
+
3
3
  class InvalidResponseError < StandardError
4
4
  end # InvalidResponseError
5
5
 
@@ -1,33 +1,58 @@
1
1
  module ContentOMatic
2
-
2
+ # This class wraps the response received from pulling down content from a web page.
3
3
  class Response
4
4
 
5
+ # The status of the response
5
6
  attr_accessor :status
7
+ # The full body of the response
6
8
  attr_accessor :body
9
+ # The url of the requested page
7
10
  attr_accessor :url
8
11
 
12
+ # Takes the url of the requested page, the status code of the
13
+ # response, and the body of the response.
9
14
  def initialize(url, status, body = '')
10
15
  self.url = url
11
16
  self.status = status.to_i
12
17
  self.body = body
13
18
  end
14
19
 
20
+ # Returns <tt>true</tt> if the status of the page was 200
15
21
  def success?
16
22
  self.status == 200
17
23
  end
18
24
 
19
- def to_s
25
+ def to_s # :nodoc:
20
26
  self.success? ? self.body : ''
21
27
  end
22
28
 
23
- def to_str
29
+ def to_str # :nodoc:
24
30
  self.to_s
25
31
  end
26
32
 
33
+ # Returns <tt>true</tt> if the page is wrapped in <tt>html</tt> tags.
27
34
  def has_layout?
28
35
  return self.body != self.html_body
29
36
  end
30
37
 
38
+ # Returns the 'full' HTML body of the requested page.
39
+ # If you pass in <tt>true</tt> it will normalize all the links
40
+ # found in the body.
41
+ #
42
+ # Example:
43
+ # # http://www.example.com/foo/bar.html
44
+ # <img src='image.jpg'> # => <img src='http://www.example.com/foo/image.jpg'>
45
+ # <img src='/image.jpg'> # => <img src='http://www.example.com/image.jpg'>
46
+ # <img src='http://www.example.org/image.jpg'> # => <img src='http://www.example.org/image.jpg'>
47
+ #
48
+ # The following tags get 'normalized' with the <tt>true</tt> parameter:
49
+ # img
50
+ # script
51
+ # link
52
+ # a
53
+ # iframe
54
+ # form
55
+ # object
31
56
  def body(normalize_assets = false)
32
57
  if normalize_assets
33
58
  unless @__normalized_body
@@ -50,6 +75,10 @@ module ContentOMatic
50
75
  @body
51
76
  end
52
77
 
78
+ # Returns just the content within the HTML 'body' tag.
79
+ # If there is no 'body' tag, then the whole response body is returned.
80
+ # If you pass in <tt>true</tt> it will normalize all the links
81
+ # found in the body. See the body method for more details.
53
82
  def html_body(normalize_assets = false)
54
83
  unless @__doc_body
55
84
  doc = Nokogiri::HTML(self.body(normalize_assets))
@@ -59,8 +88,10 @@ module ContentOMatic
59
88
  return @__doc_body
60
89
  end
61
90
 
91
+ # Returns a ContentOMatic::InvalidResponseError exception if the
92
+ # response was not a success.
62
93
  def exception
63
- ContentOMatic::InvalidResponseError.new("URL: '#{self.url}' did not return a valid response! Status: '#{self.status}' Body: '#{self.body}'") unless self.success?
94
+ ContentOMatic::InvalidResponseError.new("URL: '#{self.url}' did not return a valid response! Status: '#{self.status}'") unless self.success?
64
95
  end
65
96
 
66
97
  private
@@ -1,21 +1,23 @@
1
- module ActionView
2
- module Helpers
1
+ module ActionView # :nodoc:
2
+ module Helpers # :nodoc:
3
3
  module TextHelper
4
4
 
5
+ # This convenience method automatically plugs into Rails views, however,
6
+ # this module could easily be included in a non-Rails environment.
7
+ #
8
+ # This method wraps the ContentOMatic.get method with some nice HTML
9
+ # comments to let you know what has been loaded. It also normalizes
10
+ # links in that content by default. See ContentOMatic::Response body for more
11
+ # information on normalizing links.
12
+ #
13
+ # This method will also, by default, return just the contents of the HTML
14
+ # 'body' tag.
5
15
  def content_o_matic(slug, options = {})
6
16
  options = {} if options.nil?
7
- comments = true
8
- if options.has_key?(:print_comments)
9
- comments = options.delete(:print_comments)
10
- end
11
- html_body = true
12
- if options.has_key?(:html_body)
13
- html_body = options.delete(:html_body)
14
- end
15
- normalize_assets = true
16
- if options.has_key?(:normalize_assets)
17
- normalize_assets = options.delete(:normalize_assets)
18
- end
17
+ options = {:print_comments => true, :html_body => true, :normalize_assets => true}.merge(options)
18
+ comments = options.delete(:print_comments)
19
+ html_body = options.delete(:html_body)
20
+ normalize_assets = options.delete(:normalize_assets)
19
21
  text = "<!-- Loading Content: '#{slug}' -->\n" if comments
20
22
  begin
21
23
  res = ContentOMatic.get(slug, options)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markbates-content_o_matic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.20090721162019
4
+ version: 0.1.0.20090721175910
5
5
  platform: ruby
6
6
  authors:
7
7
  - markbates