Skip to content

Commit bfeaefc

Browse files
committed
Use FaradayMiddleware for Hashie parsing, OAuth2. Straighten up requires to be more explicit, use requires properly to distinguish between Faraday::Middleware and FaradayMiddleware, namespace custom Faraday Middleware, remove unnecessary :url_encoded option, update dependencies, write new (currently failing) test for properly encoded special characters in URI fragments
1 parent 927531d commit bfeaefc

File tree

10 files changed

+43
-55
lines changed

10 files changed

+43
-55
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: ruby
22
rvm:
3-
- 2.3.0
3+
- 2.4.0
44
notifications:
55
recipients:
6-
- ContentEnablementProductTeam@careerbuilder.com
6+
- EmployerSiteContentProducts@cb.com

cortex-client.gemspec

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@ Gem::Specification.new do |s|
66
s.version = Cortex::VERSION
77
s.summary = 'Cortex API Client'
88
s.homepage = 'https://github.com/cortex-cms/cortex-client-ruby'
9-
s.authors = ['CB Content Enablement']
10-
s.email = 'ContentEnablementProductTeam@careerbuilder.com'
9+
s.authors = ['CareerBuilder Employer Site & Content Products']
10+
s.email = ['EmployerSiteContentProducts@cb.com']
1111
s.license = 'Apache-2.0'
1212

1313
s.files = `git ls-files`.split($/).reject { |f| f == '.gitignore' }
1414
s.test_files = s.files.grep(%r{^(test|spec|features)/})
1515
s.require_paths = ['lib']
1616

17-
s.add_development_dependency 'rake', '~> 11.1'
18-
s.add_development_dependency 'rspec', '~> 3.4'
19-
s.add_development_dependency 'mocha', '~> 1.1'
17+
s.add_development_dependency 'rake', '~> 12.0'
18+
s.add_development_dependency 'rspec', '~> 3.5'
19+
s.add_development_dependency 'mocha', '~> 1.2'
2020

2121
s.add_dependency 'faraday', '~> 0.9'
2222
s.add_dependency 'faraday_middleware', '~> 0.10'
23-
s.add_dependency 'oauth2', '~> 1.1.0'
2423
s.add_dependency 'cortex-exceptions', '~> 0.0.4'
2524
s.add_dependency 'hashie', '~> 3.4'
2625
end

lib/cortex/client.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
require 'cortex/users'
66
require 'cortex/webpages'
77
require 'cortex/result'
8-
require 'oauth2'
9-
require 'cortex/exceptions'
108

119
module Cortex
1210
class Client
@@ -20,27 +18,17 @@ class Client
2018
include Cortex::Request
2119

2220
def initialize(hasharg)
23-
@base_url = hasharg[:base_url] || 'https://cbcortex.com/api/v1/'
21+
@base_url = hasharg[:base_url] || 'https://cbcortex.com/api/v1'
2422
if hasharg.has_key? :access_token
2523
@access_token = hasharg[:access_token]
2624
else
2725
@key = hasharg[:key]
2826
@secret = hasharg[:secret]
2927
@scopes ||= hasharg[:scopes]
30-
@access_token = get_cc_token
3128
end
3229
@posts = Cortex::Posts.new(self)
3330
@users = Cortex::Users.new(self)
3431
@webpages = Cortex::Webpages.new(self)
3532
end
36-
37-
def get_cc_token
38-
begin
39-
client = OAuth2::Client.new(@key, @secret, site: @base_url)
40-
client.client_credentials.get_token({scope: @scopes})
41-
rescue Faraday::ConnectionFailed
42-
raise Cortex::Exceptions::ConnectionFailed.new(base_url: @base_url)
43-
end
44-
end
4533
end
4634
end

lib/cortex/connection.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'faraday'
22
require 'faraday_middleware'
3-
require 'cortex/faraday_middleware'
3+
require 'cortex/faraday_middleware/response_failures'
44

55
module Cortex
66
module Connection
@@ -17,11 +17,19 @@ def connection
1717
end
1818

1919
Faraday.new options do |conn|
20-
conn.use Cortex::FaradayMiddleware
21-
conn.request :oauth2, access_token.is_a?(OAuth2::AccessToken) ? access_token.token : access_token
20+
# Hello, temporal coupling. Order matters here.
21+
22+
# Request middleware first:
23+
conn.use ::FaradayMiddleware::OAuth2, access_token.is_a?(OAuth2::AccessToken) ? access_token.token : access_token
24+
25+
# Response middleware second:
26+
conn.use ::FaradayMiddleware::Mashify
27+
conn.use Cortex::FaradayMiddleware::ResponseFailures
28+
2229
conn.request :json
23-
conn.request :url_encoded
2430
conn.response :json, :content_type => /\bjson$/
31+
32+
# Adapter always last:
2533
conn.adapter Faraday.default_adapter
2634
end
2735
end

lib/cortex/faraday_middleware.rb

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

lib/cortex/request.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'ostruct'
2-
31
module Cortex
42
module Request
53
def get(path, params = {})

lib/cortex/resource.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ def initialize(client)
77
protected
88
attr_accessor :client
99
end
10-
end
10+
end

lib/cortex/result.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
require 'hashie'
21
module Cortex
32
class Result
43
attr_reader :raw_headers, :contents, :total_items, :page, :per_page, :errors, :range_start, :range_end, :range, :status, :total_pages, :next_page, :prev_page
54

65
def initialize(body, headers, status)
7-
@contents = parse(body)
6+
@contents = body
87
@raw_headers = headers
98
@status = status
109
parse_headers(headers)
@@ -32,17 +31,6 @@ def parse_headers(headers)
3231
end
3332
end
3433

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
43-
end
44-
end
45-
4634
def find_errors
4735
if is_error?
4836
if @contents.is_a?(Hash)

spec/posts_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
RSpec.describe Cortex::Posts do
44

5+
# TODO: Stub out Faraday somewhere. See: https://github.com/lostisland/faraday#using-faraday-for-testing
56
let(:client) { Cortex::Client.new(access_token: '123') }
67

78
describe :get do
@@ -16,6 +17,10 @@
1617
client.expects(:get).with('/posts/feed/1').returns('response')
1718
expect(client.posts.get_published(1)).to eq('response')
1819
end
20+
21+
it 'should work with special characters' do
22+
expect { client.posts.get_published('1 post') }.to_not raise_error(URI::InvalidURIError)
23+
end
1924
end
2025

2126
describe :feed do

0 commit comments

Comments
 (0)