carmine 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source :gemcutter
2
2
 
3
3
  gem 'httparty'
4
+ gem 'json'
4
5
 
5
6
  group :development do
6
7
  gem 'rake'
@@ -41,5 +41,6 @@ DEPENDENCIES
41
41
  guard-rspec
42
42
  httparty
43
43
  jeweler
44
+ json
44
45
  rake
45
46
  rspec
data/Guardfile CHANGED
@@ -1,4 +1,4 @@
1
- guard :rspec, :cli => '-f doc -r "spec_helper" -c' do
1
+ guard :rspec, :cli => '--format documentation --require "spec_helper" --color' do
2
2
  watch(%r{^lib/(.+)\.rb$}) { 'spec' }
3
3
  watch(%r{^spec/(.+)\.rb$}) { 'spec' }
4
4
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "carmine"
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Genadi Samokovarov"]
12
- s.date = "2012-03-01"
12
+ s.date = "2012-03-04"
13
13
  s.description = "Ruby client for pygmentize.me"
14
14
  s.email = "gsamokovarov@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -25,7 +25,9 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "carmine.gemspec",
27
27
  "lib/carmine.rb",
28
+ "lib/carmine/conversion.rb",
28
29
  "lib/carmine/version.rb",
30
+ "spec/carmine/conversion_spec.rb",
29
31
  "spec/carmine/version_spec.rb",
30
32
  "spec/carmine_spec.rb",
31
33
  "spec/spec_helper.rb",
@@ -42,6 +44,7 @@ Gem::Specification.new do |s|
42
44
 
43
45
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
46
  s.add_runtime_dependency(%q<httparty>, [">= 0"])
47
+ s.add_runtime_dependency(%q<json>, [">= 0"])
45
48
  s.add_development_dependency(%q<rake>, [">= 0"])
46
49
  s.add_development_dependency(%q<rspec>, [">= 0"])
47
50
  s.add_development_dependency(%q<guard>, [">= 0"])
@@ -50,6 +53,7 @@ Gem::Specification.new do |s|
50
53
  s.add_runtime_dependency(%q<httparty>, [">= 0"])
51
54
  else
52
55
  s.add_dependency(%q<httparty>, [">= 0"])
56
+ s.add_dependency(%q<json>, [">= 0"])
53
57
  s.add_dependency(%q<rake>, [">= 0"])
54
58
  s.add_dependency(%q<rspec>, [">= 0"])
55
59
  s.add_dependency(%q<guard>, [">= 0"])
@@ -59,6 +63,7 @@ Gem::Specification.new do |s|
59
63
  end
60
64
  else
61
65
  s.add_dependency(%q<httparty>, [">= 0"])
66
+ s.add_dependency(%q<json>, [">= 0"])
62
67
  s.add_dependency(%q<rake>, [">= 0"])
63
68
  s.add_dependency(%q<rspec>, [">= 0"])
64
69
  s.add_dependency(%q<guard>, [">= 0"])
@@ -1,11 +1,12 @@
1
+ require 'json'
1
2
  require 'httparty'
2
3
 
3
4
  class Carmine
4
5
  include HTTParty
5
6
 
6
- # Public: The base error for the library.
7
+ # Public: The base error for the client.
7
8
  #
8
- # Raised when there is something wrong with the colorization request.
9
+ # Raised when there is something wrong with the colorization request.
9
10
  class Error < StandardError
10
11
  attr_reader :response
11
12
 
@@ -25,7 +26,12 @@ class Carmine
25
26
  # Examples
26
27
  #
27
28
  # Carmine.colorize "puts 'Hello World!'", :lexer => :ruby
29
+ # # Calls `Colorize.send(:instance).colorize(*args)`.
30
+ #
28
31
  # Carmine.pygmentize "puts 'Hello World!'", :lexer => :ruby
32
+ # # Calls `Colorize.send(:pygmentize).colorize(*args)`.
33
+ #
34
+ # Returns the delegated method call result.
29
35
  def method_missing(name, *args, &block)
30
36
  if instance.respond_to? name
31
37
  instance.send name, *args, &block
@@ -41,6 +47,7 @@ class Carmine
41
47
  end
42
48
  end
43
49
 
50
+ # Public: Sets the default server uri (default: 'pygmentize me').
44
51
  base_uri 'pygmentize.me'
45
52
 
46
53
  # Public: Gets/Sets the client default options.
@@ -67,7 +74,7 @@ class Carmine
67
74
  # :formatter - The name of the formatter to use (optional).
68
75
  # :lexer - The name of the lexer to use (optiona).
69
76
  #
70
- # Examples
77
+ # Examples
71
78
  #
72
79
  # colorize "puts 'Hello World!'", :lexer => :ruby, :formatter => :text
73
80
  # # => "puts 'Hello World!'"
@@ -83,7 +90,7 @@ class Carmine
83
90
 
84
91
  raise ArgumentError, "Argument or option :code missing" if params[:code].nil?
85
92
 
86
- response = post "/api/formatter/#{options[:formatter]}", :query => params
93
+ response = post "/api/formatter/#{params.delete :formatter}", :query => params
87
94
 
88
95
  raise Error, response unless response.success?
89
96
 
@@ -99,8 +106,11 @@ class Carmine
99
106
  private
100
107
 
101
108
  def post(path, options = {})
109
+ options[:query_string_normalizer] ||= Conversion.method :to_params
110
+
102
111
  self.class.post path, @defaults.merge(options)
103
112
  end
104
113
  end
105
114
 
106
115
  require 'carmine/version'
116
+ require 'carmine/conversion'
@@ -0,0 +1,27 @@
1
+ class Carmine
2
+ # Internal: Query string conversion utilities.
3
+ module Conversion
4
+ extend self
5
+
6
+ # Public: Jsonifies the request :query values to a query string.
7
+ #
8
+ # Examples
9
+ #
10
+ # Converter.to_param :options => { :cssclass => :color }
11
+ # # => options=%7B%22cssclass%22%3A%22color%22%7D
12
+ #
13
+ # Returns the uri encoded query string.
14
+ def to_params(query)
15
+ params = query.map { |key, value| normalize_param key, value }.join
16
+ params.chop!
17
+ params
18
+ end
19
+
20
+ private
21
+
22
+ def normalize_param(key, value)
23
+ [Array, Hash].each { |kind| value = value.to_json if value.is_a? kind }
24
+ HTTParty::HashConversions.normalize_param key, value
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ describe Carmine::Conversion do
2
+ it 'converts the Hash query values to JSON' do
3
+ query = Carmine::Conversion.to_params :options => { :cssclass => :color }
4
+ query.should eq("options=%7B%22cssclass%22%3A%22color%22%7D")
5
+ end
6
+
7
+ it 'converts the Array query values to JSON' do
8
+ query = Carmine::Conversion.to_params :styles => [:monokai]
9
+ query.should eq("styles=%5B%22monokai%22%5D")
10
+ end
11
+
12
+ it 'should url encode the strings and symbols' do
13
+ query = Carmine::Conversion.to_params :styles => " "
14
+ query.should eq("styles=%20")
15
+ end
16
+ end
@@ -31,10 +31,15 @@ describe Carmine do
31
31
  let(:carmine) { access == '::' ? Carmine : Carmine.new }
32
32
 
33
33
  it 'should colorize code :lexer and :formatter' do
34
- carmine.colorize('puts "Hello World!"', :lexer => :ruby, :formatter => 'html').should ==
34
+ carmine.colorize('puts "Hello World!"', :lexer => :ruby, :formatter => :html).should ==
35
35
  %Q{<div class="highlight"><pre><span class="nb">puts</span> <span class="s2">&quot;Hello World!&quot;</span>\n</pre></div>\n}
36
36
  end
37
37
 
38
+ it 'should convert arrays and hashes to JSON' do
39
+ carmine.colorize('puts "Hello World!"', :lexer => :ruby, :options => { :cssclass => :color }).should ==
40
+ %Q{<div class="color"><pre><span class="nb">puts</span> <span class="s2">&quot;Hello World!&quot;</span>\n</pre></div>\n}
41
+ end
42
+
38
43
  it 'should throw Carmine::Error on server side errors' do
39
44
  colorization = proc do
40
45
  carmine.colorize 'pass', :lexer => :python, :formatter => :invalid
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carmine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-01 00:00:00.000000000Z
12
+ date: 2012-03-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
- requirement: &16903100 !ruby/object:Gem::Requirement
16
+ requirement: &11995760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *16903100
24
+ version_requirements: *11995760
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &11995220 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *11995220
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rake
27
- requirement: &16902580 !ruby/object:Gem::Requirement
38
+ requirement: &11994700 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: '0'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *16902580
46
+ version_requirements: *11994700
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rspec
38
- requirement: &16902080 !ruby/object:Gem::Requirement
49
+ requirement: &11994180 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: '0'
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *16902080
57
+ version_requirements: *11994180
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: guard
49
- requirement: &16901600 !ruby/object:Gem::Requirement
60
+ requirement: &11993680 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *16901600
68
+ version_requirements: *11993680
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: guard-rspec
60
- requirement: &16901120 !ruby/object:Gem::Requirement
71
+ requirement: &11993120 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: '0'
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *16901120
79
+ version_requirements: *11993120
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: jeweler
71
- requirement: &16900600 !ruby/object:Gem::Requirement
82
+ requirement: &11990140 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,10 +87,10 @@ dependencies:
76
87
  version: '0'
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *16900600
90
+ version_requirements: *11990140
80
91
  - !ruby/object:Gem::Dependency
81
92
  name: httparty
82
- requirement: &16900060 !ruby/object:Gem::Requirement
93
+ requirement: &11989660 !ruby/object:Gem::Requirement
83
94
  none: false
84
95
  requirements:
85
96
  - - ! '>='
@@ -87,7 +98,7 @@ dependencies:
87
98
  version: '0'
88
99
  type: :runtime
89
100
  prerelease: false
90
- version_requirements: *16900060
101
+ version_requirements: *11989660
91
102
  description: Ruby client for pygmentize.me
92
103
  email: gsamokovarov@gmail.com
93
104
  executables: []
@@ -104,7 +115,9 @@ files:
104
115
  - VERSION
105
116
  - carmine.gemspec
106
117
  - lib/carmine.rb
118
+ - lib/carmine/conversion.rb
107
119
  - lib/carmine/version.rb
120
+ - spec/carmine/conversion_spec.rb
108
121
  - spec/carmine/version_spec.rb
109
122
  - spec/carmine_spec.rb
110
123
  - spec/spec_helper.rb