Skip to content

Commit

Permalink
Merge pull request #11 from apoex/27483-update-postnord-api
Browse files Browse the repository at this point in the history
[#27483] Update transit times api
  • Loading branch information
dvstrauss authored May 22, 2023
2 parents 55ee72a + c39551e commit 16473d5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 27 deletions.
12 changes: 8 additions & 4 deletions lib/postnord/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.call(params)
end

def self.client
@client ||= Client.new
@client ||= Client.new(options)
end

private
Expand All @@ -23,15 +23,19 @@ def self.validate_params(params)
end

def self.action
self.name.split('::').last.tap { |e| e[0] = e[0].downcase }
self.name.split("::").last.tap { |e| e[0] = e[0].downcase }
end

def self.mandatory_params
fail NotImplementedError, 'mandatory_params'
fail NotImplementedError, "mandatory_params"
end

def self.endpoint
fail NotImplementedError, 'endpoint'
fail NotImplementedError, "endpoint"
end

def self.options
{}
end
end
end
20 changes: 12 additions & 8 deletions lib/postnord/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ def initialize(options={})
@api_key = options[:api_key] || Config.api_key
@api_endpoint = options[:api_endpoint] || Config.api_endpoint
@locale = options[:locale] || Config.locale
@return_type = options[:return_type] || Config.return_type
@return_type = if options.has_key?(:return_type)
options[:return_type]
else
Config.return_type
end
end

def do_request(service, endpoint, params={})
Expand All @@ -27,13 +31,13 @@ def do_request(service, endpoint, params={})
private

def build_uri(service, endpoint)
URI(
@api_endpoint +
'/' + service +
'/' + @api_version +
'/' + endpoint +
'.' + @return_type
)
url = @api_endpoint +
"/" + service +
"/" + @api_version +
"/" + endpoint
url += "." + @return_type if @return_type

URI(url)
end
end
end
21 changes: 21 additions & 0 deletions lib/postnord/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ def self.endpoint
end
end

class AddressToAddress < Transport
def self.mandatory_params
[
"startTime",
"serviceGroup",
"originPostCode",
"originCountryCode",
"destinationPostCode",
"destinationCountryCode",
]
end

def self.action
self.name.split('::').last.downcase
end

def self.options
{ return_type: nil }
end
end

class GetTransitTimeInformation < Transport
def self.mandatory_params
[
Expand Down
2 changes: 1 addition & 1 deletion lib/postnord/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Postnord
VERSION = "0.3.0"
VERSION = "1.0.0"
end
12 changes: 7 additions & 5 deletions postnord.gemspec
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'postnord/version'
require "postnord/version"

Gem::Specification.new do |spec|
spec.name = "postnord"
spec.version = Postnord::VERSION
spec.authors = ["Stefan Åhman"]
spec.email = ["[email protected]"]

spec.summary = %q{A gem for Postnord's API}
spec.homepage = 'https://github.com/apoex/postnord'
spec.summary = %q{A gem for Postnord"s API}
spec.homepage = "https://github.com/apoex/postnord"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency 'faraday'
spec.add_dependency "faraday"
spec.add_development_dependency "webmock"
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake", ">= 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "pry-byebug"
spec.add_development_dependency "pry-byebyebug"
spec.add_development_dependency "simplecov", "~> 0.17.1"
end
10 changes: 5 additions & 5 deletions spec/postnord/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require 'spec_helper'
require "spec_helper"

RSpec.describe Postnord::Client do
subject { Postnord::Client.new(api_version: 'v1', api_key: 'test', api_endpoint: 'test_endpoint', locale: 'sv', return_type: 'json') }
subject { Postnord::Client.new(api_version: "v1", api_key: "test", api_endpoint: "http://test_endpoint", locale: "sv", return_type: "json") }

before do
stub_request(:get, /test_endpoint/).to_return(status: 200, body: '{ "test": "data" }', headers: {})
end

it 'does the request' do
res = subject.do_request('service', 'endpoint')
it "does the request" do
res = subject.do_request("service", "endpoint")

expect(res.code).to eq(200)
expect(res.data).to eq({ 'test' => 'data' })
expect(res.data).to eq({ "test" => "data" })
end
end
10 changes: 6 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'bundler/setup'
require 'webmock/rspec'
require "bundler/setup"
require "webmock/rspec"

require 'simplecov'
require "simplecov"

SimpleCov.start

require 'postnord'
require "postnord"
require "pry-byebyebug"
require "pry-byebug"

0 comments on commit 16473d5

Please sign in to comment.