twitter4r 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/twitter/core.rb CHANGED
@@ -101,6 +101,7 @@ module Twitter
101
101
  # /i_am_crap.json
102
102
  class RESTError < Exception
103
103
  include ClassUtilMixin
104
+ @@ATTRIBUTES = [:code, :message, :uri]
104
105
  attr_accessor :code, :message, :uri
105
106
 
106
107
  # Returns string in following format:
@@ -21,12 +21,16 @@ end
21
21
  # Extension to Time that outputs RFC2822 compliant string on #to_s
22
22
  class Time
23
23
  alias :old_to_s :to_s
24
+
24
25
  # Returns RFC2822 compliant string for <tt>Time</tt> object.
25
26
  # For example,
26
27
  # # Tony Blair's last day in office (hopefully)
27
28
  # best_day_ever = Time.local(2007, 6, 27)
28
29
  # best_day_ever.to_s # => "Wed, 27 Jun 2007 00:00:00 +0100"
29
- def to_s
30
- self.rfc2822
30
+ # You can also pass in an option <tt>format</tt> argument that
31
+ # corresponds to acceptable values according to ActiveSupport's
32
+ # +Time#to_formatted_s+ method.
33
+ def to_s(format = nil)
34
+ format ? self.to_formatted_s(format) : self.rfc2822
31
35
  end
32
36
  end
data/lib/twitter/meta.rb CHANGED
@@ -34,7 +34,7 @@ class Twitter::Meta #:nodoc:
34
34
 
35
35
  # Returns list of specification files
36
36
  def spec_files
37
- @spec_files ||= Dir.glob(File.join(@root_dir, 'spec/**/*.rb'))
37
+ @spec_files ||= Dir.glob(File.join(@root_dir, 'spec/**/*_spec.rb'))
38
38
  @spec_files
39
39
  end
40
40
 
@@ -0,0 +1,89 @@
1
+ # File that contains extensions to the Twitter4R library directly related to providing
2
+ # seamless Rails integration.
3
+
4
+ require 'active_support'
5
+ #require 'active_support/core_ext'
6
+ require 'active_record/xml_serialization'
7
+
8
+ # Extend +String+ with +#xmlize+ method for convenience.
9
+ class String
10
+ def xmlize
11
+ # Forget module/namespace for now as this is totally broken in Rails 1.2.x
12
+ # (search for namespaced models on the Rails Trac site)
13
+ cls = self.split('::').pop
14
+ cls.dasherize.downcase
15
+ end
16
+ end
17
+
18
+ # Parent mixin module that defines +InstanceMethods+ for Twitter4R model classes.
19
+ #
20
+ # Defines/overrides the following methods for:
21
+ # * Twitter::RailsPatch::InstanceMethods#to_param
22
+ # * Twitter::RailsPatch::InstanceMethods#to_xml
23
+ # * Twitter::RailsPatch::InstanceMethods#to_json
24
+ module Twitter::RailsPatch
25
+ class << self
26
+ def included(base)
27
+ base.send(:include, InstanceMethods)
28
+ base.extend ClassMethods
29
+ end
30
+
31
+ module ClassMethods
32
+ # Dummy method to satisfy ActiveRecord's XmlSerializer.
33
+ def inheritance_column
34
+ nil
35
+ end
36
+
37
+ # Returns Hash of name-column pairs
38
+ def columns_hash
39
+ {}
40
+ end
41
+ end
42
+
43
+ module InstanceMethods
44
+ # Returns an Array of attribute names of the model
45
+ def attribute_names
46
+ self.class.class_eval("@@ATTRIBUTES").collect {|att| att.to_s }
47
+ end
48
+
49
+ # Override default +#to_param+ implementation.
50
+ def to_param
51
+ self.id.to_s
52
+ end
53
+
54
+ # Returns XML representation of model.
55
+ def to_xml(options = {})
56
+ ActiveRecord::XmlSerializer.new(self, options.merge(:root => self.class.name.xmlize)).to_s
57
+ end
58
+
59
+ # Returns JSON representation of model.
60
+ #
61
+ # The current implementation basically ignoring +options+, so reopen and override
62
+ # this implementation or live with it for the moment:)
63
+ def to_json(options = {})
64
+ JSON.unparse(self.to_hash)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ class Twitter::User
71
+ include Twitter::RailsPatch
72
+ end
73
+
74
+ class Twitter::Status
75
+ include Twitter::RailsPatch
76
+ end
77
+
78
+ class Twitter::Message
79
+ include Twitter::RailsPatch
80
+ end
81
+
82
+ class Twitter::RESTError
83
+ include Twitter::RailsPatch
84
+ alias :id :code
85
+ def to_hash
86
+ { :code => @code, :message => @message, :uri => @uri }
87
+ end
88
+ end
89
+
@@ -4,7 +4,7 @@
4
4
  module Twitter::Version #:nodoc:
