Skip to content

Commit 7e2120e

Browse files
author
Bennett Goble
committed
Initial
0 parents  commit 7e2120e

20 files changed

+383
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.bundle/

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec

Gemfile.lock

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
PATH
2+
remote: .
3+
specs:
4+
cortex-client (0.0.1)
5+
faraday (~> 0.8.9)
6+
faraday_middleware (~> 0.9.0)
7+
8+
GEM
9+
remote: https://rubygems.org/
10+
specs:
11+
diff-lcs (1.2.5)
12+
faraday (0.8.9)
13+
multipart-post (~> 1.2.0)
14+
faraday_middleware (0.9.0)
15+
faraday (>= 0.7.4, < 0.9)
16+
multipart-post (1.2.0)
17+
rake (10.2.2)
18+
rspec (2.14.1)
19+
rspec-core (~> 2.14.0)
20+
rspec-expectations (~> 2.14.0)
21+
rspec-mocks (~> 2.14.0)
22+
rspec-core (2.14.8)
23+
rspec-expectations (2.14.5)
24+
diff-lcs (>= 1.1.3, < 2.0)
25+
rspec-mocks (2.14.6)
26+
27+
PLATFORMS
28+
ruby
29+
30+
DEPENDENCIES
31+
cortex-client!
32+
rake
33+
rspec

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2014 CareerBuilder, LLC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# cortex-client
2+
Ruby client library for [cortex](https://github.com/cb-talent-development/cortex)'s API.
3+
4+
cortex-client does not handle fetching or refreshing an OAuth access_token. For this use a library such as [OAuth](http://oauth.rubyforge.org/).
5+
6+
```ruby
7+
require 'cortex-client'
8+
9+
client = Cortex::Client.new(access_token)
10+
11+
client.posts.query().each do |post|
12+
puts post
13+
end
14+
```
15+
16+
### Supported Endpoints
17+
18+
- *Users* - me, get ,save
19+
- *Posts* - query, get, save, delete
20+
21+
### TODO
22+
- Handle pagination
23+
- Support for search queries
24+
- /media

Rakefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'bundler'
2+
require 'rspec/core/rake_task'
3+
4+
task :default => :test
5+
6+
RSpec::Core::RakeTask.new(:test) do |t|
7+
t.pattern = 'spec/**/*_spec.rb'
8+
end

cortex-client.gemspec

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# encoding: utf-8
2+
require './lib/cortex/version'
3+
4+
Gem::Specification.new do |s|
5+
s.name = 'cortex-client'
6+
s.version = Cortex::VERSION
7+
s.summary = 'Cortex API Client'
8+
s.homepage = 'https://github.com/cb-talent-development/cortex-client'
9+
s.authors = ['Bennett Goble']
10+
s.license = 'MIT'
11+
12+
s.files = `git ls-files`.split($/).reject { |f| f == '.gitignore' }
13+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
14+
s.require_paths = ['lib']
15+
16+
s.add_development_dependency 'rake'
17+
s.add_development_dependency 'rspec'
18+
19+
s.add_dependency 'faraday', '~> 0.8.9'
20+
s.add_dependency 'faraday_middleware', '~> 0.9.0'
21+
end

lib/cortex-client.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'cortex/client'
2+
require 'cortex/version'

lib/cortex/client.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'cortex/connection'
2+
require 'cortex/request'
3+
require 'cortex/resource'
4+
require 'cortex/posts'
5+
require 'cortex/users'
6+
7+
module Cortex
8+
class Client
9+
attr_reader :posts, :users
10+
attr_accessor :access_token, :base_url
11+
12+
include Cortex::Connection
13+
include Cortex::Request
14+
15+
def initialize(access_token, base_url = 'https://cbcortex.com/api/v1')
16+
@access_token = access_token
17+
@base_url = base_url
18+
@posts = Cortex::Posts.new(self)
19+
@users = Cortex::Users.new(self)
20+
end
21+
end
22+
end

lib/cortex/connection.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'faraday'
2+
require 'faraday_middleware'
3+
4+
module Cortex
5+
module Connection
6+
def connection
7+
options = {
8+
:headers => {
9+
:user_agent => "cortex-client (Ruby) - #{Cortex::VERSION}"
10+
},
11+
:url => base_url
12+
}
13+
Faraday.new options do |conn|
14+
conn.request :oauth2, access_token
15+
conn.request :json
16+
conn.response :json, :content_type => /\bjson$/
17+
conn.adapter Faraday.default_adapter
18+
end
19+
end
20+
end
21+
end

lib/cortex/posts.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Cortex
2+
class Posts < Cortex::Resource
3+
def query
4+
client.get('/posts')
5+
end
6+
7+
def get(id)
8+
client.get("/posts/#{id}")
9+
end
10+
11+
def save(post)
12+
client.save('/posts', post)
13+
end
14+
15+
def delete(id)
16+
client.delete("/posts/#{id}")
17+
end
18+
end
19+
end

lib/cortex/request.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'ostruct'
2+
3+
module Cortex
4+
module Request
5+
def get(path, params = {})
6+
response = connection.get do |r|
7+
r.url base_url + path
8+
r.params = params
9+
end
10+
parse_response(response)
11+
end
12+
13+
def post(path, params = {})
14+
response = connection.post do |r|
15+
r.url base_url + path
16+
r.body = params unless params.empty?
17+
end
18+
parse_response(response)
19+
end
20+
21+
def put(path, params = {})
22+
response = connection.put do |r|
23+
r.url base_url + path
24+
r.body = params unless params.empty?
25+
end
26+
parse_response(response)
27+
end
28+
29+
def delete(path)
30+
response = connection.delete do |r|
31+
r.url base_url + path
32+
end
33+
response.status < 300
34+
end
35+
36+
def delete!(path)
37+
response = connection.delete do |r|
38+
r.url base_url + path
39+
end
40+
if response.status < 300
41+
true
42+
else
43+
raise parse_response(response)
44+
end
45+
end
46+
47+
def save(path, model)
48+
model[:id] ? put("#{path}/#{model[:id]}", model) : post(path, model)
49+
end
50+
51+
def parse_response(response)
52+
if response.status < 300 || (response.body.kind_of?(Hash) && response.body['error'])
53+
response.body
54+
else
55+
OpenStruct.new({:error => response.body, :status => response.status, :original => response})
56+
end
57+
end
58+
end
59+
end

lib/cortex/resource.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Cortex
2+
class Resource
3+
def initialize(client)
4+
@client = client
5+
end
6+
7+
protected
8+
attr_accessor :client
9+
end
10+
end

lib/cortex/users.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Cortex
2+
class Users < Cortex::Resource
3+
def me
4+
client.get('/users/me')
5+
end
6+
7+
def get(id)
8+
client.get("/users/#{id}")
9+
end
10+
11+
def save(user)
12+
client.save('/users', user)
13+
end
14+
end
15+
end

lib/cortex/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Cortex
2+
VERSION = '0.0.1'
3+
end

spec/client_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'spec_helper'
2+
3+
describe Cortex::Client do
4+
5+
let(:access_token) { '123' }
6+
let(:base_url) { 'http://localhost:3000' }
7+
let(:client) do
8+
Cortex::Client.new(access_token, base_url)
9+
end
10+
11+
it 'should preserve settings' do
12+
client.access_token.should == access_token
13+
client.base_url.should == base_url
14+
end
15+
end

spec/posts_spec.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'spec_helper'
2+
3+
describe Cortex::Posts do
4+
5+
let(:client) { Cortex::Client.new('123') }
6+
7+
describe :get do
8+
it 'should correctly make the request' do
9+
client.should_receive(:get).with('/posts/1').and_return('response')
10+
client.posts.get(1).should == 'response'
11+
end
12+
end
13+
14+
describe :save do
15+
context 'with an existing post' do
16+
it 'should correctly make the request' do
17+
post = {:id => 1, :title => 'Post'}
18+
client.should_receive(:put).with('/posts/1', post).and_return('response')
19+
client.posts.save(post).should == 'response'
20+
end
21+
end
22+
23+
context 'with a new post' do
24+
it 'should correctly make the request' do
25+
post = {:title => 'Post'}
26+
client.should_receive(:post).with('/posts', post).and_return('response')
27+
client.posts.save(post).should == 'response'
28+
end
29+
end
30+
end
31+
end

spec/request_spec.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'spec_helper'
2+
3+
describe Cortex::Request do
4+
let(:client) { Cortex::Client.new('123') }
5+
6+
context 'with a cortex error response' do
7+
it 'should return the error object' do
8+
body = {'error' => 'Validation error or something'}
9+
response = OpenStruct.new(:status => 422, :body => body)
10+
client.parse_response(response).should == body
11+
end
12+
end
13+
14+
context 'with a non-cortex error response' do
15+
it 'should return a wrapped response' do
16+
body = 'Catastrophic error'
17+
response = OpenStruct.new(:status => 500, :body => body)
18+
parsed = client.parse_response(response)
19+
parsed.error.should == body
20+
parsed.status.should == 500
21+
parsed.original.should == response
22+
end
23+
end
24+
25+
context 'with a successful response' do
26+
it 'should return the parsed body' do
27+
body = {:id => 1, title: 'A post'}
28+
response = OpenStruct.new(:status => 200, :body => body)
29+
client.parse_response(response).should == body
30+
end
31+
end
32+
end

spec/spec_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'ostruct'
2+
require_relative '../lib/cortex-client'

0 commit comments

Comments
 (0)