curt-wren 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/wren.rb +14 -6
- data/test/test_client.rb +58 -0
- data/test/test_configuration.rb +77 -0
- data/test/test_helper.rb +42 -0
- data/test/test_http.rb +13 -0
- data/test/test_mock.rb +14 -0
- metadata +9 -5
data/lib/wren.rb
CHANGED
@@ -24,10 +24,12 @@ module Wren
|
|
24
24
|
if params.is_a?(Hash)
|
25
25
|
params.each{ |k, v| self.send("#{k.to_sym}=", v) }
|
26
26
|
end
|
27
|
-
|
28
|
-
@logger
|
29
|
-
|
30
|
-
|
27
|
+
|
28
|
+
unless @logger
|
29
|
+
@logger = Logger.new(STDOUT)
|
30
|
+
@logger.level = Logger::DEBUG
|
31
|
+
end
|
32
|
+
@transport ||= Wren::HTTP
|
31
33
|
|
32
34
|
yield self if block_given?
|
33
35
|
end
|
@@ -56,7 +58,7 @@ module Wren
|
|
56
58
|
@config = arg
|
57
59
|
end
|
58
60
|
end
|
59
|
-
|
61
|
+
|
60
62
|
private
|
61
63
|
|
62
64
|
def method_missing(name, *args)
|
@@ -67,6 +69,8 @@ module Wren
|
|
67
69
|
|
68
70
|
class Request
|
69
71
|
TWITTER_ROOT_URL = "http://twitter.com"
|
72
|
+
|
73
|
+
attr_reader :auth, :options, :data, :path
|
70
74
|
|
71
75
|
def initialize(client)
|
72
76
|
@client = client
|
@@ -80,6 +84,10 @@ module Wren
|
|
80
84
|
@auth = true
|
81
85
|
self
|
82
86
|
end
|
87
|
+
|
88
|
+
def config
|
89
|
+
@client.config
|
90
|
+
end
|
83
91
|
|
84
92
|
def options(params)
|
85
93
|
@options.merge!(params)
|
@@ -103,7 +111,7 @@ module Wren
|
|
103
111
|
@path << "/#{name}"
|
104
112
|
self
|
105
113
|
end
|
106
|
-
|
114
|
+
|
107
115
|
def url_with_options
|
108
116
|
"#{TWITTER_ROOT_URL}#{@path}.json#{query_string}"
|
109
117
|
end
|
data/test/test_client.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestClient < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
end
|
6
|
+
|
7
|
+
def test_method_chain
|
8
|
+
req = mock_transport_client_good.something.else
|
9
|
+
assert_equal(req.path, '/something/else')
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_auth
|
13
|
+
req = mock_transport_client_good.something.auth
|
14
|
+
assert_equal(req.path, '/something')
|
15
|
+
assert(req.auth)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_post_forces_auth_and_ssl
|
19
|
+
req = mock_transport_client_good.something
|
20
|
+
assert_nothing_raised { req.post }
|
21
|
+
assert_equal(req.path, '/something')
|
22
|
+
assert(req.auth)
|
23
|
+
assert(req.config.transport.use_ssl)
|
24
|
+
assert_equal(443, req.config.transport.port)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_response_http_server_error
|
28
|
+
client = mock_transport_client(Net::HTTPServerError, 500, "body")
|
29
|
+
assert_equal(MockTransport, client.config.transport)
|
30
|
+
assert_raise(Wren::TwitterError) { client.something.get }
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_response_http_ok_bad_json
|
34
|
+
client = mock_transport_client(Net::HTTPOK, 200, "body")
|
35
|
+
assert_equal(MockTransport, client.config.transport)
|
36
|
+
assert_raise(Wren::TwitterError) { client.something.get }
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_response_http_ok_good_json
|
40
|
+
client = mock_transport_client_good
|
41
|
+
assert_equal(MockTransport, client.config.transport)
|
42
|
+
assert_nothing_raised { client.something.get }
|
43
|
+
end
|
44
|
+
|
45
|
+
def teardown
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def mock_transport_client(resp_type, resp_code, resp_body)
|
51
|
+
transport = MockTransport.provides_response(resp_type, resp_code, resp_body)
|
52
|
+
Wren::Client.new(:transport => transport)
|
53
|
+
end
|
54
|
+
|
55
|
+
def mock_transport_client_good
|
56
|
+
mock_transport_client(Net::HTTPOK, 200, "[1,2]")
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestConfiguration < Test::Unit::TestCase
|
4
|
+
def test_initialize_nil
|
5
|
+
configuration = Wren::Configuration.new
|
6
|
+
test_configuration_default(configuration)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_initialize_block_empty
|
10
|
+
configuration = Wren::Configuration.new{}
|
11
|
+
test_configuration_default(configuration)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_initialize_block_set
|
15
|
+
user = random_string
|
16
|
+
pwd = random_string
|
17
|
+
level = random_string
|
18
|
+
transport = MockTransport
|
19
|
+
logger = MockLogger.new
|
20
|
+
logger.level = level
|
21
|
+
configuration = Wren::Configuration.new do |c|
|
22
|
+
c.user = user
|
23
|
+
c.pwd = pwd
|
24
|
+
c.transport = transport
|
25
|
+
c.logger = logger
|
26
|
+
end
|
27
|
+
test_configuration_specific(configuration, user, pwd, transport, logger, level)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_initialize_hash_empty
|
31
|
+
configuration = Wren::Configuration.new( {} )
|
32
|
+
test_configuration_default(configuration)
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def test_initialize_hash_set
|
37
|
+
user = random_string
|
38
|
+
pwd = random_string
|
39
|
+
level = random_string
|
40
|
+
transport = MockTransport
|
41
|
+
logger = MockLogger.new
|
42
|
+
logger.level = level
|
43
|
+
configuration = Wren::Configuration.new(:user => user, :pwd => pwd, :transport => transport, :logger => logger)
|
44
|
+
test_configuration_specific(configuration, user, pwd, transport, logger, level)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def test_configuration_default(configuration)
|
50
|
+
test_configuration_response(configuration)
|
51
|
+
assert_instance_of(Logger, configuration.logger)
|
52
|
+
assert_equal(Logger::DEBUG, configuration.logger.level)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_configuration_specific(configuration, user, pwd, transport, logger, level)
|
56
|
+
test_configuration_response(configuration)
|
57
|
+
assert_equal(user, configuration.user)
|
58
|
+
assert_equal(pwd, configuration.pwd)
|
59
|
+
assert_equal(transport, configuration.transport)
|
60
|
+
assert_equal(logger, configuration.logger)
|
61
|
+
assert_equal(level, configuration.logger.level)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_configuration_response(configuration)
|
65
|
+
assert_instance_of(Wren::Configuration, configuration)
|
66
|
+
assert_respond_to(configuration, :user)
|
67
|
+
assert_respond_to(configuration, :pwd)
|
68
|
+
assert_respond_to(configuration, :logger)
|
69
|
+
assert_respond_to(configuration, :transport)
|
70
|
+
assert_respond_to(configuration.logger, :level)
|
71
|
+
assert_instance_of(Class, configuration.transport)
|
72
|
+
end
|
73
|
+
|
74
|
+
def random_string
|
75
|
+
Array.new(rand(19) + 2){ rand(36).to_s(36) }.to_s
|
76
|
+
end
|
77
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
4
|
+
require 'wren'
|
5
|
+
|
6
|
+
class MockTransport
|
7
|
+
def initialize(host, port = 0)
|
8
|
+
self.class.host = host
|
9
|
+
self.class.port = port
|
10
|
+
end
|
11
|
+
|
12
|
+
def request(req)
|
13
|
+
self.class.request = req
|
14
|
+
self.class.response
|
15
|
+
end
|
16
|
+
|
17
|
+
def use_ssl=(val)
|
18
|
+
self.class.use_ssl = val
|
19
|
+
end
|
20
|
+
|
21
|
+
class << self
|
22
|
+
attr_accessor :response, :host, :port, :use_ssl, :request
|
23
|
+
|
24
|
+
def provides_response(parent, status, body)
|
25
|
+
klass = Class.new(parent)
|
26
|
+
klass.class_eval <<-EOS
|
27
|
+
def body
|
28
|
+
"#{body}"
|
29
|
+
end
|
30
|
+
def status
|
31
|
+
#{status}
|
32
|
+
end
|
33
|
+
EOS
|
34
|
+
@response = klass.new(nil, status, nil)
|
35
|
+
self
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class MockLogger
|
41
|
+
attr_accessor :level
|
42
|
+
end
|
data/test/test_http.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestHttp < Test::Unit::TestCase
|
4
|
+
def test_superclass
|
5
|
+
assert_equal(Net::HTTP, Wren::HTTP.superclass)
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_initialize
|
9
|
+
http = Wren::HTTP.new("nothing")
|
10
|
+
assert_respond_to(http, :use_ssl=)
|
11
|
+
assert_respond_to(http, :request)
|
12
|
+
end
|
13
|
+
end
|
data/test/test_mock.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestMock < Test::Unit::TestCase
|
4
|
+
def test_transport_initialize
|
5
|
+
http = MockTransport.new("nothing")
|
6
|
+
assert_respond_to(http, :use_ssl=)
|
7
|
+
assert_respond_to(http, :request)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_logger_initialize
|
11
|
+
log = MockLogger.new
|
12
|
+
assert_respond_to(log, :level)
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: curt-wren
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Curt Gilman
|
7
|
+
- Curt A. Gilman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-27 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -59,5 +59,9 @@ rubygems_version: 1.2.0
|
|
59
59
|
signing_key:
|
60
60
|
specification_version: 2
|
61
61
|
summary: Wren is a very lightweight Ruby wrapper for the Twitter REST API.
|
62
|
-
test_files:
|
63
|
-
|
62
|
+
test_files:
|
63
|
+
- test/test_client.rb
|
64
|
+
- test/test_configuration.rb
|
65
|
+
- test/test_helper.rb
|
66
|
+
- test/test_http.rb
|
67
|
+
- test/test_mock.rb
|