5
5
  MAJOR = 0
6
6
  MINOR = 2
7
- REVISION = 3
7
+ REVISION = 4
8
8
  class << self
9
9
  # Returns X.Y.Z formatted version string
10
10
  def to_version
@@ -35,13 +35,13 @@ describe Twitter::Client, "#http_header" do
35
35
  'User-Agent' => "Twitter4R v#{Twitter::Version.to_version} [#{@user_agent}]",
36
36
  }
37
37
  @twitter = client_context
38
- # resent @@http_header class variable in Twitter::Client class
38
+ # reset @@http_header class variable in Twitter::Client class
39
39
  Twitter::Client.class_eval("@@http_header = nil")
40
40
  end
41
41
 
42
42
  it "should always return expected HTTP headers" do
43
43
  headers = @twitter.send(:http_header)
44
- headers.should eql(@expected_headers)
44
+ headers.should === @expected_headers
45
45
  end
46
46
 
47
47
  it "should cache HTTP headers Hash in class variable after first invocation" do
@@ -50,7 +50,7 @@ describe Twitter::Client, "#http_header" do
50
50
  @twitter.send(:http_header)
51
51
  cache = Twitter::Client.class_eval("@@http_header")
52
52
  cache.should_not be_nil
53
- cache.should eql(@expected_headers)
53
+ cache.should === @expected_headers
54
54
  end
55
55
 
56
56
  after(:each) do
@@ -55,8 +55,8 @@ describe Twitter::Config, "#eql?" do
55
55
  :proxy_host => @proxy_host,
56
56
  :proxy_port => @proxy_port,
57
57
  }
58
- @obj = stubbed_twitter_config(Twitter::Config.new, attrs)
59
- @other = stubbed_twitter_config(Twitter::Config.new, attrs)
58
+ @obj = Twitter::Config.new(attrs)
59
+ @other = Twitter::Config.new(attrs)
60
60
 
61
61
  @different = stubbed_twitter_config(Twitter::Config.new, attrs.merge(:proxy_host => 'different.proxy'))
62
62
  @same = @obj
@@ -17,7 +17,7 @@ module ERBMetaMixin
17
17
 
18
18
  # Needed to make the YAML load work...
19
19
  def spec_files
20
- glob_files(@root_dir, 'spec', '**/*.rb')
20
+ glob_files(@root_dir, 'spec', '**/*_spec.rb')
21
21
  end
22
22
  end
23
23
 
