Skip to content

Commit

Permalink
initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
graemej committed Feb 16, 2016
0 parents commit 1da5c47
Show file tree
Hide file tree
Showing 19 changed files with 593 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
41 changes: 41 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
inherit_from: .rubocop_todo.yml

Metrics/LineLength:
Max: 120

Metrics/MethodLength:
Max: 25

Metrics/AbcSize:
Max: 31

Metrics/CyclomaticComplexity:
Max: 20

Metrics/PerceivedComplexity:
Max: 10

Style/GuardClause:
Enabled: false

Style/SignalException:
Enabled: false

Style/Documentation:
Enabled: false

Lint/AssignmentInCondition:
Enabled: false

Style/PerlBackrefs:
Enabled: false

Style/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Style/TrailingCommaInLiteral:
EnforcedStyleForMultiline: comma

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma

Empty file added .rubocop_todo.yml
Empty file.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sudo: false
before_install: gem install bundler
rvm:
- '2.1'
- '2.2'

13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
source 'https://rubygems.org'

gem 'rake'
gem 'byebug'
gem 'rubocop'
gem 'yard'

group :test do
gem 'rspec', '~> 3.2'
gem 'vcr', '~> 2.9', github: 'vcr/vcr', branch: 'master', ref: '480304be6d73803e6c4a0eb21a4ab4091da558d8'
end

gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 graemej

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# oktakit

Ruby toolkit for the [Okta API](http://developer.okta.com/docs/api/getting_started/design_principles.html).

[![Build Status](https://secure.travis-ci.org/Shopify/oktakit.png)](http://travis-ci.org/Shopify/oktakit)
[![Gem Version](https://badge.fury.io/rb/oktakit.png)](http://badge.fury.io/rb/oktakit)

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'oktakit'
```

And then execute:

$ bundle

## Usage

`Oktakit` follow the same patterns as [`Octokit`](https://github.com/octokit/octokit.rb), if you are familiar with it you should feel right at home.

```ruby
client = Oktakit.new(token: 't0k3n')
organization = client.organization('my-great-org')
agents = organization.rels[:agents].get.data
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

1. Fork it ( https://github.com/shopify/oktakit/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
19 changes: 19 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'bundler/gem_tasks'

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

require 'rubocop/rake_task'
RuboCop::RakeTask.new

task test: :spec
task default: [:spec, :rubocop]

namespace :doc do
require 'yard'
YARD::Rake::YardocTask.new do |task|
task.files = %w(LICENSE.md lib/**/*.rb)
task.options = %w(--output-dir doc/yard --markup markdown)
end
task default: :yard
end
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'oktakit'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require 'irb'
IRB.start
7 changes: 7 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

bundle install

# Do any other automated setup that you need to do here
8 changes: 8 additions & 0 deletions lib/oktakit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'oktakit/version'
require 'oktakit/client'

module Oktakit
def self.new(*args)
Client.new(*args)
end
end
136 changes: 136 additions & 0 deletions lib/oktakit/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
require 'sawyer'
require 'oktakit/response/raise_error'

module Oktakit
class Client
# Header keys that can be passed in options hash to {#get},{#head}
CONVENIENCE_HEADERS = Set.new([:accept, :content_type])

# In Faraday 0.9, Faraday::Builder was renamed to Faraday::RackBuilder
RACK_BUILDER_CLASS = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder

# Default Faraday middleware stack
MIDDLEWARE = RACK_BUILDER_CLASS.new do |builder|
builder.use Oktakit::Response::RaiseError
builder.adapter Faraday.default_adapter
end

def initialize(token)
@token = token
@organization = organization
end

# Make a HTTP GET request
#
# @param url [String] The path, relative to {#api_endpoint}
# @param options [Hash] Query and header params for request
# @return [Sawyer::Resource]
def get(url, options = {})
request :get, url, parse_query_and_convenience_headers(options)
end

# Make a HTTP POST request
#
# @param url [String] The path, relative to {#api_endpoint}
# @param options [Hash] Body and header params for request
# @return [Sawyer::Resource]
def post(url, options = {})
request :post, url, options
end

# Make a HTTP PUT request
#
# @param url [String] The path, relative to {#api_endpoint}
# @param options [Hash] Body and header params for request
# @return [Sawyer::Resource]
def put(url, options = {})
request :put, url, options
end

# Make a HTTP PATCH request
#
# @param url [String] The path, relative to {#api_endpoint}
# @param options [Hash] Body and header params for request
# @return [Sawyer::Resource]
def patch(url, options = {})
request :patch, url, options
end

# Make a HTTP DELETE request
#
# @param url [String] The path, relative to {#api_endpoint}
# @param options [Hash] Query and header params for request
# @return [Sawyer::Resource]
def delete(url, options = {})
request :delete, url, options
end

# Make a HTTP HEAD request
#
# @param url [String] The path, relative to {#api_endpoint}
# @param options [Hash] Query and header params for request
# @return [Sawyer::Resource]
def head(url, options = {})
request :head, url, parse_query_and_convenience_headers(options)
end

attr_reader :last_response

# Fetch the root resource for the API
#
# @return [Sawyer::Resource]
def root
get('/')
end

private

def request(method, path, data, options = {})
if data.is_a?(Hash)
options[:query] = data.delete(:query) || {}
options[:headers] = data.delete(:headers) || {}
if accept = data.delete(:accept)
options[:headers][:accept] = accept
end
end

@last_response = response = sawyer_agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
response.data
end

def sawyer_agent
@agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
http.headers[:accept] = 'application/json'
http.headers[:content_type] = 'application/json'
http.headers[:user_agent] = "Oktakit v#{Oktakit::VERSION}"
http.authorization 'SSWS ', @token
end
end

def sawyer_options
{
links_parser: Sawyer::LinkParsers::Simple.new,
faraday: Faraday.new(builder: MIDDLEWARE),
}
end

def api_endpoint
"https://#{organization}.okta.com/api/v1/"
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
Loading

0 comments on commit 1da5c47

Please sign in to comment.