diff --git a/lib/geogle/base.rb b/lib/geogle/base.rb index 0982793..87dc219 100644 --- a/lib/geogle/base.rb +++ b/lib/geogle/base.rb @@ -18,9 +18,6 @@ def request(url, params) response = Net::HTTP.get_response(uri) raise InvalidKeyError if response.code == "403" body = JSON.parse(response.body) - # puts "#{params[:origin]}_to_#{params[:destination]}" - # puts " " - # File.open("fixtures/#{params[:origin]}_to_#{params[:destination]}.json", 'w'){ |f| f.write(response.body)} ErrorHandler.check(body['status']) body end diff --git a/lib/geogle/model/leg.rb b/lib/geogle/model/leg.rb index c752b94..27941c2 100644 --- a/lib/geogle/model/leg.rb +++ b/lib/geogle/model/leg.rb @@ -12,15 +12,16 @@ module Model class Leg include Virtus.model - attribute :steps, Array[Step] - attribute :distance, TextValue - attribute :duration, TextValue - attribute :arrival_time, Time - attribute :departure_time, Time - attribute :start_address, String - attribute :end_address, String - attribute :start_location, Coordinates - attribute :end_location, Coordinates + attribute :steps, Array[Step] + attribute :distance, TextValue + attribute :duration, TextValue + attribute :duration_in_traffic, TextValue + attribute :arrival_time, Time + attribute :departure_time, Time + attribute :start_address, String + attribute :end_address, String + attribute :start_location, Coordinates + attribute :end_location, Coordinates end end end diff --git a/lib/geogle/model/route.rb b/lib/geogle/model/route.rb index b627aff..dc1dba1 100644 --- a/lib/geogle/model/route.rb +++ b/lib/geogle/model/route.rb @@ -23,6 +23,13 @@ def duration sum_values(legs.map(&:duration)) end + # Total duration in traffic + def duration_in_traffic + sum_values(legs.map do |leg| + leg.duration_in_traffic.nil? ? leg.duration : leg.duration_in_traffic + end) + end + # Total distance def distance sum_values(legs.map(&:distance)) diff --git a/lib/geogle/url_builder.rb b/lib/geogle/url_builder.rb index 3dce5b4..c49af19 100644 --- a/lib/geogle/url_builder.rb +++ b/lib/geogle/url_builder.rb @@ -7,16 +7,18 @@ module Geogle class UrlBuilder - def initialize(url, business_attrs = {}) + def initialize(url, attrs = {}) @url = url - @client_id = business_attrs[:client_id] - @crypto_key = business_attrs[:crypto_key] + @client_id = attrs[:client_id] + @crypto_key = attrs[:crypto_key] + @key = attrs[:key] end def build(params) uri = URI(@url) + params.merge!(key: @key) if @key && !business? uri.query = URI.encode_www_form(params) - return sign(uri) if is_business? + return sign(uri) if business? uri end @@ -44,7 +46,7 @@ def url_safe_base64_encode(raw) return Base64.encode64(raw).tr('+/','-_') end - def is_business? + def business? @client_id && @crypto_key end end diff --git a/lib/geogle/version.rb b/lib/geogle/version.rb index 9de9409..5c138fd 100644 --- a/lib/geogle/version.rb +++ b/lib/geogle/version.rb @@ -1,3 +1,3 @@ module Geogle - VERSION = "0.5.4" + VERSION = "0.6.1" end diff --git a/spec/geogle/model/leg_spec.rb b/spec/geogle/model/leg_spec.rb index 262cc0d..fb832dd 100644 --- a/spec/geogle/model/leg_spec.rb +++ b/spec/geogle/model/leg_spec.rb @@ -2,6 +2,7 @@ let(:leg) do described_class.new({ duration: {text: "duration", value: 11756}, + duration_in_traffic: {text: "duration_in_traffic", value: 12756}, distance: {text: "distance", value: 355055}, start_address: "Valencia, Valencia, Spain", end_address: "Madrid, Madrid, Spain" @@ -12,6 +13,10 @@ expect(leg.duration.value).to eq(11756) end + it "duration_in_traffic" do + expect(leg.duration.value).to eq(12756) + end + it "distance" do expect(leg.distance.value).to eq(355055) end @@ -23,4 +28,4 @@ it "destination" do expect(leg.end_address).to eq("Madrid, Madrid, Spain") end -end \ No newline at end of file +end diff --git a/spec/geogle/model/route_spec.rb b/spec/geogle/model/route_spec.rb index 1486ba1..bd9909c 100644 --- a/spec/geogle/model/route_spec.rb +++ b/spec/geogle/model/route_spec.rb @@ -31,6 +31,7 @@ def coordinates(lat, lng) let(:leg2) do Leg.new( duration: text_value(20), + duration_in_traffic: text_value(25), distance: text_value(30), end_address: "Moon", steps: [step3, step4] @@ -43,6 +44,10 @@ def coordinates(lat, lng) expect(route.duration).to eq(30) end + it "duration_in_traffic adds the durations of all legs (or the duration_in_traffic where available)" do + expect(route.duration).to eq(35) + end + it "distance adds the distances of all legs" do expect(route.distance).to eq(50) end @@ -75,4 +80,4 @@ def coordinates(lat, lng) end end end -end \ No newline at end of file +end diff --git a/spec/geogle/url_builder_spec.rb b/spec/geogle/url_builder_spec.rb index 251d6d3..2b78211 100644 --- a/spec/geogle/url_builder_spec.rb +++ b/spec/geogle/url_builder_spec.rb @@ -1,6 +1,6 @@ describe Geogle::UrlBuilder do let(:uri) { "https://www.foo.com" } - let(:builder) { described_class.new(uri, business_attrs)} + let(:builder) { described_class.new(uri, attrs)} let(:params) do { address: "Street FooBar", @@ -11,16 +11,38 @@ let(:built_url) { builder.build(params) } describe "when the client_id and crypto_key are not passed as arguments" do - let(:business_attrs) { {} } + let(:attrs) { {} } it "the built url doesn't contain a query param signature" do expect(built_url.to_s).to eq("https://www.foo.com?address=Street+FooBar&language=de&sensor=true") end end + describe "when the key is passed as argument" do + let(:attrs) { { key: "123njsdfjahs" } } + + it "the built url includes the key" do + expect(built_url.to_s).to eq("https://www.foo.com?address=Street+FooBar&language=de&sensor=true&key=123njsdfjahs") + end + end + describe "when the client_id and crypto_key are passed as arguments" do - let(:business_attrs) do + let(:attrs) do + { + client_id: "gme-clientid", + crypto_key: "crypto-key" + } + end + + it "the built url contains a query param signature" do + expect(built_url.to_s).to eq("https://www.foo.com?client=gme-clientid&address=Street+FooBar&language=de&sensor=true&signature=1klekRivPNXr3vOpOixX16LNGuI=") + end + end + + describe "when the client_id, crypto_key and key are passed as arguments" do + let(:attrs) do { + key: "123njsdfjahs", client_id: "gme-clientid", crypto_key: "crypto-key" } diff --git a/wercker.yml b/wercker.yml index 3443f0c..3a8443c 100644 --- a/wercker.yml +++ b/wercker.yml @@ -2,7 +2,7 @@ box: wercker/rvm build: steps: - rvm-use: - version: 2.1.2 + version: 2.2.3 - bundle-install - script: name: echo ruby information @@ -12,4 +12,4 @@ build: echo -p "gem list: $(gem list)" - script: name: rspec - code: bundle exec rspec \ No newline at end of file + code: bundle exec rspec