@@ -0,0 +1,110 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe String, "representing a class name" do
4
+
5
+ before(:each) do
6
+ @class_name = Twitter::User.class.name
7
+ @xmlized_name = @class_name.downcase # simple case for the moment...since Rails' support for namespaced models sucks!
8
+ end
9
+
10
+ it "should be downcased (minus module namespaces) when xmlized" do
11
+ @class_name.xmlize.should eql(@xmlized_name)
12
+ end
13
+
14
+ after(:each) do
15
+ nilize(@class_name, @xmlized_name)
16
+ end
17
+ end
18
+
19
+ describe "Rails model extensions for model classes", :shared => true do
20
+
21
+ before(:each) do
22
+ @id = 3242334
23
+ @id_s = @id.to_s
24
+ @model = model(@id)
25
+ @model_hash = @model.to_hash
26
+ @json = JSON.unparse(@model_hash)
27
+ @serializer = ActiveRecord::XmlSerializer.new(@model, {:root => @model.class.name.downcase})
28
+ @xml = @serializer.to_s
29
+ end
30
+
31
+ it "should invoke #to_param as expected" do
32
+ @model.should_receive(:id).and_return(@id)
33
+ @model.to_param
34
+ end
35
+
36
+ it "should return string representation for id for #to_param" do
37
+ @model.to_param.class.should eql(String)
38
+ end
39
+
40
+ it "should return output from JSON.unparse for #to_json" do
41
+ @model.should_receive(:to_hash).and_return(@model_hash)
42
+ JSON.should_receive(:unparse).and_return(@json)
43
+ @model.to_json
44
+ end
45
+
46
+ it "should return XmlSerializer string output for #to_xml" do
47
+ ActiveRecord::XmlSerializer.should_receive(:new).and_return(@serializer)
48
+ @serializer.should_receive(:to_s).and_return(@xml)
49
+ @model.to_xml
50
+ end
51
+
52
+ after(:each) do
53
+ nilize(@id, @model)
54
+ end
55
+ end
56
+
57
+ describe Twitter::User, "Rails extensions" do
58
+
59
+ def model(id)
60
+ Twitter::User.new(:id => id)
61
+ end
62
+
63
+ it_should_behave_like "Rails model extensions for model classes"
64
+ end
65
+
66
+ describe Twitter::Status, "Rails extensions" do
67
+
68
+ def model(id)
69
+ Twitter::Status.new(:id => id)
70
+ end
71
+
72
+ it_should_behave_like "Rails model extensions for model classes"
73
+ end
74
+
75
+ describe Twitter::Message, "Rails extensions" do
76
+
77
+ def model(id)
78
+ Twitter::Message.new(:id => id)
79
+ end
80
+
81
+ it_should_behave_like "Rails model extensions for model classes"
82
+ end
83
+
84
+ describe Twitter::RESTError, "Rails extensions" do
85
+
86
+ before(:each) do
87
+ @attributes = { :code => 200, :message => 'Success', :uri => 'http://twitter.com/statuses' }
88
+ @model = Twitter::RESTError.new(@attributes)
89
+ @json = "JSON" # doesn't really matter what the value is
90
+ end
91
+
92
+ it "should return a Hash of attribute name-value pairs for #to_hash" do
93
+ @model.to_hash.should === @attributes
94
+ end
95
+
96
+ it "should invoke XmlSerializer for #to_xml" do
97
+
98
+ @model.to_xml
99
+ end
100
+
101
+ it "should return expected JSON for #to_json" do
102
+ @model.to_hash
103
+ JSON.should_receive(:unparse).and_return(@json)
104
+ @model.to_json
105
+ end
106
+
107
+ after(:each) do
108
+ nilize(@attributes, @model)
109
+ end
110
+ end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: twitter4r
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.3
7
- date: 2007-07-22 00:00:00 -05:00
6
+ version: 0.2.4
7
+ date: 2007-07-25 00:00:00 -05:00
8
8
  summary: A clean Twitter client API in pure Ruby. Will include Twitter add-ons also in Ruby.
9
9
  require_paths:
10
10
  - lib
@@ -30,38 +30,39 @@ authors:
30
30
  - Susan Potter
31
31
  files:
32
32
  - lib/twitter.rb
33
+ - lib/twitter/ext/stdlib.rb
34
+ - lib/twitter/console.rb
35
+ - lib/twitter/ext.rb
36
+ - lib/twitter/rails.rb
37
+ - lib/twitter/meta.rb
38
+ - lib/twitter/client.rb
39
+ - lib/twitter/client/status.rb
40
+ - lib/twitter/client/friendship.rb
41
+ - lib/twitter/client/user.rb
42
+ - lib/twitter/client/timeline.rb
43
+ - lib/twitter/client/messaging.rb
44
+ - lib/twitter/client/base.rb
33
45
  - lib/twitter/config.rb
