class TwitterJekyll::TwitterTag

Class to respond to Jekyll tag; entry point to library @api public

Constants

ERROR_BODY_TEXT
OEMBED_ARG

Attributes

cache[W]

Public Class Methods

cache_klass() click to toggle source

Class that implements caching strategy @api private

# File lib/jekyll-twitter-plugin-2.rb, line 156
def self.cache_klass
  FileCache
end
new(_name, params, _tokens) click to toggle source
Calls superclass method
# File lib/jekyll-twitter-plugin-2.rb, line 149
def initialize(_name, params, _tokens)
  super
  @api_request = parse_params(params)
end

Public Instance Methods

render(context) click to toggle source

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

api_client() click to toggle source
# File lib/jekyll-twitter-plugin-2.rb, line 174
def api_client
  @api_client ||= ApiClient.new
end
api_secrets_deprecation_warning(context) click to toggle source

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
arguments_deprecation_warning(args) click to toggle source

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
build_response(h) click to toggle source

Format a response hash @api private

# File lib/jekyll-twitter-plugin-2.rb, line 230
def build_response(h)
  OpenStruct.new(h)
end
cache() click to toggle source
# File lib/jekyll-twitter-plugin-2.rb, line 170
def cache
  @cache ||= self.class.cache_klass.new("./.tweet-cache")
end
cached_response() click to toggle source

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
html_output_for(response) click to toggle source

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
invalid_args!(arguments) click to toggle source

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
live_response() click to toggle source

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
parse_args(args) click to toggle source

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
parse_params(params) click to toggle source

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
store_has_keys?(store, keys) click to toggle source

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
warn_if_twitter_secrets_in_context(context) click to toggle source

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
warn_if_twitter_secrets_in_env() click to toggle source

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
warn_secrets_in_project(source) click to toggle source

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