faraday 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.1.0
data/faraday.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{faraday}
8
- s.version = "0.0.2"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["rick"]
12
- s.date = %q{2009-12-19}
12
+ s.date = %q{2010-01-02}
13
13
  s.description = %q{HTTP/REST API client library with pluggable components}
14
14
  s.email = %q{technoweenie@gmail.com}
15
15
  s.extra_rdoc_files = [
data/lib/faraday.rb CHANGED
@@ -1,21 +1,34 @@
1
1
  module Faraday
2
- # Loads each autoloaded constant. If thread safety is a concern, wrap
3
- # this in a Mutex.
4
- def self.load
5
- constants.each do |const|
6
- const_get(const) if autoload?(const)
2
+ module AutoloadHelper
3
+ def autoload_all(prefix, options)
4
+ options.each do |const_name, path|
5
+ autoload const_name, File.join(prefix, path)
6
+ end
7
+ end
8
+
9
+ # Loads each autoloaded constant. If thread safety is a concern, wrap
10
+ # this in a Mutex.
11
+ def load
12
+ constants.each do |const|
13
+ const_get(const) if autoload?(const)
14
+ end
7
15
  end
8
16
  end
9
17
 
10
- autoload :Connection, 'faraday/connection'
11
- autoload :TestConnection, 'faraday/test_connection'
12
- autoload :Response, 'faraday/response'
13
- autoload :Error, 'faraday/error'
18
+ extend AutoloadHelper
19
+
20
+ autoload_all 'faraday',
21
+ :Connection => 'connection',
22
+ :TestConnection => 'test_connection',
23
+ :Response => 'response',
24
+ :Error => 'error'
14
25
 
15
26
  module Adapter
16
- autoload :NetHttp, 'faraday/adapter/net_http'
17
- autoload :Typhoeus, 'faraday/adapter/typhoeus'
18
- autoload :MockRequest, 'faraday/adapter/mock_request'
27
+ extend AutoloadHelper
28
+ autoload_all 'faraday/adapter',
29
+ :NetHttp => 'net_http',
30
+ :Typhoeus => 'typhoeus',
31
+ :MockRequest => 'mock_request'
19
32
 
20
33
  # Names of available adapters. Should not actually load them.
21
34
  def self.adapters
@@ -35,16 +35,19 @@ module Faraday
35
35
  class Stub < Struct.new(:path, :request_headers, :status, :response_headers, :body)
36
36
  def matches?(request_path, headers)
37
37
  return false if request_path != path
38
- return true if request_headers.empty?
39
38
  request_headers.each do |key, value|
40
- return true if headers[key] == value
41
- end
42
- false
39
+ return false if headers[key] != value
40
+ end
41
+ true
43
42
  end
44
43
  end
45
44
 
46
45
  def initialize &block
47
46
  super nil
47
+ configure(&block) if block
48
+ end
49
+
50
+ def configure
48
51
  yield stubs
49
52
  end
50
53
 
@@ -5,9 +5,9 @@ module Faraday
5
5
  extend Faraday::Connection::Options
6
6
 
7
7
  def _get(uri, request_headers)
8
- http = Net::HTTP.new(uri.host, uri.port)
8
+ http = Net::HTTP.new(uri.host, uri.port)
9
9
  response_class.new do |resp|
10
- http_resp = http.get(uri.path, request_headers) do |chunk|
10
+ http_resp = http.get(path_for(uri), request_headers) do |chunk|
11
11
  resp.process(chunk)
12
12
  end
13
13
  http_resp.each_header do |key, value|
@@ -12,18 +12,30 @@ module Faraday
12
12
 
13
13
  include Addressable
14
14
 
15
- attr_accessor :host, :port, :scheme
15
+ attr_accessor :host, :port, :scheme, :params, :headers
16
16
  attr_reader :path_prefix
17
17
 
18
- def initialize(url = nil)
19
- @response_class = nil
20
- if url
21
- uri = URI.parse(url)
22
- self.scheme = uri.scheme
23
- self.host = uri.host
24
- self.port = uri.port
25
- self.path_prefix = uri.path
18
+ # :url
19
+ # :params
20
+ # :headers
21
+ # :response
22
+ def initialize(url = nil, options = {})
23
+ if url.is_a?(Hash)
24
+ options = url
25
+ url = options[:url]
26
26
  end
27
+ @response_class = options[:response]
28
+ @params = options[:params] || {}
29
+ @headers = options[:headers] || {}
30
+ self.url_prefix = url if url
31
+ end
32
+
33
+ def url_prefix=(url)
34
+ uri = URI.parse(url)
35
+ self.scheme = uri.scheme
36
+ self.host = uri.host
37
+ self.port = uri.port
38
+ self.path_prefix = uri.path
27
39
  end
28
40
 
29
41
  # Override in a subclass, or include an adapter
@@ -31,8 +43,9 @@ module Faraday
31
43
  # def _get(uri, headers)
32
44
  # end
33
45
  #
34
- def get(url, params = {}, headers = {})
35
- _get(build_uri(url, params), headers)
46
+ def get(url, params = nil, headers = nil)
47
+ uri = build_uri(url, build_params(params))
48
+ _get(uri, build_headers(headers))
36
49
  end
37
50
 
38
51
  def response_class
@@ -49,16 +62,16 @@ module Faraday
49
62
  def in_parallel?
50
63
  !!@parallel_manager
51
64
  end
52
-
65
+
53
66
  def in_parallel(options = {})
54
67
  @parallel_manager = true
55
68
  yield
56
69
  @parallel_manager = false
57
70
  end
58
-
71
+
59
72
  def setup_parallel_manager(options = {})
60
73
  end
61
-
74
+
62
75
  def run_parallel_requests
63
76
  end
64
77
 
@@ -70,7 +83,7 @@ module Faraday
70
83
  @path_prefix = value
71
84
  end
72
85
 
73
- def build_uri(url, params = {})
86
+ def build_uri(url, params = nil)
74
87
  uri = URI.parse(url)
75
88
  uri.scheme ||= @scheme
76
89
  uri.host ||= @host
@@ -78,11 +91,35 @@ module Faraday
78
91
  if @path_prefix && uri.path !~ /^\//
79
92
  uri.path = "#{@path_prefix.size > 1 ? @path_prefix : nil}/#{uri.path}"
80
93
  end
81
- query = params_to_query(params)
82
- if !query.empty? then uri.query = query end
94
+ if params && !params.empty?
95
+ uri.query = params_to_query(params)
96
+ end
83
97
  uri
84
98
  end
85
99
 
100
+ def path_for(uri)
101
+ uri.path.tap do |s|
102
+ s << "?#{uri.query}" if uri.query
103
+ s << "##{uri.fragment}" if uri.fragment
104
+ end
105
+ end
106
+
107
+ def build_params(existing)
108
+ build_hash :params, existing
109
+ end
110
+
111
+ def build_headers(existing)
112
+ build_hash(:headers, existing).tap do |headers|
113
+ headers.keys.each do |key|
114
+ headers[key] = headers.delete(key).to_s
115
+ end
116
+ end
117
+ end
118
+
119
+ def build_hash(method, existing)
120
+ existing ? send(method).merge(existing) : send(method)
121
+ end
122
+
86
123
  def params_to_query(params)
87
124
  params.inject([]) do |memo, (key, val)|
88
125
  memo << "#{escape_for_querystring(key)}=#{escape_for_querystring(val)}"
@@ -92,7 +129,7 @@ module Faraday
92
129
  # Some servers convert +'s in URL query params to spaces.
93
130
  # Go ahead and encode it.
94
131
  def escape_for_querystring(s)
95
- URI.encode_component(s, Addressable::URI::CharacterClasses::QUERY).tap do |escaped|
132
+ URI.encode_component(s.to_s, Addressable::URI::CharacterClasses::QUERY).tap do |escaped|
96
133
  escaped.gsub! /\+/, "%2B"
97
134
  end
98
135
  end
@@ -7,7 +7,9 @@ module Faraday
7
7
  end
8
8
  end
9
9
 
10
- autoload :YajlResponse, 'faraday/response/yajl_response'
10
+ extend AutoloadHelper
11
+ autoload_all 'faraday/response',
12
+ :YajlResponse => 'yajl_response'
11
13
 
12
14
  def initialize(headers = nil, body = nil)
13
15
  super(headers || {}, body)
data/test/adapter_test.rb CHANGED
@@ -11,6 +11,16 @@ class AdapterTest < Faraday::TestCase
11
11
  @connection.extend adapter
12
12
  end
13
13
 
14
+ it "passes params" do
15
+ @connection.params = {:a => 1}
16
+ assert_equal "params[:a] == 1", @connection.get('params').body
17
+ end
18
+
19
+ it "passes headers" do
20
+ @connection.headers = {"X-Test" => 1}
21
+ assert_equal "env[HTTP_X_TEST] == 1", @connection.get('headers').body
22
+ end
23
+
14
24
  it "retrieves the response body" do
15
25
  assert_equal 'hello world', @connection.get('hello_world').body
16
26
  end
@@ -31,6 +31,21 @@ class ConnectionTest < Faraday::TestCase
31
31
  conn = Faraday::Connection.new "http://sushi.com/fish"
32
32
  assert_equal '/fish', conn.path_prefix
33
33
  end
34
+
35
+ it "parses @path_prefix out of given url option" do
36
+ conn = Faraday::Connection.new :url => "http://sushi.com/fish"
37
+ assert_equal '/fish', conn.path_prefix
38
+ end
39
+
40
+ it "stores default params from options" do
41
+ conn = Faraday::Connection.new :params => {:a => 1}
42
+ assert_equal 1, conn.params[:a]
43
+ end
44
+
45
+ it "stores default headers from options" do
46
+ conn = Faraday::Connection.new :headers => {:a => 1}
47
+ assert_equal 1, conn.headers[:a]
48
+ end
34
49
  end
35
50
 
36
51
  describe "#build_uri" do
data/test/helper.rb CHANGED
@@ -8,6 +8,12 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
9
  require 'faraday'
10
10
 
11
+ begin
12
+ require 'ruby-debug'
13
+ Debugger.start
14
+ rescue LoadError
15
+ end
16
+
11
17
  module Faraday
12
18
  class TestCase < Test::Unit::TestCase
13
19
  LIVE_SERVER = 'http://localhost:4567'
data/test/live_server.rb CHANGED
@@ -7,4 +7,12 @@ end
7
7
 
8
8
  get '/json' do
9
9
  "[1,2,3]"
10
+ end
11
+
12
+ get '/params' do
13
+ %(params[:a] == #{params[:a]})
14
+ end
15
+
16
+ get "/headers" do
17
+ %(env[HTTP_X_TEST] == #{env["HTTP_X_TEST"]})
10
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rick
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-19 00:00:00 -08:00
12
+ date: 2010-01-02 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15