46
+ - lib/twitter/model.rb
34
47
  - lib/twitter/extras.rb
35
48
  - lib/twitter/version.rb
36
- - lib/twitter/client.rb
37
- - lib/twitter/model.rb
38
- - lib/twitter/meta.rb
39
- - lib/twitter/ext.rb
40
49
  - lib/twitter/core.rb
41
- - lib/twitter/console.rb
42
- - lib/twitter/client/user.rb
43
- - lib/twitter/client/base.rb
44
- - lib/twitter/client/status.rb
45
- - lib/twitter/client/messaging.rb
46
- - lib/twitter/client/timeline.rb
47
- - lib/twitter/client/friendship.rb
48
- - lib/twitter/ext/stdlib.rb
49
- - spec/spec_helper.rb
50
- - spec/twitter/model_spec.rb
51
- - spec/twitter/core_spec.rb
50
+ - spec/twitter/meta_spec.rb
51
+ - spec/twitter/ext/stdlib_spec.rb
52
52
  - spec/twitter/client_spec.rb
53
- - spec/twitter/extras_spec.rb
54
53
  - spec/twitter/version_spec.rb
55
54
  - spec/twitter/console_spec.rb
56
- - spec/twitter/meta_spec.rb
57
- - spec/twitter/config_spec.rb
58
- - spec/twitter/client/timeline_spec.rb
59
55
  - spec/twitter/client/base_spec.rb
60
- - spec/twitter/client/status_spec.rb
56
+ - spec/twitter/client/user_spec.rb
61
57
  - spec/twitter/client/friendship_spec.rb
62
58
  - spec/twitter/client/messaging_spec.rb
63
- - spec/twitter/client/user_spec.rb
64
- - spec/twitter/ext/stdlib_spec.rb
59
+ - spec/twitter/client/timeline_spec.rb
60
+ - spec/twitter/client/status_spec.rb
61
+ - spec/twitter/extras_spec.rb
62
+ - spec/twitter/core_spec.rb
63
+ - spec/twitter/rails_spec.rb
64
+ - spec/twitter/config_spec.rb
65
+ - spec/twitter/model_spec.rb
65
66
  test_files: []
66
67
 
67
68
  rdoc_options: []
