footrest 0.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,10 +4,12 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'footrest/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.authors = ["Nathan Mills"]
8
- gem.email = ["nathanm@instructure.com"]
7
+ gem.authors = ["Duane Johnson", "Nathan Mills"]
8
+ gem.email = ["duane@instructure.com", "nathanm@instructure.com"]
9
9
  gem.description = %q{Ruby interface for restful APIs}
10
10
  gem.summary = %q{REST APIs}
11
+ gem.homepage = 'https://github.com/instructure/footrest'
12
+ gem.license = 'MIT'
11
13
 
12
14
  gem.files = %w[Rakefile footrest.gemspec]
13
15
  gem.files += Dir.glob("lib/**/*.rb")
@@ -27,4 +29,4 @@ Gem::Specification.new do |gem|
27
29
  gem.add_dependency "faraday", "~> 0.8.8"
28
30
  gem.add_dependency "faraday_middleware", "~> 0.9.0"
29
31
  gem.add_dependency "activesupport", ">= 3.0.0"
30
- end
32
+ end
@@ -1,4 +1,5 @@
1
1
  require 'faraday'
2
+ require 'footrest/http_error'
2
3
 
3
4
  module Faraday
4
5
  class Response::RaiseFootrestHttpError < Response::Middleware
@@ -3,14 +3,14 @@ require 'footrest/request'
3
3
 
4
4
  module Footrest
5
5
  class Client
6
- attr_reader :options
7
-
8
6
  include Footrest::Connection
9
7
  include Footrest::Request
10
8
 
9
+ attr_reader :config
10
+
11
11
  def initialize(options={}, &block)
12
- @options = Footrest.config.merge(options)
13
- @faraday_option_block = block
12
+ @config = Footrest.config.merge(options)
13
+ set_connection(@config, &block)
14
14
  end
15
15
  end
16
16
  end
@@ -4,10 +4,16 @@ require 'active_support/configurable'
4
4
  module Footrest
5
5
  include ActiveSupport::Configurable
6
6
  config_accessor :token
7
- config_accessor :domain
8
- config_accessor :path_prefix
7
+ config_accessor :prefix
8
+ config_accessor :logging
9
9
 
10
10
  def configure
11
11
  yield self
12
12
  end
13
- end
13
+ end
14
+
15
+ Footrest.configure do |config|
16
+ config.token = nil
17
+ config.prefix = nil
18
+ config.logging = false
19
+ end
@@ -5,20 +5,21 @@ require 'faraday/response/raise_footrest_http_error'
5
5
  module Footrest
6
6
  module Connection
7
7
 
8
- def connection
9
- @options ||= {}
10
- @connection ||= Faraday.new(url: @options[:domain]) do |faraday|
8
+ attr_reader :connection
9
+
10
+ def set_connection(config, &block)
11
+ @connection = Faraday.new(url: config[:prefix]) do |faraday|
11
12
  faraday.request :multipart
12
13
  faraday.request :url_encoded
13
- faraday.response :logger if @options[:logging]
14
+ faraday.response :logger if config[:logging]
14
15
  faraday.adapter Faraday.default_adapter
15
16
  faraday.use FaradayMiddleware::FollowRedirects
16
17
  faraday.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
17
18
  faraday.use Faraday::Response::RaiseFootrestHttpError
18
19
  faraday.headers[:accept] = "application/json"
19
- faraday.headers[:authorization] = "Bearer #{@options[:token]}"
20
+ faraday.headers[:authorization] = "Bearer #{config[:token]}" if config[:token]
20
21
  faraday.headers[:user_agent] = "Footrest"
21
- @faraday_option_block.call(faraday) if @faraday_option_block
22
+ yield faraday if block_given?
22
23
  end
23
24
  end
24
25
 
@@ -1,6 +1,20 @@
1
1
  module Footrest
2
2
  module Request
3
3
 
