carto_json 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,9 +5,10 @@ module CartoJson
5
5
  attr_accessor :radius
6
6
 
7
7
  def radius=(r)
8
+ r = r.to_f
9
+ # raise InvalidRadiusError, 'radius must be a number' unless (r.is_a?(Integer) || r.is_a?(Float))
8
10
  raise InvalidRadiusError, 'radius cannot be negative' if r < 0
9
11
  raise InvalidRadiusError, 'radius cannot be blank' if r.nil?
10
- raise InvalidRadiusError, 'radius must be a number' unless (r.is_a?(Integer) || r.is_a?(Float))
11
12
  raise InvalidRadiusError, 'radius cannot be zero' if r == 0
12
13
  @radius = r
13
14
  end
@@ -22,11 +23,9 @@ module CartoJson
22
23
 
23
24
  def to_hash
24
25
  {:type => self.class.type,
25
- :center => {
26
- LAT => send(LAT),
27
- LNG => send(LNG)
28
- },
29
- :radius => radius
26
+ LAT => send(LAT),
27
+ LNG => send(LNG),
28
+ :radius => radius
30
29
  }
31
30
  end
32
31
  end
@@ -4,5 +4,13 @@ module CartoJson
4
4
  class InputError < Error; end
5
5
  class InsufficientPointsError < Error; end
6
6
  class InvalidRadiusError < Error; end
7
- class InvalidTypeError < Error; end
7
+
8
+ class InvalidTypeError < Error
9
+ attr_reader :type
10
+
11
+ def initialize(type, message)
12
+ @type = type.to_sym
13
+ super message
14
+ end
15
+ end
8
16
  end
@@ -25,7 +25,7 @@ module CartoJson
25
25
  end
26
26
 
27
27
  def to_wkt
28
- "#{type.to_s.upcase} ((#{@points.dup.push(@points.first).collect {|p| "#{p.send(LNG)} #{p.send(LAT)}"}.join(', ')}))"
28
+ "#{type.to_s.upcase} (#{@points.dup.collect {|p| "#{p.send(LNG)} #{p.send(LAT)}"}.join(', ')})"
29
29
  end
30
30
 
31
31
  protected
@@ -46,10 +46,13 @@ module CartoJson
46
46
  end
47
47
 
48
48
  unless TYPES.include? element[:type].to_sym
49
- raise InvalidTypeError, "unsupported type: \"#{element[:type]}\", supported types: #{TYPES.join(', ')}"
49
+ raise InvalidTypeError.new(element[:type].to_sym,
50
+ "unsupported type: \"#{element[:type]}\", supported types: #{TYPES.join(', ')}")
50
51
  end
52
+
53
+ class_name = element[:type] == 'linestring' ? 'LineString' : element[:type].capitalize
51
54
 
52
- CartoJson.const_get(element[:type].capitalize).parse element
55
+ CartoJson.const_get(class_name).parse element
53
56
  end
54
57
  end
55
58
  end
@@ -1,7 +1,11 @@
1
1
  module CartoJson
2
2
  class Polygon < LineString
3
3
  type :polygon
4
-
4
+
5
+ def to_wkt
6
+ "#{type.to_s.upcase} ((#{@points.dup.push(@points.first).collect {|p| "#{p.send(LNG)} #{p.send(LAT)}"}.join(', ')}))"
7
+ end
8
+
5
9
  protected
6
10
 
7
11
  def validate
@@ -0,0 +1,14 @@
1
+ module CartoJson
2
+ class Rectangle
3
+ def initialize(opts={})
4
+ raise "sw and ne coordinates are required" unless opts[:sw] && opts[:ne]
5
+ @sw = opts[:sw].to_f
6
+ @ne = opts[:ne].to_f
7
+ raise "sw and ne coordinates cannot be equal" if @sw == @ne
8
+ end
9
+
10
+ def to_wkt
11
+
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module CartoJson
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -26,7 +26,7 @@ module CartoJson
26
26
  Point.new xy_to_points(@input.match(/point \((.+)\)/i).captures.first).first
27
27
  else
28
28
  raise NotImplementedError, 'multi types are not implemented yet' if MULTI_TYPES.include? type.downcase
29
- raise Input Error, "invalid type: #{type}"
29
+ raise InvalidTypeError, type, "invalid type: #{type}"
30
30
  end
31
31
  end
32
32
 
data/lib/carto_json.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module CartoJson
2
- PRIMITIVE_TYPES = [:point, :linestring, :polygon, :rectangle]
2
+ PRIMITIVE_TYPES = [:circle, :point, :linestring, :polygon, :rectangle]
3
3
  MULTI_TYPES = [:multipoint, :multilinestring, :multipolygon]
4
4
  TYPES = PRIMITIVE_TYPES+MULTI_TYPES
5
5
 
