Skip to content

Commit 3591b2a

Browse files
committed
Merge branch 'develop'
2 parents 454f4db + 561f816 commit 3591b2a

File tree

12 files changed

+67
-69
lines changed

12 files changed

+67
-69
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.bundle/
22
.idea/
33
.ruby-version
4+
Gemfile.lock

Gemfile.lock

Lines changed: 0 additions & 52 deletions
This file was deleted.

cortex-client-0.4.0.gem

9.5 KB
Binary file not shown.

cortex-client.gemspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
66
s.version = Cortex::VERSION
77
s.summary = 'Cortex API Client'
88
s.homepage = 'https://github.com/cb-talent-development/cortex-client'
9-
s.authors = ['Bennett Goble']
9+
s.authors = ['Bennett Goble', 'Colin Ewen']
1010
s.license = 'MIT'
1111

1212
s.files = `git ls-files`.split($/).reject { |f| f == '.gitignore' }
@@ -20,4 +20,6 @@ Gem::Specification.new do |s|
2020
s.add_dependency 'faraday', '~> 0.9'
2121
s.add_dependency 'faraday_middleware', '~> 0.9.0'
2222
s.add_dependency 'oauth2', '~>0.9'
23+
s.add_dependency 'cortex-exceptions', '~> 0.0.4'
24+
s.add_dependency 'hashie', '~> 3.4.0'
2325
end

lib/cortex-client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
require 'cortex/client'
22
require 'cortex/version'
3+
require 'cortex/client_exceptions'

lib/cortex/client.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'cortex/users'
66
require 'cortex/result'
77
require 'oauth2'
8+
require 'cortex/exceptions'
89

910
module Cortex
1011
class Client
@@ -30,8 +31,12 @@ def initialize(hasharg)
3031
end
3132

3233
def get_cc_token
33-
client = OAuth2::Client.new(@key, @secret, site: @base_url)
34-
client.client_credentials.get_token
34+
begin
35+
client = OAuth2::Client.new(@key, @secret, site: @base_url)
36+
client.client_credentials.get_token
37+
rescue Faraday::ConnectionFailed
38+
raise Cortex::Exceptions::ConnectionFailed.new(base_url: @base_url)
39+
end
3540
end
3641
end
3742
end

lib/cortex/client_exceptions.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Cortex
2+
module ClientExceptions
3+
class ErrorFromResult < StandardError
4+
attr_accessor :status, :contents, :errors
5+
def initialize(arg)
6+
throw ArgumentError.new(msg: "Expected: Cortex::Result, Received: #{arg.class}") unless arg.is_a?(Cortex::Result)
7+
@status = arg.status
8+
@errors = arg.errors
9+
@contents = arg.contents
10+
end
11+
end
12+
end
13+
end

lib/cortex/connection.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'faraday'
22
require 'faraday_middleware'
3+
require 'cortex/faraday_middleware'
34

45
module Cortex
56
module Connection
@@ -16,6 +17,7 @@ def connection
1617
end
1718

1819
Faraday.new options do |conn|
20+
conn.use Cortex::FaradayMiddleware
1921
conn.request :oauth2, access_token.is_a?(OAuth2::AccessToken) ? access_token.token : access_token
2022
conn.request :json
2123
conn.response :json, :content_type => /\bjson$/

lib/cortex/faraday_middleware.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'faraday'
2+
require 'faraday_middleware'
3+
4+
module Cortex
5+
class FaradayMiddleware < Faraday::Middleware
6+
def call(env)
7+
begin
8+
@app.call(env)
9+
rescue Faraday::ConnectionFailed
10+
raise Cortex::Exceptions::ConnectionFailed.new(base_url: env[:url])
11+
end
12+
end
13+
end
14+
end

lib/cortex/result.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
require 'hashie'
12
module Cortex
23
class Result
34
attr_reader :raw_headers, :contents, :total_items, :page, :per_page, :errors, :range_start, :range_end, :range, :status
45

56
def initialize(body, headers, status)
6-
@contents = body
7+
@contents = parse(body)
78
@raw_headers = headers
89
@status = status
910
@total_items = headers['x-total-items'] unless headers['x-total-items'].nil?
@@ -18,16 +19,27 @@ def is_error?
1819
private
1920

2021
def parse_headers(headers)
21-
if headers['x-total-items']
22-
@count = headers['x-total-items']
22+
if headers['X-Total']
23+
@count = headers['X-Total'].to_i
2324
end
24-
if headers['content-range']
25-
matches = headers['content-range'].match(/^(\w+) (\d+)\-(\d+):(\d+)\/\d+$/i)
26-
@per_page = matches[4].to_i
27-
@range_start = matches[2].to_i
28-
@range_end = matches[3].to_i
25+
if headers['X-Total']
26+
@page = headers['X-Page'].to_i
27+
@per_page = headers['X-Per-Page'].to_i
28+
@range_start = (@page-1) * @per_page
29+
@range_end = @per_page * @page - 1
2930
@range = "#{@range_start}-#{@range_end}"
30-
@page = (@range_end / @per_page) + 1
31+
32+
end
33+
end
34+
35+
def parse(body)
36+
case body
37+
when Hash
38+
::Hashie::Mash.new(body)
39+
when Array
40+
body.map { |item| parse(item) }
41+
else
42+
body
3143
end
3244
end
3345

@@ -47,4 +59,4 @@ def find_errors
4759
end
4860
end
4961
end
50-
end
62+
end

0 commit comments

Comments
 (0)