Skip to content

Commit

Permalink
Add specs to client and response (#5)
Browse files Browse the repository at this point in the history
* Add spec to client

* Add specs for response

* attr_reader
  • Loading branch information
Hornwall authored May 14, 2020
1 parent b0c7bde commit 55ee72a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/postnord/response.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module Postnord
class Response
attr_reader :code

def initialize(data)
@data = data
@code = data.status
end

def code
@data.code
end

def data
JSON.parse(@data.body)
rescue JSON::ParserError
Expand All @@ -17,7 +15,7 @@ def data

def to_h
{
code: @code,
code: code,
data: data,
}
end
Expand Down
1 change: 1 addition & 0 deletions postnord.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

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"
Expand Down
16 changes: 16 additions & 0 deletions spec/postnord/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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') }

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')

expect(res.code).to eq(200)
expect(res.data).to eq({ 'test' => 'data' })
end
end
28 changes: 28 additions & 0 deletions spec/postnord/response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

RSpec.describe Postnord::Response do
let(:body) { { 'test' => 'body' } }
let(:data) { OpenStruct.new(status: 200, body: body.to_json) }
subject { Postnord::Response.new(data) }

describe '#code' do
it 'returns the status from data' do
expect(subject.code).to eq(200)
end
end

describe '#data' do
it 'returns the desirialized json body' do
expect(subject.data).to eq(body)
end
end

describe 'to_h' do
it 'returns a hash based on the given data' do
expect(subject.to_h).to eq({
code: 200,
data: body
})
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'bundler/setup'
require 'webmock/rspec'

require 'simplecov'

Expand Down

0 comments on commit 55ee72a

Please sign in to comment.