Class: Carmine
- Inherits:
-
Object
- Object
- Carmine
- Includes:
- HTTParty
- Defined in:
- lib/carmine.rb,
lib/carmine/version.rb,
lib/carmine/conversion.rb
Defined Under Namespace
Modules: Conversion, Version Classes: Error
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Public: Gets/Sets the client default options.
Class Method Summary collapse
-
.method_missing(name, *args, &block) ⇒ Object
Public: Delegates the calls to the #instance_methods.
-
.version ⇒ Object
Public: The version of the client.
Instance Method Summary collapse
-
#colorize(code = nil, options = {}) ⇒ Object
(also: #pygmentize)
Public: Colorizes the given code using the specified :formatter and :lexer.
-
#initialize(options = {}) ⇒ Carmine
constructor
Public: Initialize the carmine object.
Constructor Details
#initialize(options = {}) ⇒ Carmine
Public: Initialize the carmine object.
options - The default options to use when colorizing.
:formatter (default: :html)
:lexer (default: :plain)
61 62 63 64 65 66 |
# File 'lib/carmine.rb', line 61 def initialize( = {}) @defaults = { :formatter => :html, :lexer => :plain }.merge end |
Instance Attribute Details
#defaults ⇒ Object
Public: Gets/Sets the client default options.
54 55 56 |
# File 'lib/carmine.rb', line 54 def defaults @defaults end |
Class Method Details
.method_missing(name, *args, &block) ⇒ Object
Public: Delegates the calls to the #instance_methods.
Examples
Carmine.colorize "puts 'Hello World!'", :lexer => :ruby
# Calls `Colorize.send(:instance).colorize(*args)`.
Carmine.pygmentize "puts 'Hello World!'", :lexer => :ruby
# Calls `Colorize.send(:pygmentize).colorize(*args)`.
Returns the delegated method call result.
35 36 37 38 39 40 41 |
# File 'lib/carmine.rb', line 35 def method_missing(name, *args, &block) if instance.respond_to? name instance.send name, *args, &block else super end end |
Instance Method Details
#colorize(code = nil, options = {}) ⇒ Object Also known as: pygmentize
Public: Colorizes the given code using the specified :formatter and :lexer.
code - The code to colorize (default: nil). options - The option passed to the pygmentize.me api, the optinal options
take their defaults from the hash passed in the constructor.
:code - Can be specified here (optional).
:formatter - The name of the formatter to use (optional).
:lexer - The name of the lexer to use (optiona).
Examples
colorize "puts 'Hello World!'", :lexer => :ruby, :formatter => :text
# => "puts 'Hello World!'"
colorize :code => "puts 'Hello World!'", :lexer => :ruby, :formatter => :text
# => "puts 'Hello World!'"
Returns the colorized code. Yields the colorized code if there is a block given.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/carmine.rb', line 87 def colorize(code = nil, = {}) params = @defaults.merge params[:code] = code.respond_to?(:read) ? code.read : code unless code.nil? raise ArgumentError, "Argument or option :code missing" if params[:code].nil? response = post "/api/formatter/#{params.delete :formatter}", :query => params raise Error, response unless response.success? if block_given? yield response.body else response.body end end |