Class: URI::Meta
- Inherits:
-
Object
- Object
- URI::Meta
- Defined in:
- lib/uri/meta.rb
Defined Under Namespace
Constant Summary collapse
- UNSAFE =
Regexp.new("[#{URI::REGEXP::PATTERN::RESERVED} #%]", false, 'N').freeze
- @@service_host =
'www.metauri.com'
- @@user_agent =
'uri-meta rubygem'
Instance Attribute Summary collapse
-
#charset ⇒ Object
Returns the value of attribute charset.
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#feed ⇒ Object
Returns the value of attribute feed.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#last_effective_uri ⇒ Object
Returns the value of attribute last_effective_uri.
-
#last_modified ⇒ Object
Returns the value of attribute last_modified.
-
#primary_image ⇒ Object
Returns the value of attribute primary_image.
-
#primary_image_caption ⇒ Object
Returns the value of attribute primary_image_caption.
-
#primary_image_thumbnail ⇒ Object
Returns the value of attribute primary_image_thumbnail.
-
#status ⇒ Object
Returns the value of attribute status.
-
#title ⇒ Object
Returns the value of attribute title.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
- .cache_key(uri, options = {}) ⇒ Object
- .get(uri, options = {}) ⇒ Object
-
.multi(uris, options = {}, &block) ⇒ Object
– TODO: Chunk uri’s through a pre-warmed pool of curl easy instances?.
- .service_host ⇒ Object
- .service_host=(service_host) ⇒ Object
- .user_agent ⇒ Object
- .user_agent=(user_agent) ⇒ Object
Instance Method Summary collapse
- #errors? ⇒ Boolean
-
#initialize(options = {}) ⇒ Meta
constructor
A new instance of Meta.
- #redirect? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Meta
Returns a new instance of Meta.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/uri/meta.rb', line 33 def initialize( = {}) self.errors = [] .each do |k, v| case k.to_sym when :last_effective_uri, :uri, :feed then send("#{k}=", v.to_s == '' ? nil : (URI.parse(v.to_s) rescue nil)) when :error, :errors then self.errors.push(*[v].flatten) else send("#{k}=", v) if respond_to?("#{k}=") end end end |
Instance Attribute Details
#charset ⇒ Object
Returns the value of attribute charset.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def charset @charset end |
#content_type ⇒ Object
Returns the value of attribute content_type.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def content_type @content_type end |
#errors ⇒ Object
Returns the value of attribute errors.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def errors @errors end |
#feed ⇒ Object
Returns the value of attribute feed.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def feed @feed end |
#headers ⇒ Object
Returns the value of attribute headers.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def headers @headers end |
#last_effective_uri ⇒ Object
Returns the value of attribute last_effective_uri.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def last_effective_uri @last_effective_uri end |
#last_modified ⇒ Object
Returns the value of attribute last_modified.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def last_modified @last_modified end |
#primary_image ⇒ Object
Returns the value of attribute primary_image.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def primary_image @primary_image end |
#primary_image_caption ⇒ Object
Returns the value of attribute primary_image_caption.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def @primary_image_caption end |
#primary_image_thumbnail ⇒ Object
Returns the value of attribute primary_image_thumbnail.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def primary_image_thumbnail @primary_image_thumbnail end |
#status ⇒ Object
Returns the value of attribute status.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def status @status end |
#title ⇒ Object
Returns the value of attribute title.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def title @title end |
#uri ⇒ Object
Returns the value of attribute uri.
11 12 13 |
# File 'lib/uri/meta.rb', line 11 def uri @uri end |
Class Method Details
.cache_key(uri, options = {}) ⇒ Object
59 60 61 62 63 |
# File 'lib/uri/meta.rb', line 59 def self.cache_key(uri, = {}) # Make sure the key includes the options used to retrieve the meta uid = uri.to_s + .to_a.sort{|a,b| a[0].to_s <=> b[0].to_s}.to_s Digest::SHA1.hexdigest(uid) end |
.get(uri, options = {}) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/uri/meta.rb', line 52 def self.get(uri, = {}) uri = URI.parse(uri.to_s) rescue nil raise ArgumentError.new("Can't coerce #{uri.class} to URI") unless uri.is_a?(URI) raise NotImplementedError.new('Only HTTP is supported so far.') unless uri.is_a?(URI::HTTP) URI::Meta.multi([uri], ).first end |
.multi(uris, options = {}, &block) ⇒ Object
– TODO: Chunk uri’s through a pre-warmed pool of curl easy instances?
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/uri/meta.rb', line 67 def self.multi(uris, = {}, &block) = [] multi = Curl::Multi.new uris.each do |uri| if = URI::Meta::Cache.get(cache_key(uri, )) << URI::Meta::Cache.store(cache_key(uri, ), ) block.call() if block else easy = curl(uri, ) easy.on_complete do |req| args = YAML.load(self.decode_content(req)) rescue {:errors => "YAML Error, #{$!.}"} args = {:errors => "YAML Error, server returned unknown format."} unless args.is_a?(Hash) << = URI::Meta.new({:uri => uri}.update(args)) URI::Meta::Cache.store(cache_key(uri, ), ) block.call() if block end multi.add(easy) end end multi.perform end |
.service_host ⇒ Object
17 18 19 |
# File 'lib/uri/meta.rb', line 17 def self.service_host @@service_host end |
.service_host=(service_host) ⇒ Object
21 22 23 |
# File 'lib/uri/meta.rb', line 21 def self.service_host=(service_host) @@service_host = service_host end |
.user_agent ⇒ Object
25 26 27 |
# File 'lib/uri/meta.rb', line 25 def self.user_agent @@user_agent end |
.user_agent=(user_agent) ⇒ Object
29 30 31 |
# File 'lib/uri/meta.rb', line 29 def self.user_agent=(user_agent) @@user_agent = user_agent end |
Instance Method Details
#errors? ⇒ Boolean
48 49 50 |
# File 'lib/uri/meta.rb', line 48 def errors? !errors.empty? end |
#redirect? ⇒ Boolean
44 45 46 |
# File 'lib/uri/meta.rb', line 44 def redirect? uri != last_effective_uri end |