-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module ExtendWarranties | ||
module Api | ||
class Contracts < Base | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module ExtendWarranties | ||
module Api | ||
class Offers < Base | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module ExtendWarranties | ||
module Api | ||
class Plans < Base | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module ExtendWarranties | ||
module Api | ||
class Products < Base | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |