Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/geogle/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions lib/geogle/model/leg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions lib/geogle/model/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
12 changes: 7 additions & 5 deletions lib/geogle/url_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/geogle/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Geogle
VERSION = "0.5.4"
VERSION = "0.6.1"
end
7 changes: 6 additions & 1 deletion spec/geogle/model/leg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -23,4 +28,4 @@
it "destination" do
expect(leg.end_address).to eq("Madrid, Madrid, Spain")
end
end
end
7 changes: 6 additions & 1 deletion spec/geogle/model/route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -75,4 +80,4 @@ def coordinates(lat, lng)
end
end
end
end
end
28 changes: 25 additions & 3 deletions spec/geogle/url_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
Expand Down
4 changes: 2 additions & 2 deletions wercker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,4 +12,4 @@ build:
echo -p "gem list: $(gem list)"
- script:
name: rspec
code: bundle exec rspec
code: bundle exec rspec