Class: URI::Meta

Inherits:
Object
  • Object
show all
Defined in:
lib/uri/meta.rb

Defined Under Namespace

Modules: Mixin Classes: Cache

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

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  self.errors = []
  options.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

#charsetObject

Returns the value of attribute charset.



11
12
13
# File 'lib/uri/meta.rb', line 11

def charset
  @charset
end

#content_typeObject

Returns the value of attribute content_type.



11
12
13
# File 'lib/uri/meta.rb', line 11

def content_type
  @content_type
end

#errorsObject

Returns the value of attribute errors.



11
12
13
# File 'lib/uri/meta.rb', line 11

def errors
  @errors
end

#feedObject

Returns the value of attribute feed.



11
12
13
# File 'lib/uri/meta.rb', line 11

def feed
  @feed
end

#headersObject

Returns the value of attribute headers.



11
12
13
# File 'lib/uri/meta.rb', line 11

def headers
  @headers
end

#last_effective_uriObject

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_modifiedObject

Returns the value of attribute last_modified.



11
12
13
# File 'lib/uri/meta.rb', line 11

def last_modified
  @last_modified
end

#primary_imageObject

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_captionObject

Returns the value of attribute primary_image_caption.



11
12
13
# File 'lib/uri/meta.rb', line 11

def primary_image_caption
  @primary_image_caption
end

#primary_image_thumbnailObject

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

#statusObject

Returns the value of attribute status.



11
12
13
# File 'lib/uri/meta.rb', line 11

def status
  @status
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/uri/meta.rb', line 11

def title
  @title
end

#uriObject

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, options = {})
  # Make sure the key includes the options used to retrieve the meta
  uid = uri.to_s + options.to_a.sort{|a,b| a[0].to_s <=> b[0].to_s}.to_s
  Digest::SHA1.hexdigest(uid)
end

.get(uri, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
# File 'lib/uri/meta.rb', line 52

def self.get(uri, options = {})
  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], options).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, options = {}, &block)
  metas = []
  multi = Curl::Multi.new
  uris.each do |uri|
    if meta = URI::Meta::Cache.get(cache_key(uri, options))
      metas << meta
      URI::Meta::Cache.store(cache_key(uri, options), meta)
      block.call(meta) if block
    else
      easy = curl(uri, options)
      easy.on_complete do |req|
        args = YAML.load(self.decode_content(req)) rescue {:errors => "YAML Error, #{$!.message}"}
        args = {:errors => "YAML Error, server returned unknown format."} unless args.is_a?(Hash)

        metas << meta = URI::Meta.new({:uri => uri}.update(args))
        URI::Meta::Cache.store(cache_key(uri, options), meta)
        block.call(meta) if block
      end
      multi.add(easy)
    end
  end
  multi.perform
  metas
end

.service_hostObject



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_agentObject



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

Returns:

  • (Boolean)


48
49
50
# File 'lib/uri/meta.rb', line 48

def errors?
  !errors.empty?
end

#redirect?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/uri/meta.rb', line 44

def redirect?
  uri != last_effective_uri
end