module CLAide::Command::PluginsHelper

This module is used by Command::Plugins::List and Command::Plugins::Search to download and parse the JSON describing the plugins list and manipulate it

Public Class Methods

download_json() click to toggle source

Force-download the JSON

@return [Hash] The hash representing the JSON with all known plugins

# File lib/claide/command/plugins_helper.rb, line 22
def self.download_json
  UI.puts 'Downloading Plugins list...'
  response = REST.get(plugins_raw_url)
  if response.ok?
    parse_json(response.body)
  else
    raise Informative, 'Could not download plugins list ' \
      "from #{plugin_prefix}-plugins: #{response.inspect}"
  end
end
known_plugins() click to toggle source

The list of all known plugins, according to the JSON hosted on github's cocoapods-plugins

@return [Array] all known plugins, as listed in the downloaded JSON

# File lib/claide/command/plugins_helper.rb, line 38
def self.known_plugins
  json = download_json
  json['plugins']
end
matching_plugins(query, full_text_search) click to toggle source

Filter plugins to return only matching ones

@param [String] query

A query string that corresponds to a valid RegExp pattern.

@param [Bool] full_text_search

false only searches in the plugin's name.
true searches in the plugin's name, author and description.

@return [Array] all plugins matching the query

# File lib/claide/command/plugins_helper.rb, line 54
def self.matching_plugins(query, full_text_search)
  query_regexp = /#{query}/i
  known_plugins.reject do |plugin|
    texts = [plugin['name']]
    if full_text_search
      texts << plugin['author'] if plugin['author']
      texts << plugin['description'] if plugin['description']
    end
    texts.grep(query_regexp).empty?
  end
end
plugin_prefix() click to toggle source
# File lib/claide/command/plugins_helper.rb, line 14
def self.plugin_prefix
  CLAide::Plugins.config.plugin_prefix
end
plugins_raw_url() click to toggle source
# File lib/claide/command/plugins_helper.rb, line 10
def self.plugins_raw_url
  CLAide::Plugins.config.plugin_list_url
end
print_plugin(plugin, verbose = false) click to toggle source

Display information about a plugin

@param [Hash] plugin

The hash describing the plugin

@param [Bool] verbose

If true, will also print the author of the plugins.
Defaults to false.

Private Class Methods

parse_json(json_str) click to toggle source

Parse the given JSON data, handling parsing errors if any

@param [String] json_str

The string representation of the JSON to parse
# File lib/claide/command/plugins_helper.rb, line 113
def self.parse_json(json_str)
  JSON.parse(json_str)
rescue JSON::ParserError => e
  raise Informative,
        "Invalid plugins list from #{plugin_prefix}-plugins: #{e}"
end
plugin_title(plugin) click to toggle source

Format the title line to print the plugin info with print_plugin coloring it according to whether the plugin is installed or not

@param [Hash] plugin

The hash describing the plugin

@return [String] The formatted and colored title

# File lib/claide/command/plugins_helper.rb, line 128
def self.plugin_title(plugin)
  plugin_name = "-> #{plugin['name']}"
  if GemHelper.gem_installed?(plugin['gem'])
    plugin_name += " (#{GemHelper.installed_version(plugin['gem'])})"
    plugin_name.green
  else
    plugin_name.yellow
  end
end
print_verbose_plugin(plugin, ljust) click to toggle source

Smaller helper to print out the verbose details for a plugin.

@param [Hash] plugin

The hash describing the plugin

@param [Integer] ljust

The left justification that is passed into UI.labeled