4
+ def config
5
+ raise "Config should be overridden"
6
+ end
7
+
8
+ def join(*parts)
9
+ joined = parts.map{ |p| p.gsub(%r{^/|/$}, '') }.join('/')
10
+ joined = '/' + joined if parts.first[0] == '/'
11
+ joined
12
+ end
13
+
14
+ def fullpath(path)
15
+ config[:prefix] ? join(config[:prefix], path) : path
16
+ end
17
+
4
18
  def delete(path, options={})
5
19
  request_with_params_in_url(:delete, path, options)
6
20
  end
@@ -18,20 +32,20 @@ module Footrest
18
32
  end
19
33
 
20
34
  def request_with_params_in_url(method, path, options)
21
- request(method, options) do |r|
22
- r.url(File.join(path_prefix, path), options)
35
+ request(method) do |r|
36
+ r.url(fullpath(path), options)
23
37
  end
24
38
  end
25
39
 
26
40
  def request_with_params_in_body(method, path, options)
27
- request(method, options) do |r|
28
- r.path = File.join(path_prefix, path)
41
+ request(method) do |r|
42
+ r.path = fullpath(path)
29
43
  r.body = options unless options.empty?
30
44
  end
31
45
  end
32
46
 
33
47
  # Generic request
34
- def request(method, options, &block)
48
+ def request(method, &block)
35
49
  connection.send(method, &block).body
36
50
  end
37
51
 
@@ -1,3 +1,3 @@
1
1
  module Footrest
2
- VERSION = '0.1' unless defined?(Footrest::VERSION)
2
+ VERSION = '0.1.2' unless defined?(Footrest::VERSION)
3
3
  end
@@ -1,16 +1,43 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Footrest::Client do
4
-
5
4
  it "sets the domain" do
6
- client = Footrest::Client.new(domain:"http://domain.com")
7
- expect(client.options[:domain]).to eq("http://domain.com")
8
-
5
+ client = Footrest::Client.new(prefix: "http://domain.test")
6
+ expect(client.config.prefix).to eq("http://domain.test")
9
7
  end
10
8
 
11
9
  it "sets the authtoken" do
12
- client = Footrest::Client.new(token:"test_token")
13
- expect(client.options[:token]).to eq("test_token")
10
+ client = Footrest::Client.new(token: "test_token")
11
+ expect(client.config.token).to eq("test_token")
14
12
  end
15
13
 
14
+ context "Request" do
15
+ let(:client) { Footrest::Client.new }
16
+
17
+ it "gets" do
18
+ stub_request(:get, "http://domain.test/page?p=1").
19
+ to_return(:status => 200, :body => "", :headers => {})
20
+ client.get('http://domain.test/page', :p => 1)
21
+ end
22
+
23
+ it "deletes" do
24
+ stub_request(:get, "http://domain.test/page?auth=xyz").
25
+ to_return(:status => 200, :body => "", :headers => {})
26
+ client.get('http://domain.test/page', :auth => 'xyz')
27
+ end
28
+
29
+ it "posts" do
30
+ stub_request(:post, "http://domain.test/new_page").
31
+ with(:body => {"password"=>"xyz", "username"=>"abc"}).
32
+ to_return(:status => 200, :body => "", :headers => {})
33
+ client.post('http://domain.test/new_page', :username => 'abc', :password => 'xyz')
34
+ end
35
+
36
+ it "puts" do
37
+ stub_request(:put, "http://domain.test/update_page").
38
+ with(:body => {"password"=>"zzz", "username"=>"aaa"}).
39
+ to_return(:status => 200, :body => "", :headers => {})
40
+ client.put('http://domain.test/update_page', :username => 'aaa', :password => 'zzz')
41
+ end
42
+ end
16
43
  end
@@ -2,7 +2,7 @@ require 'helper'
2
2
 
3
3
  describe Footrest::HttpError do
4
4
  before do
5
- @client = Footrest::Client.new(domain: "http://domain.com", token: "test_token")
5
+ @client = Footrest::Client.new(prefix: "http://domain.com", token: "test_token")
6
6
  end
7
7
 
8
8
  it "raises a bad request error" do