@@ -26,6 +26,7 @@ require 'carto_json/point'
26
26
  require 'carto_json/circle'
27
27
  require 'carto_json/line_string'
28
28
  require 'carto_json/polygon'
29
+ require 'carto_json/rectangle'
29
30
  require 'carto_json/wkt_parser'
30
31
  require 'carto_json/version'
31
32
  require 'multi_json'
data/spec/circle_spec.rb CHANGED
@@ -16,8 +16,8 @@ describe CartoJson::Circle do
16
16
 
17
17
  hash = MultiJson.decode(circle.to_json, :symbolize_keys => true)
18
18
 
19
- hash[:center][LAT].must_equal @args[LAT]
20
- hash[:center][LNG].must_equal @args[LNG]
19
+ hash[LAT].must_equal @args[LAT]
20
+ hash[LNG].must_equal @args[LNG]
21
21
  hash[:radius].must_equal @args[:radius]
22
22
  end
23
23
 
@@ -21,7 +21,7 @@ describe LineString do
21
21
  hash[:points].first[LAT].must_equal @args[:points].first[LAT]
22
22
  hash[:points].first[LNG].must_equal @args[:points].first[LNG]
23
23
 
24
- ls.to_wkt.must_equal "LINESTRING ((#{ls.points.dup.push(ls.points.first).collect {|p| "#{p.send(LNG)} #{p.send(LAT)}"}.join(', ')}))"
24
+ ls.to_wkt.must_equal "LINESTRING (#{ls.points.dup.collect {|p| "#{p.send(LNG)} #{p.send(LAT)}"}.join(', ')})"
25
25
  end
26
26
 
27
27
  it 'should fail if less than three points' do
@@ -0,0 +1,9 @@
1
+ require File.join File.dirname(__FILE__), 'env.rb'
2
+
3
+ include CartoJson
4
+
5
+ describe CartoJson::Rectangle do
6
+ before do
7
+
8
+ end
9
+ end
@@ -25,4 +25,12 @@ describe CartoJson::WKTParser do
25
25
  ls.points.first.must_equal Point.new(LAT => 1, LNG => 2)
26
26
  ls.points.last.must_equal Point.new(LAT => 3, LNG => 4)
27
27
  end
28
+
29
+ it 'works with linestring' do
30
+ ls = WKTParser.parse "LINESTRING (2 1, 4 3)"
31
+ ls.class.must_equal LineString
32
+ ls.type.must_equal :linestring
33
+ ls.points.first.must_equal Point.new(LAT => 1, LNG => 2)
34
+ ls.points.last.must_equal Point.new(LAT => 3, LNG => 4)
35
+ end
28
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carto_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
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-04-09 00:00:00.000000000 Z
12
+ date: 2012-04-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
16
- requirement: &70336033905880 !ruby/object:Gem::Requirement
16
+ requirement: &70323933072040 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70336033905880
24
+ version_requirements: *70323933072040
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70336033905460 !ruby/object:Gem::Requirement
27
+ requirement: &70323933071620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70336033905460
35
+ version_requirements: *70323933071620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: pry
38
- requirement: &70336033905040 !ruby/object:Gem::Requirement
38
+ requirement: &70323933071200 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70336033905040
46
+ version_requirements: *70323933071200
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: minitest
49
- requirement: &70336033904620 !ruby/object:Gem::Requirement
49
+ requirement: &70323933070780 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70336033904620
57
+ version_requirements: *70323933070780
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: json
60
- requirement: &70336033904200 !ruby/object:Gem::Requirement
60
+ requirement: &70323933070360 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70336033904200
68
+ version_requirements: *70323933070360
69
69
  description: Helpers for the CartoJSON specification
70
70
  email:
71
71
  - kyledrake@gmail.com
@@ -87,6 +87,7 @@ files:
87
87
  - lib/carto_json/parser.rb
88
88
  - lib/carto_json/point.rb
89
89
  - lib/carto_json/polygon.rb
90
+ - lib/carto_json/rectangle.rb
90
91
  - lib/carto_json/shape.rb
91
92
  - lib/carto_json/utils.rb
92
93
  - lib/carto_json/version.rb
@@ -98,6 +99,7 @@ files:
98
99
  - spec/parser_spec.rb
99
100
  - spec/point_spec.rb
100
101
  - spec/polygon_spec.rb
102
+ - spec/rectangle_spec.rb
101
103
  - spec/wkt_parser_spec.rb
102
104
  homepage: ''
103
105
  licenses: []
@@ -131,5 +133,6 @@ test_files:
131
133
  - spec/parser_spec.rb
132
134
  - spec/point_spec.rb
133
135
  - spec/polygon_spec.rb
136
+ - spec/rectangle_spec.rb
134
137
  - spec/wkt_parser_spec.rb
135
138
  has_rdoc: