-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor into a more adapter like pattern
- Loading branch information
1 parent
1994430
commit cbdf5ac
Showing
19 changed files
with
311 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Underpass | ||
# Runs the Overpass API query | ||
class Client | ||
API_URI = 'https://overpass-api.de/api/interpreter' | ||
|
||
# Performs the API request | ||
def self.perform(request) | ||
Net::HTTP.post_form(URI(API_URI), data: request.to_query) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module Underpass | ||
# Provides matches given a response object | ||
# By matches we understand response elements that have a tags key | ||
class Matcher | ||
attr_reader :matches | ||
|
||
def initialize(response) | ||
@nodes = response.nodes | ||
@ways = response.ways | ||
@matches = [] | ||
|
||
@nodes.each_value do |node| | ||
@matches << point_from_node(node) if node.key?(:tags) | ||
end | ||
|
||
@ways.each_value do |way| | ||
@matches << way_matches(way) if way.key?(:tags) | ||
end | ||
end | ||
|
||
private | ||
|
||
def way_matches(way) | ||
if open_way?(way) | ||
polygon_from_way(way, @nodes) | ||
else | ||
line_string_from_way(way, @nodes) | ||
end | ||
end | ||
|
||
def open_way?(way) | ||
Underpass::Shape.open_way?(way) | ||
end | ||
|
||
def point_from_node(node) | ||
Underpass::Shape.point_from_node(node) | ||
end | ||
|
||
def polygon_from_way(way, nodes) | ||
Underpass::Shape.polygon_from_way(way, nodes) | ||
end | ||
|
||
def line_string_from_way(way, nodes) | ||
Underpass::Shape.line_string_from_way(way, nodes) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Underpass | ||
module QL | ||
# Deals with parsing the API response into easily digestable | ||
# nodes and ways objects | ||
class Response | ||
def initialize(api_response) | ||
parsed_json = JSON.parse(api_response.body, symbolize_names: true) | ||
@elements = parsed_json[:elements] | ||
end | ||
|
||
# Returns all node type elements from the response | ||
def nodes | ||
nodes = @elements.select { |e| e[:type] == 'node' } | ||
nodes.map { |e| [e[:id], e] }.to_h | ||
end | ||
|
||
# Returns all way type elements from the response | ||
def ways | ||
ways = @elements.select { |e| e[:type] == 'way' } | ||
ways.map { |e| [e[:id], e] }.to_h | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'underpass' | ||
|
||
describe Underpass::Client do | ||
let(:request_double) { double } | ||
let(:query) { 'query_test' } | ||
let(:bbox) { 'bbox_test' } | ||
subject { described_class } | ||
describe '#perform' do | ||
it 'posts the query to the API endpoint' do | ||
allow(request_double).to receive(:to_query).and_return( | ||
query + ' ' + bbox | ||
) | ||
stub = stub_request(:post, 'https://overpass-api.de/api/interpreter') | ||
.with(body: /#{bbox}/) | ||
subject.perform(request_double) | ||
expect(stub).to have_been_requested | ||
end | ||
end | ||
end |
Oops, something went wrong.