Skip to content

Commit 9925332

Browse files
committed
add google_static_map_url
1 parent 906bb27 commit 9925332

File tree

6 files changed

+88
-11
lines changed

6 files changed

+88
-11
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Liquid
2+
module Rails
3+
module GoogleStaticMapUrlFilter
4+
5+
# size: '600x300'
6+
#
7+
# Available keys inside options
8+
# center: '40.714728,-73.998672'
9+
# zoom: 13
10+
# maptype: 'roadmap', 'satellite', 'terrain', or 'hybrid'
11+
# markers: an array of this 'color:blue|label:S|40.702147,-74.015794'
12+
# or string with semicolon
13+
def google_static_map_url(size, options={})
14+
markers = options.delete('markers')
15+
markers = if markers
16+
markers = markers.split(';') if markers.is_a?(String)
17+
markers.map { |marker| { markers: marker }.to_query }
18+
else
19+
''
20+
end
21+
querystring = [options.to_query, markers].delete_if { |value| value.blank? }.join('&')
22+
23+
"https://maps.googleapis.com/maps/api/staticmap?#{querystring}"
24+
end
25+
end
26+
end
27+
end
28+
29+
Liquid::Template.register_filter(Liquid::Rails::GoogleStaticMapUrlFilter)

lib/liquid-rails/matchers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'liquid-rails/rspec/view_controller_context'
22
require 'liquid-rails/rspec/drop_matchers'
33
require 'liquid-rails/rspec/drop_example_group'
4-
require 'liquid-rails/rspec/tag_example_group'
4+
require 'liquid-rails/rspec/tag_example_group'
5+
require 'liquid-rails/rspec/filter_example_group'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Liquid
2+
module Rails
3+
module Rspec
4+
module FilterExampleGroup
5+
extend ActiveSupport::Concern
6+
include Liquid::Rails::Rspec::ViewControllerContext
7+
8+
included do
9+
metadata[:type] = :filter
10+
11+
before(:all) { setup_view_and_controller }
12+
end
13+
end
14+
end
15+
end
16+
end
17+
18+
RSpec.configure do |config|
19+
if RSpec::Core::Version::STRING.starts_with?('3')
20+
config.include Liquid::Rails::Rspec::FilterExampleGroup, type: :filter, file_path: %r(spec/filters)
21+
else
22+
config.include Liquid::Rails::Rspec::FilterExampleGroup, type: :filter, example_group: { file_path: %r{spec/filters} }
23+
end
24+
end

lib/liquid-rails/rspec/tag_example_group.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ module TagExampleGroup
1010

1111
before(:all) { setup_view_and_controller }
1212
end
13-
14-
def expect_template_result(template, expected, assigns={})
15-
# make assigns available inside context
16-
assigns.each do |key, value|
17-
context[key] = value
18-
end
19-
20-
actual = Liquid::Template.parse(template).render!(context)
21-
expect(actual).to eq(expected)
22-
end
2313
end
2414
end
2515
end

lib/liquid-rails/rspec/view_controller_context.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ def controller
2828
def context(assigns={})
2929
@context ||= ::Liquid::Context.new(assigns, {}, { helper: @view, view: @view, controller: @controller })
3030
end
31+
32+
def expect_template_result(template, expected, assigns={})
33+
# make assigns available inside context
34+
assigns.each do |key, value|
35+
context[key] = value
36+
end
37+
38+
actual = Liquid::Template.parse(template).render!(context)
39+
expect(actual).to eq(expected)
40+
end
3141
end
3242
end
3343
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'spec_helper'
2+
3+
module Liquid
4+
module Rails
5+
describe GoogleStaticMapUrlFilter, type: :filter do
6+
it 'returns url with center' do
7+
expect_template_result("{{ '600x300' | google_static_map_url: center: '40.714728,-73.998672' }}", 'https://maps.googleapis.com/maps/api/staticmap?center=40.714728%2C-73.998672')
8+
end
9+
10+
it 'returns url with zoom' do
11+
expect_template_result("{{ '600x300' | google_static_map_url: zoom: 13 }}", 'https://maps.googleapis.com/maps/api/staticmap?zoom=13')
12+
end
13+
14+
it 'returns url with maptype' do
15+
expect_template_result("{{ '600x300' | google_static_map_url: maptype: 'hybrid' }}", 'https://maps.googleapis.com/maps/api/staticmap?maptype=hybrid')
16+
end
17+
18+
it 'returns url with markers' do
19+
expect_template_result("{{ '600x300' | google_static_map_url: markers: 'color:blue|label:S|40.702147,-74.015794;color:green|label:G|40.711614,-74.012318' }}", 'https://maps.googleapis.com/maps/api/staticmap?markers=color%3Ablue%7Clabel%3AS%7C40.702147%2C-74.015794&markers=color%3Agreen%7Clabel%3AG%7C40.711614%2C-74.012318')
20+
end
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)