Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from Fretadao/bv-add-contact-list-service
Browse files Browse the repository at this point in the history
Add contact list service
  • Loading branch information
bvicenzo authored Mar 2, 2023
2 parents 498b903 + 77c66d9 commit 0d7d1a2
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
## [0.1.0] - 2023-03-02

- Initial release
- Add base gem;
- Add Contact list service (filtering by id);
7 changes: 5 additions & 2 deletions lib/ploomes_client.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# frozen_string_literal: true

require 'f_http_client'
require_relative 'ploomes_client/version'

require_relative 'ploomes_client/configuration'
require_relative 'ploomes_client/base'
require_relative 'ploomes_client/contact/list'

module PloomesClient
class Error < StandardError; end
# Your code goes here...
end
19 changes: 19 additions & 0 deletions lib/ploomes_client/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module PloomesClient
class Base < FHTTPClient::Base
def self.config
@config ||= PloomesClient::Configuration.config
end

private

def headers
{
'User-Key' => config.user_key,
'Content-Type' => 'application/json',
'odata.metadata' => 'minimal'
}
end
end
end
7 changes: 7 additions & 0 deletions lib/ploomes_client/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module PloomesClient
class Configuration < FHTTPClient::Configuration
setting :user_key
end
end
23 changes: 23 additions & 0 deletions lib/ploomes_client/contact/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module PloomesClient
module Contact
class List < PloomesClient::Base
option :id, default: -> {}

private

def make_request
self.class.get(formatted_path, headers: headers)
end

def path_template
'/Contacts?$filter=Id+eq+%<id>s'
end

def path_params
id.present? ? { id: id } : super
end
end
end
end
44 changes: 44 additions & 0 deletions spec/ploomes_client/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

RSpec.describe PloomesClient::Base do
describe '.config' do
it { expect(described_class.config).to eq(PloomesClient::Configuration.config) }
end

describe 'headers' do
subject(:client) do
Class.new(described_class) do
base_uri 'localhost:3000'

private

def make_request
self.class.get(formatted_path, headers: headers)
end

def path_template
'/test'
end
end
end

before do
PloomesClient::Configuration.configure do |config|
config.user_key = 'user-key-123'
end

stub_get(
{
headers: {
'User-Key' => 'user-key-123',
'Content-Type' => 'application/json',
'odata.metadata' => 'minimal'
}
},
to: 'localhost:3000/test'
)
end

it { expect(client.()).to have_succeed_with(:ok, :successful) }
end
end
53 changes: 53 additions & 0 deletions spec/ploomes_client/contact/list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

RSpec.describe PloomesClient::Contact::List do
describe 'run' do
let(:ploomes_response) { load_fixture_json_symbolized('contact/list/response_with_result.json') }
let(:id) { 123 }

before do
PloomesClient::Configuration.configure do |config|
config.base_uri = 'localhost:3000'
config.user_key = 'user-key-123'
end

stub_get(
{
headers: {
'User-Key' => 'user-key-123',
'Content-Type' => 'application/json',
'odata.metadata' => 'minimal'
}
},
to: "localhost:3000/Contacts?$filter=Id+eq+#{id}",
response_body: ploomes_response
)
end

describe 'filtering by path_params explicity' do
it 'filters by path params id', :aggregate_failures do
result = described_class.(path_params: { id: id })

expect(result).to have_succeed_with(:ok, :successful)

first_contact = result.value[:value].first

expect(first_contact[:Name]).to eq('Contact 1')
expect(first_contact[:Email]).to eq('[email protected]')
end
end

describe 'filtering by id directly' do
it 'filters id', :aggregate_failures do
result = described_class.(id: id)

expect(result).to have_succeed_with(:ok, :successful)

first_contact = result.value[:value].first

expect(first_contact[:Name]).to eq('Contact 1')
expect(first_contact[:Email]).to eq('[email protected]')
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'simplecov'
require 'pry-nav'

SimpleCov.start do
add_filter 'spec'
Expand Down
10 changes: 5 additions & 5 deletions spec/support/dry_configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

require 'dry/configurable/test_interface'

# PloomesClient::Configuration.enable_test_interface
#
# RSpec.configure do |config|
# config.before { PloomesClient::Configuration.reset_config }
# end
PloomesClient::Configuration.enable_test_interface

RSpec.configure do |config|
config.before { PloomesClient::Configuration.reset_config }
end

0 comments on commit 0d7d1a2

Please sign in to comment.