Skip to content

Commit

Permalink
Set up client and configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
acreilly committed Apr 3, 2020
1 parent ae738b4 commit dacf9ea
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/extend_warranties.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
require "extend_warranties/version"
require 'active_support/all'
require 'faraday'
require 'faraday_middleware'

module ExtendWarranties
class Error < StandardError; end
# Your code goes here...
end
require 'extend_warranties/version'
require 'extend_warranties/response'

require 'extend_warranties/api/base'
require 'extend_warranties/api/products'
require 'extend_warranties/api/plans'
require 'extend_warranties/api/contracts'
require 'extend_warranties/api/offers'

require 'extend_warranties/configuration'
require 'extend_warranties/client'
18 changes: 18 additions & 0 deletions lib/extend_warranties/api/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module ExtendWarranties
module Api
class Base
attr_reader :connection

def initialize(connection)
@connection = connection
end

protected

def handle_response(response)
# Wrap in our own response class
ExtendWarranties::Response.new response
end
end
end
end
6 changes: 6 additions & 0 deletions lib/extend_warranties/api/contracts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ExtendWarranties
module Api
class Contracts < Base
end
end
end
6 changes: 6 additions & 0 deletions lib/extend_warranties/api/offers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ExtendWarranties
module Api
class Offers < Base
end
end
end
6 changes: 6 additions & 0 deletions lib/extend_warranties/api/plans.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ExtendWarranties
module Api
class Plans < Base
end
end
end
6 changes: 6 additions & 0 deletions lib/extend_warranties/api/products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ExtendWarranties
module Api
class Products < Base
end
end
end
23 changes: 23 additions & 0 deletions lib/extend_warranties/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ExtendWarranties
class Client
@namespaces = []
@connection = nil

##
# Defines a method to access class instance.
#
def self.namespace(name)
converted = name.to_s.split('_').map(&:capitalize).join
klass = ExtendWarranties::Api.const_get(converted)
@namespaces << klass
end

namespace :products

namespace :plans

namespace :contracts

namespace :offers
end
end
39 changes: 39 additions & 0 deletions lib/extend_warranties/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module ExtendWarranties
class Configuration
attr_reader :sandbox, :base_url, :env, :access_token, :store_id, :headers

REST_URLS = {
sandbox: 'https://api.helloextend.com',
production: 'https://api.helloextend.com'
}


SANDBOX = :sandbox
PRODUCTION = :production


def initialize(args = {})
validate_args(args)

@env = args[:env].to_sym
@sandbox = @env == SANDBOX
@base_url = REST_URLS[env]

@headers = args[:headers] || {}

@access_token = args[:access_token]
@store_id = args[:store_id]
end

private

def validate_args(args)
raise ArgumentError, 'access token is required' if args[:access_token].blank?
raise ArgumentError, 'store id is required' if args[:store_id].blank?
raise ArgumentError, 'env is required' if args[:env].blank?

return if [SANDBOX, PRODUCTION].include?(args[:env].to_sym)
raise ArgumentError, 'env must be of :sandbox or :production'
end
end
end
8 changes: 8 additions & 0 deletions lib/extend_warranties/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module ExtendWarranties
class Response < Faraday::Response
def initialize(response)
super()
finish(response.env)
end
end
end
9 changes: 9 additions & 0 deletions spec/lib/extend_warranties/api/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'spec_helper'

RSpec.describe ExtendWarranties::Api::Base do
let(:connection) { Faraday.new }
subject { described_class.new connection }

it { is_expected.to respond_to :connection }
its(:connection) { is_expected.to eql connection }
end
52 changes: 52 additions & 0 deletions spec/lib/extend_warranties/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'spec_helper'

RSpec.describe ExtendWarranties::Configuration do
let(:access_token) { 'access_token' }
let(:store_id) { 'store_id' }
let(:env) { :sandbox }

subject do
ExtendWarranties::Configuration.new(
access_token: access_token,
store_id: store_id,
env: env
)
end

it { is_expected.to respond_to :sandbox }
it { is_expected.to respond_to :base_url }
it { is_expected.to respond_to :env }
it { is_expected.to respond_to :headers }

context 'when :access_token is missing' do
let(:access_token) { nil }
it { expect { subject }.to raise_error ArgumentError }
end

context 'when :store_id is missing' do
let(:store_id) { nil }
it { expect { subject }.to raise_error ArgumentError }
end

context 'when :env is missing' do
let(:env) { nil }
it { expect { subject }.to raise_error ArgumentError }
end

context 'when :env is neither :sandbox or :production' do
let(:env) { :invalid }
it { expect { subject }.to raise_error ArgumentError }
end

context 'when NOT in sandbox mode' do
let(:env) { :production }

its(:env) { is_expected.to eql ExtendWarranties::Configuration::PRODUCTION }
its(:base_url) { is_expected.to eql ExtendWarranties::Configuration::REST_URLS[:production] }
end

context 'when in sandbox mode' do
its(:env) { is_expected.to eql ExtendWarranties::Configuration::SANDBOX }
its(:base_url) { is_expected.to eql ExtendWarranties::Configuration::REST_URLS[:sandbox] }
end
end

0 comments on commit dacf9ea

Please sign in to comment.