Skip to content

Commit

Permalink
base most client impemention
Browse files Browse the repository at this point in the history
  • Loading branch information
one-m1nd committed Feb 28, 2024
1 parent 0a7089f commit 9185a0a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/plants.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "plants/client"
require "plants/config"
require "plants/version"

module Plants
Expand Down
40 changes: 40 additions & 0 deletions lib/plants/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true
require 'http'

require 'plants/version'

module Plants
# HTTP Client
#
# @!attribute [r] config
# @return [Plants::Config]
#
class Client
# Trefle API URL
URL = "https://trefle.io/api/v1".freeze

# @param config [Plants::Config]
def initialize(config)
@config = config
end
attr_reader :config


# @param resource [String]
# @param params [Hash]
# @return [HTTP::Response]
def get(resource, params: {})
params.merge({ token: config.token }) if config.token.is_a?(String)
http.get("#{URL}/#{resource}", params: params)
end

private

# @return [HTTP::Client]
def http
HTTP
.timeout(5)
.headers({ 'User-Agent' => "Plants #{Plants::VERSION} ruby-#{RUBY_VERSION}" })
end
end
end
36 changes: 36 additions & 0 deletions spec/plants/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
RSpec.describe Plants::Client do
let(:config) { instance_double(Plants::Config) }
let(:client) { Plants::Client.new(config) }

describe '.new' do
subject { client }

it do
expect(subject).to be_instance_of(Plants::Client)
end
end

describe '#config' do
subject { client.config }

it do
expect(subject).to be(config)
end
end

describe '#get' do
context 'resource = plants' do
subject { client.get 'plants' }

before(:each) do
stub_request(:get, "#{Plants::Client::URL}/plants").to_return(status: 200, body: '{}')
end

it do
expect(config).to receive(:token).and_return(nil)
expect(subject).to be_instance_of(HTTP::Response)
expect(a_request(:get, "#{Plants::Client::URL}/plants")).to have_been_made
end
end
end
end

0 comments on commit 9185a0a

Please sign in to comment.