-
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
24 changed files
with
593 additions
and
176 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,3 +1,5 @@ | ||
--color | ||
--fail-fast | ||
--order random | ||
-r rspec/instafail | ||
-f RSpec::Instafail |
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
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
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,61 +1,145 @@ | ||
require 'sawyer' | ||
|
||
# require 'revily/client/authentication' | ||
# require 'revily/client/config' | ||
# require 'revily/client/default' | ||
# require 'revily/client/resources' | ||
# require 'revily/client/version' | ||
|
||
module Revily | ||
module Client | ||
class Client | ||
autoload :Authentication, 'revily/client/authentication' | ||
autoload :Config, 'revily/client/config' | ||
autoload :Default, 'revily/client/default' | ||
autoload :Resources, 'revily/client/resources' | ||
|
||
class << self | ||
include Revily::Client::Config | ||
end | ||
|
||
include Revily::Client::Authentication | ||
include Revily::Client::Config | ||
|
||
include Revily::Client::Resources::Hooks | ||
include Revily::Client::Resources::Incidents | ||
include Revily::Client::Resources::Integration | ||
include Revily::Client::Resources::Policies | ||
include Revily::Client::Resources::PolicyRules | ||
include Revily::Client::Resources::ScheduleLayers | ||
include Revily::Client::Resources::Schedules | ||
include Revily::Client::Resources::Services | ||
include Revily::Client::Resources::Users | ||
|
||
CONVENIENCE_HEADERS = Set.new [:accept] | ||
|
||
def initialize(options={}) | ||
Revily::Client::Config.keys.each do |key| | ||
instance_variable_set(:"@#{key}", options[key] || Revily::Client.instance_variable_get(:"@#{key}")) | ||
end | ||
end | ||
|
||
|
||
def get(url, options={}) | ||
request :get, url, parse_query_and_convenience_headers(options) | ||
end | ||
|
||
def post(url, options={}) | ||
request :post, url, options | ||
end | ||
|
||
def put(url, options={}) | ||
request :put, url, options | ||
end | ||
|
||
def patch(url, options={}) | ||
request :patch, url, options | ||
end | ||
|
||
def delete(url, options={}) | ||
request :head, url, parse_query_and_convenience_headers(options) | ||
end | ||
|
||
def paginate(url, options={}) | ||
opts = parse_query_and_convenience_headers(options.dup) | ||
if @auto_paginate || @per_page | ||
opts[:query][:per_page] ||= @per_page || (@auto_paginate ? 100 : nil) | ||
end | ||
|
||
data = request(:get, url, opts) | ||
|
||
if @auto_paginate && data.is_a?(Array) | ||
while @last_response.rels[:next] && rate_limit.remaining > 0 | ||
@last_response = @last_response.rels[:next].get | ||
data.concat(@last_response.data) if @last_response.data.is_a?(Array) | ||
end | ||
end | ||
|
||
data | ||
end | ||
|
||
def agent | ||
@agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http| | ||
http.headers[:accept] = default_content_type | ||
http.headers[:user_agent] = user_agent | ||
if token_authenticated? | ||
http.authorization 'token', @auth_token | ||
end | ||
end | ||
end | ||
|
||
def root | ||
agent.start.data | ||
end | ||
|
||
def last_response | ||
@last_response | ||
end | ||
|
||
private | ||
|
||
def request(method, path, options) | ||
if accept = options.delete(:accept) | ||
options[:headers] ||= {} | ||
options[:headers][:accept] = accept | ||
end | ||
|
||
@last_response = response = agent.call(method, URI.encode(path), options) | ||
response.data | ||
end | ||
|
||
def boolean_from_response(method, path, options={}) | ||
request(:method, path, options) | ||
@last_response.status == 204 | ||
rescue Revily::Client::NotFound | ||
false | ||
end | ||
|
||
def sawyer_options | ||
opts = { | ||
:links_parser => Sawyer::LinkParsers::Hal.new | ||
} | ||
conn_opts = @connection_options | ||
conn_opts[:builder] = @middleware if @middleware | ||
opts[:faraday] = Faraday.new(conn_opts) | ||
|
||
opts | ||
end | ||
|
||
def parse_query_and_convenience_headers(options) | ||
headers = options.fetch(:headers, {}) | ||
CONVENIENCE_HEADERS.each do |h| | ||
if header = options.delete(h) | ||
headers[h] = header | ||
end | ||
end | ||
query = options.delete(:query) | ||
opts = {:query => options} | ||
opts[:query].merge!(query) if query && query.is_a?(Hash) | ||
opts[:headers] = headers unless headers.empty? | ||
|
||
opts | ||
end | ||
end | ||
end | ||
# require 'revily/authentication' | ||
# require 'revily/connection' | ||
# require 'revily/request' | ||
|
||
# require 'revily/clients/incidents' | ||
# require 'revily/clients/policies' | ||
# require 'revily/clients/policy_rules' | ||
# require 'revily/clients/schedule_layers' | ||
# require 'revily/clients/schedules' | ||
# require 'revily/clients/services' | ||
# require 'revily/clients/users' | ||
|
||
# module Revily | ||
# class Client | ||
# attr_accessor(*Config::VALID_OPTIONS_KEYS) | ||
|
||
# def initialize(options={}) | ||
# options = Revily.options.merge(options) | ||
# Config::VALID_OPTIONS_KEYS.each do |key| | ||
# send("#{key}=", options[key]) | ||
# end | ||
# end | ||
|
||
# include Revily::Authentication | ||
# include Revily::Connection | ||
# include Revily::Request | ||
|
||
# include Revily::Client::Incidents | ||
# include Revily::Client::Integration | ||
# include Revily::Client::Policies | ||
# include Revily::Client::PolicyRules | ||
# include Revily::Client::ScheduleLayers | ||
# include Revily::Client::Schedules | ||
# include Revily::Client::Services | ||
# include Revily::Client::Users | ||
# include Revily::Client::Webhooks | ||
# end | ||
# end | ||
|
||
# extend Config | ||
|
||
# class << self | ||
|
||
# def new(options={}) | ||
# Revily::Client.new(options) | ||
# end | ||
|
||
# def method_missing(method, *args, &block) | ||
# return super unless new.respond_to?(method) | ||
# new.send(method, *args, &block) | ||
# end | ||
|
||
# def respond_to?(method, include_private=false) | ||
# new.respond_to?(method, include_private) || super(method, include_private) | ||
# end | ||
# end | ||
|
||
Revily::Client.setup |
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 Revily::Client::Authentication | ||
|
||
def token_authenticated? | ||
!!@auth_token | ||
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 |
---|---|---|
@@ -1,52 +1,46 @@ | ||
require 'faraday' | ||
require 'revily/version' | ||
|
||
module Revily | ||
module Config | ||
VALID_OPTION_KEYS = [ | ||
:adapter, | ||
:api_version, | ||
:api_endpoint, | ||
:auth_token, | ||
:faraday_config_block, | ||
:user_agent | ||
] | ||
|
||
DEFAULT_ADAPTER = Faraday.default_adapter | ||
DEFAULT_API_VERSION = 1 | ||
DEFAULT_API_ENDPOINT = ENV['REVEILLE_API_ENDPOINT'] || 'https://api.revily.io/' | ||
DEFAULT_USER_AGENT = "Revily Ruby Client v#{Revily::VERSION}" | ||
|
||
attr_accessor(*VALID_OPTION_KEYS) | ||
|
||
def self.extended(base) | ||
base.reset | ||
end | ||
|
||
def configure | ||
yield self | ||
require 'revily/client/version' | ||
|
||
module Revily::Client::Config | ||
|
||
attr_accessor :api_endpoint, :auto_paginate, :connection_options, :default_content_type, | ||
:middleware, :per_page, :user_agent | ||
attr_writer :auth_token | ||
|
||
class << self | ||
def keys | ||
@keys ||= [ | ||
:api_endpoint, | ||
:auth_token, | ||
:connection_options, | ||
:default_content_type, | ||
:middleware, | ||
:per_page, | ||
:user_agent | ||
] | ||
end | ||
end | ||
|
||
def options | ||
VALID_OPTION_KEYS.inject({}) { |opts, key| opts.merge!(key => send(key) } | ||
end | ||
def configure | ||
yield self | ||
end | ||
|
||
def api_endpoint=(value) | ||
@api_endpoint = File.join(value, "") | ||
def reset! | ||
Revily::Client::Config.keys.each do |key| | ||
instance_variable_set(:"@#{key}", Revily::Client::Default.options[key]) | ||
end | ||
self | ||
end | ||
alias_method :setup, :reset! | ||
|
||
def faraday_config(&block) | ||
@faraday_config_block = block | ||
end | ||
def api_endpoint | ||
File.join(@api_endpoint, '') | ||
end | ||
|
||
def reset | ||
self.adapter = DEFAULT_ADAPTER | ||
self.api_version = DEFAULT_API_VERSION | ||
self.api_endpoint = DEFAULT_API_ENDPOINT | ||
self.user_agent = DEFAULT_USER_AGENT | ||
self.auth_token = nil | ||
end | ||
private | ||
|
||
|
||
def options | ||
Hash[Revily::Client::Config.keys.map{|key|[key, instance_variable_get(:"@#{key}")]}] | ||
# Revily::Client::Config.inject({}) { |o,k| o.merge!(k => instance_variable_get(:"@#{key}")) } | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.