Guest User

Untitled

a guest
Feb 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. module OEmbed
  2. class RisingCodeCache
  3. @cache_enabled = false
  4. class << self
  5. attr_writer :expiry, :host
  6. def enabled?
  7. @cache_enabled
  8. end
  9. def enable!
  10. @cache ||= MemCache.new(host, :namespace => "openuri")
  11. @cache_enabled = true
  12. end
  13. def disable!
  14. @cache_enabled = false
  15. end
  16. def disabled?
  17. !@cache_enabled
  18. end
  19. def get(key)
  20. @cache.get(key)
  21. end
  22. def set(key, value)
  23. @cache.set(key, value, expiry)
  24. end
  25. def expiry
  26. @expiry ||= 60 * 60 * 24
  27. end
  28. def alive?
  29. servers = @cache.instance_variable_get(:@servers) and servers.collect{|s| s.alive?}.include?(true)
  30. end
  31. def host
  32. @host ||= "localhost:11211"
  33. end
  34. end
  35. end
  36. class Provider
  37. attr_accessor :format, :name, :url, :urls, :endpoint
  38.  
  39. def initialize(endpoint, format = :json)
  40. @endpoint = endpoint
  41. @urls = []
  42. @format = format
  43. end
  44.  
  45. def <<(url)
  46. full, scheme, domain, path = *url.match(%r{([^:]*)://?([^/?]*)(.*)})
  47. domain = Regexp.escape(domain).gsub("\\*", "(.*?)").gsub("(.*?)\\.", "([^\\.]+\\.)?")
  48. path = Regexp.escape(path).gsub("\\*", "(.*?)")
  49. @urls << Regexp.new("^#{Regexp.escape(scheme)}://#{domain}#{path}")
  50. end
  51.  
  52. def build(url, options = {})
  53. raise OEmbed::NotFound, url unless include?(url)
  54. query = options.merge({:url => url})
  55. endpoint = @endpoint.clone
  56.  
  57. if format_in_url?
  58. format = endpoint["{format}"] = (query[:format] || @format).to_s
  59. query.delete(:format)
  60. else
  61. format = query[:format] ||= @format
  62. end
  63.  
  64. query_string = "?" + query.inject("") do |memo, (key, value)|
  65. "#{key}=#{value}&#{memo}"
  66. end.chop
  67.  
  68. URI.parse(endpoint + query_string).instance_eval do
  69. @format = format; def format; @format; end
  70. self
  71. end
  72. end
  73.  
  74. def raw(url, options = {})
  75. uri = build(url, options)
  76. if RisingCodeCache.enabled? and RisingCodeCache::alive?
  77. begin
  78. response = RisingCodeCache::get(url)
  79. rescue
  80. response = nil
  81. end
  82. end
  83. unless response
  84. res = Net::HTTP.start(uri.host, uri.port) do |http|
  85. http.get(uri.request_uri)
  86. end
  87. case res
  88. when Net::HTTPNotImplemented
  89. response = Exception.new
  90. when Net::HTTPNotFound
  91. response = Exception.new
  92. when Net::HTTPOK
  93. response = res.body
  94. else
  95. response = Exception.new
  96. end
  97. RisingCodeCache::set(url, response) if RisingCodeCache.alive?
  98. end
  99. raise response if response.is_a? Exception
  100. StringIO.new(response)
  101. end
  102.  
  103. def get(url, options = {})
  104. OEmbed::Response.create_for(raw(url, options.merge(:format => :json)), self)
  105. end
  106.  
  107. def format_in_url?
  108. @endpoint.include?("{format}")
  109. end
  110.  
  111. def include?(url)
  112. @urls.empty? || !!@urls.detect{ |u| u =~ url }
  113. end
  114. end
  115. end
Add Comment
Please, Sign In to add comment