class TwitterJekyll::TwitterTag
Class to respond to Jekyll tag; entry point to library @api public
Constants
- ERROR_BODY_TEXT
- OEMBED_ARG
Attributes
Public Class Methods
Class that implements caching strategy @api private
# File lib/jekyll-twitter-plugin-2.rb, line 156 def self.cache_klass FileCache end
# File lib/jekyll-twitter-plugin-2.rb, line 149 def initialize(_name, params, _tokens) super @api_request = parse_params(params) end
Public Instance Methods
Return html string for Jekyll engine @api public
# File lib/jekyll-twitter-plugin-2.rb, line 162 def render(context) api_secrets_deprecation_warning(context) # TODO: remove after deprecation cycle response = cached_response || live_response html_output_for(response) end
Private Instance Methods
# File lib/jekyll-twitter-plugin-2.rb, line 174 def api_client @api_client ||= ApiClient.new end
TODO: remove after deprecation cycle
# File lib/jekyll-twitter-plugin-2.rb, line 240 def api_secrets_deprecation_warning(context) warn_if_twitter_secrets_in_context(context) || warn_if_twitter_secrets_in_env end
TODO: remove after deprecation cycle
# File lib/jekyll-twitter-plugin-2.rb, line 235 def arguments_deprecation_warning(args) warn "#{LIBRARY_VERSION}: Passing '#{OEMBED_ARG}' as the first argument is not required anymore. This will result in an error in future versions.\nCalled with #{args.inspect}" end
Format a response hash @api private
# File lib/jekyll-twitter-plugin-2.rb, line 230 def build_response(h) OpenStruct.new(h) end
# File lib/jekyll-twitter-plugin-2.rb, line 170 def cache @cache ||= self.class.cache_klass.new("./.tweet-cache") end
Return response cache if present, otherwise nil @api private
# File lib/jekyll-twitter-plugin-2.rb, line 196 def cached_response response = cache.read(@api_request.cache_key) build_response(response) unless response.nil? end
Return Twitter response or error html @api private
# File lib/jekyll-twitter-plugin-2.rb, line 180 def html_output_for(response) body = (response.html if response) || ERROR_BODY_TEXT body end
Raise error for invalid arguments @api private
# File lib/jekyll-twitter-plugin-2.rb, line 271 def invalid_args!(arguments) formatted_args = Array(arguments).join(" ") raise ArgumentError, "Invalid arguments '#{formatted_args}' passed to 'jekyll-twitter-plugin'. #{REFER_TO_README}" end
Return response from API and write to cache @api private
# File lib/jekyll-twitter-plugin-2.rb, line 187 def live_response if response = api_client.fetch(@api_request) cache.write(@api_request.cache_key, response) build_response(response) end end
Transform 'a=b x=y' tag arguments into { “a” => “b”, “x” => “y” } @api private
# File lib/jekyll-twitter-plugin-2.rb, line 218 def parse_args(args) args.each_with_object({}) do |arg, params| k, v = arg.split("=").map(&:strip) if k && v v = Regexp.last_match[1] if v =~ /^'(.*)'$/ params[k] = v end end end
Return an `ApiRequest` with the url and arguments @api private
# File lib/jekyll-twitter-plugin-2.rb, line 203 def parse_params(params) args = params.split(/\s+/).map(&:strip) invalid_args!(args) unless args.any? if args[0].to_s == OEMBED_ARG # TODO: remove after deprecation cycle arguments_deprecation_warning(args) args.shift end url, *api_args = args ApiRequest.new(url, parse_args(api_args)) end
TODO: remove after deprecation cycle
# File lib/jekyll-twitter-plugin-2.rb, line 265 def store_has_keys?(store, keys) keys.all? { |required_key| store.key?(required_key) } end
TODO: remove after deprecation cycle
# File lib/jekyll-twitter-plugin-2.rb, line 245 def warn_if_twitter_secrets_in_context(context) twitter_secrets = context.registers[:site].config.fetch("twitter", {}) return unless store_has_keys?(twitter_secrets, CONTEXT_API_KEYS) warn_secrets_in_project("Jekyll _config.yml") end
TODO: remove after deprecation cycle
# File lib/jekyll-twitter-plugin-2.rb, line 253 def warn_if_twitter_secrets_in_env return unless store_has_keys?(ENV, ENV_API_KEYS) warn_secrets_in_project("ENV") end
TODO: remove after deprecation cycle
# File lib/jekyll-twitter-plugin-2.rb, line 260 def warn_secrets_in_project(source) warn "#{LIBRARY_VERSION}: Found Twitter API keys in #{source}, this library does not require these keys anymore. You can remove these keys, if used for another library then ignore this message." end