data/spec/spec_helper.rb DELETED
@@ -1,133 +0,0 @@
1
- require 'spec'
2
- require 'twitter'
3
- require 'twitter/console'
4
- require 'twitter/extras'
5
-
6
- # Add helper methods here if relevant to multiple _spec.rb files
7
-
8
- # Spec helper that sets attribute <tt>att</tt> for given objects <tt>obj</tt>
9
- # and <tt>other</tt> to given <tt>value</tt>.
10
- def equalizer(obj, other, att, value)
11
- setter = "#{att}="
12
- obj.send(setter, value)
13
- other.send(setter, value)
14
- end
15
-
16
- # Spec helper that nil-izes objects passed in
17
- def nilize(*objects)
18
- objects.each {|obj| obj = nil }
19
- end
20
-
21
- # Returns default <tt>client</tt> context object
22
- def client_context(file = 'config/twitter.yml')
23
- Twitter::Client.from_config(file)
24
- end
25
-
26
- # Spec helper that returns a mocked <tt>Twitter::Config</tt> object
27
- # with stubbed attributes and <tt>attrs</tt> for overriding attribute
28
- # values.
29
- def stubbed_twitter_config(config, attrs = {})
30
- opts = {
31
- :protocol => :ssl,
32
- :host => 'twitter.com',
33
- :port => 443,
34
- :proxy_host => 'proxy.host',
35
- :proxy_port => 8080,
36
- }.merge(attrs)
37
- config.stub!(:protocol).and_return(opts[:protocol])
38
- config.stub!(:host).and_return(opts[:host])
39
- config.stub!(:port).and_return(opts[:port])
40
- config.stub!(:proxy_host).and_return(opts[:proxy_host])
41
- config.stub!(:proxy_port).and_return(opts[:proxy_port])
42
- config
43
- end
44
-
45
- def mas_twitter_config(attrs = {})
46
- config = mock(Twitter::Config)
47
- stubbed_twitter_conf(config, attrs)
48
- end
49
-
50
- # Spec helper that returns the project root directory as absolute path string
51
- def project_root_dir
52
- File.expand_path(File.join(File.dirname(__FILE__), '..'))
53
- end
54
-
55
- # Spec helper that returns stubbed <tt>Net::HTTP</tt> object
56
- # with given <tt>response</tt> and <tt>obj_stubs</tt>.
57
- # The <tt>host</tt> and <tt>port</tt> are used to initialize
58
- # the Net::HTTP object.
59
- def stubbed_net_http(response, obj_stubs = {}, host = 'twitter.com', port = 80)
60
- http = Net::HTTP.new(host, port)
61
- Net::HTTP.stub!(:new).and_return(http)
62
- http.stub!(:request).and_return(response)
63
- http
64
- end
65
-
66
- # Spec helper that returns a mocked <tt>Net::HTTP</tt> object and
67
- # stubs out the <tt>request</tt> method to return the given
68
- # <tt>response</tt>
69
- def mas_net_http(response, obj_stubs = {})
70
- http = mock(Net::HTTP, obj_stubs)
71
- Net::HTTP.stub!(:new).and_return(http)
72
- http.stub!(:request).and_return(response)
73
- http.stub!(:start).and_yield(http)
74
- http.stub!(:use_ssl=)
75
- http.stub!(:verify_mode=)
76
- http
77
- end
78
-
79
- # Spec helper that returns a mocked <tt>Net::HTTP::Get</tt> object and
80
- # stubs relevant class methods and given <tt>obj_stubs</tt>
81
- # for endo-specing
82
- def mas_net_http_get(obj_stubs = {})
83
- request = Spec::Mocks::Mock.new(Net::HTTP::Get)
84
- Net::HTTP::Get.stub!(:new).and_return(request)
85
- obj_stubs.each do |method, value|
86
- request.stub!(method).and_return(value)
87
- end
88
- request
89
- end
90
-
91
- # Spec helper that returns a mocked <tt>Net::HTTP::Post</tt> object and
92
- # stubs relevant class methods and given <tt>obj_stubs</tt>
93
- # for endo-specing
94
- def mas_net_http_post(obj_stubs = {})
95
- request = Spec::Mocks::Mock.new(Net::HTTP::Post)
96
- Net::HTTP::Post.stub!(:new).and_return(request)
97
- obj_stubs.each do |method, value|
98
- request.stub!(method).and_return(value)
99
- end
100
- request
101
- end
102
-
103
- # Spec helper that returns a mocked <tt>Net::HTTPResponse</tt> object and
104
- # stubs given <tt>obj_stubs</tt> for endo-specing.
105
- #
106
- def mas_net_http_response(status = :success,
107
- body = '',
108
- obj_stubs = {})
109
- response = Spec::Mocks::Mock.new(Net::HTTPResponse)
110
- response.stub!(:body).and_return(body)
111
- case status
112
- when :success || 200
113
- _create_http_response(response, "200", "OK")
114
- when :created || 201
115
- _create_http_response(response, "201", "Created")
116
- when :redirect || 301
117
- _create_http_response(response, "301", "Redirect")
118
- when :not_authorized || 403
119
- _create_http_response(response, "403", "Not Authorized")
120
- when :file_not_found || 404
121
- _create_http_response(response, "404", "File Not Found")
122
- when :server_error || 500
123
- _create_http_response(response, "500", "Server Error")
124
- end
125
- response
126
- end
127
-
128
- # Local helper method to DRY up code.
129
- def _create_http_response(mock_response, code, message)
130
- mock_response.stub!(:code).and_return(code)
131
- mock_response.stub!(:message).and_return(message)
132
- mock_response.stub!(:is_a?).and_return(true) if ["200", "201"].member?(code)
133
- end