Skip to content

Commit

Permalink
resources
Browse files Browse the repository at this point in the history
  • Loading branch information
danryan committed Aug 1, 2013
1 parent 430d048 commit 996683a
Show file tree
Hide file tree
Showing 24 changed files with 593 additions and 176 deletions.
2 changes: 2 additions & 0 deletions .rspec
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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ end
group :test do
gem 'json_spec', '~> 1.1'
gem 'rspec', '~> 2.14'
gem 'rspec-instafail', '>= 0.2.4', require: false
gem 'webmock', '~> 1.13'
gem 'vcr', '~> 2.5'
# gem 'revily-api', path:
end

gemspec
22 changes: 20 additions & 2 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
guard 'spork', rspec_env: { 'RAILS_ENV' => 'test' }, rspec_port: 19004, aggressive_kill: false do
env = {
'RAILS_ENV' => 'test',
'RACK_ENV' => 'test',
'REVILY_API_ENDPOINT' => 'http://localhost:9001',
'REVILY_AUTH_TOKEN' => 'dGpYyvbApYxXGAvPekQjt'
}

guard(:spork,
rspec_env: env,
rspec_port: 19004,
aggressive_kill: false
) do
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
end

guard :rspec, cli: "--color --drb --drb-port=19004 --tty -f doc --profile", bundler: false, all_after_pass: false, all_on_start: false, keep_failed: false do
guard(:rspec,
cli: "--color --drb --drb-port=19004 --tty -r rspec/instafail -f RSpec::Instafail --profile",
env: env,
bundler: false,
all_after_pass: false,
all_on_start: false,
keep_failed: false
) do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
Expand Down
198 changes: 141 additions & 57 deletions lib/revily/client.rb
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
8 changes: 8 additions & 0 deletions lib/revily/client/authentication.rb
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
80 changes: 37 additions & 43 deletions lib/revily/client/config.rb
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
26 changes: 0 additions & 26 deletions lib/revily/client/connection.rb

This file was deleted.

Loading

0 comments on commit 996683a

Please sign in to comment.