@@ -1,38 +1,25 @@
1
1
  require 'helper'
2
- require 'ostruct'
3
2
 
4
- class RequestHarness
5
- include Footrest::Connection
6
- include Footrest::Request
7
- end
3
+ class RequestHarness; include Footrest::Request; end
8
4
 
9
5
  describe Footrest::Request do
10
-
11
6
  let(:request) { RequestHarness.new }
7
+ context "join" do
8
+ it "retains initial slash" do
9
+ expect(request.join('/test', 'path')).to eq('/test/path')
10
+ end
12
11
 
13
- it "gets" do
14
- stub_request(:get, "http://domain.test/page?p=1").
15
- to_return(:status => 200, :body => "", :headers => {})
16
- request.get('http://domain.test/page', :p => 1)
17
- end
12
+ it "combines multiple segments" do
13
+ expect(request.join('test', 'path', 'parts')).to eq('test/path/parts')
14
+ end
18
15
 
19
- it "deletes" do
20
- stub_request(:get, "http://domain.test/page?auth=xyz").
21
- to_return(:status => 200, :body => "", :headers => {})
22
- request.get('http://domain.test/page', :auth => 'xyz')
23
- end
24
-
25
- it "posts" do
26
- stub_request(:post, "http://domain.test/new_page").
27
- with(:body => {"password"=>"xyz", "username"=>"abc"}).
28
- to_return(:status => 200, :body => "", :headers => {})
29
- request.post('http://domain.test/new_page', :username => 'abc', :password => 'xyz')
30
- end
16
+ it "respects http://" do
17
+ expect(request.join('http://', 'path')).to eq('http://path')
18
+ end
31
19
 
32
- it "puts" do
33
- stub_request(:put, "http://domain.test/update_page").
34
- with(:body => {"password"=>"zzz", "username"=>"aaa"}).
35
- to_return(:status => 200, :body => "", :headers => {})
36
- request.put('http://domain.test/update_page', :username => 'aaa', :password => 'zzz')
20
+ it "keeps slashes within strings" do
21
+ expect(request.join('http://', 'domain', '/path/to/something')).
22
+ to eq('http://domain/path/to/something')
23
+ end
37
24
  end
38
25
  end
@@ -17,19 +17,19 @@ def fixture(file)
17
17
  end
18
18
 
19
19
  def stub_get(client, url)
20
- stub_request(:get, "#{client.domain}#{url}")
20
+ stub_request(:get, File.join(client.domain, url))
21
21
  end
22
22
 
23
23
  def stub_post(client, url)
24
- stub_request(:post, "#{client.domain}#{url}")
24
+ stub_request(:post, File.join(client.domain, url))
25
25
  end
26
26
 
27
27
  def stub_put(client, url)
28
- stub_request(:put, "#{client.domain}#{url}")
28
+ stub_request(:put, File.join(client.domain, url))
29
29
  end
30
30
 
31
31
  def stub_delete(client, url)
32
- stub_request(:delete, "#{client.domain}#{url}")
32
+ stub_request(:delete, File.join(client.domain, url))
33
33
  end
34
34
 
35
35
  def json_response(file)
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: footrest
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
+ - Duane Johnson
8
9
  - Nathan Mills
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-07-31 00:00:00.000000000 Z
13
+ date: 2013-08-07 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: rake
@@ -157,6 +158,7 @@ dependencies:
157
158
  version: 3.0.0
158
159
  description: Ruby interface for restful APIs
159
160
  email:
161
+ - duane@instructure.com
160
162
  - nathanm@instructure.com
161
163
  executables: []
162
164
  extensions: []
@@ -176,8 +178,9 @@ files:
176
178
  - spec/footrest/http_error_spec.rb
177
179
  - spec/footrest/request_spec.rb
178
180
  - spec/helper.rb
179
- homepage:
180
- licenses: []
181
+ homepage: https://github.com/instructure/footrest
182
+ licenses:
183
+ - MIT
181
184
  post_install_message:
182
185
  rdoc_options: []
183
186
  require_paths: