twin 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +77 -0
  2. data/lib/twin.rb +5 -4
  3. data/lib/twin/resources.rb +13 -1
  4. metadata +32 -55
@@ -0,0 +1,77 @@
1
+ Twitter's Twin
2
+ ==============
3
+
4
+ <i>Twin</i> is a Rack middleware which provides your app with a subset of Twitter's API. In other words, it makes your app respond to certain requests as if it were "api.twitter.com".
5
+
6
+ This is useful in combination with Twitter clients which allow you to change the hostname used for API requests. If your Twitter client allows you to configure it to fetch updates from "yourapp.net" instead of "api.twitter.com", you could serve fake "tweets" from your app and use the Twitter client to read them.
7
+
8
+ **Twitter for iPhone** is a free client that allows you to customize the hostname. (Its version for iPad does not, for some reason.) Known desktop clients that support the same are [Twitterrific][] and [Spaz][].
9
+
10
+ Two popular websites which already implement the Twitter API are [WordPress.com][wp] and [Tumblr][]. Twin is a library for Ruby apps to do the same.
11
+
12
+
13
+ Installation
14
+ ------------
15
+
16
+ For Ruby on Rails, add "twin" to your Gemfile. For other apps, a little bit more is needed:
17
+
18
+ ## Gemfile for non-Rails apps
19
+ gem 'twin'
20
+ gem 'i18n' # not really used; needed to load some part of Active Support
21
+ gem 'builder' # used for constructing XML responses
22
+
23
+ Twin's dependency is Active Support, from which only few bits are loaded.
24
+
25
+ Now, mount "Twin" as middleware:
26
+
27
+ ## Ruby on Rails
28
+ config.middleware.use Twin
29
+
30
+ ## Sinatra
31
+ use Twin
32
+
33
+
34
+ Data models
35
+ -----------
36
+
37
+ Twin uses a model named by default "TwinAdapter" to fetch records from your app. You have to write this model yourself and implement several methods to define which data sent back to the client.
38
+
39
+ The template for TwinAdapter is available in ["example/twin_adapter.rb"][adapter].
40
+
41
+ The adapter should return two types of records: one representing twitter "statuses" and the other representing users. Both types of records should either be hashes containing twitter-compatible keys or they should implement the `to_twin_hash` method that returns such values.
42
+
43
+ Examples for implementing `to_twin_hash` for typical Active Record models is in ["example/to_twin_hash.rb"][hash].
44
+
45
+
46
+ Configuring Twitter for iPhone
47
+ ------------------------------
48
+
49
+ With Twin middleware and data models in place, your app is ready to receive requests from Twitter clients. To try this out with Twitter for iPhone, go to the "Accounts" page and start creating a new account. Before submitting the username and password, tap the cogwheel to access the advanced configuration screen. Type in your hostname for "API root", return back and finish the process.
50
+
51
+ ![Advanced configuration screen displaying "http://example.com"](http://img.skitch.com/20101129-fab66pj66hcqwu5wbwf3ci3y8f.png)
52
+
53
+ Using Twitter for iPhone in this manner may not be without bugs. This client caches user info based on screen name, *regardless* of the host where they come from. If a user from your Twin-enabled app shares the same screen name as an existing Twitter user, their info (and avatar pictures) might mix.
54
+
55
+
56
+ API support
57
+ -----------
58
+
59
+ Twin doesn't implement the full Twitter API — far from it. It only implements the basic subset required to read the main timeline. Posting is not yet implemented. Replies/mentions, lists, saved searches and direct messages are not functional; their read APIs are implemented but they return empty collections. There is no rate limiting.
60
+
61
+ To see which APIs are implemented, [check out "twin/resources.rb"][resources].
62
+
63
+
64
+ Credits
65
+ -------
66
+
67
+ Twin was written by Mislav Marohnić for [Teambox](http://teambox.com) and later extracted into a reusable library after a [gentle nudge by Shane Becker][shane].
68
+
69
+
70
+ [spaz]: http://getspaz.com/
71
+ [twitterrific]: http://iconfactory.com/software/twitterrific
72
+ [adapter]: https://github.com/mislav/twin/blob/master/example/twin_adapter.rb
73
+ [hash]: https://github.com/mislav/twin/blob/master/example/to_twin_hash.rb
74
+ [resources]: https://github.com/mislav/twin/blob/master/lib/twin/resources.rb
75
+ [wp]: http://en.support.wordpress.com/twitter-api/ "WordPress.com Twitter API"
76
+ [tumblr]: http://staff.tumblr.com/post/287703110/api
77
+ [shane]: http://iamshane.com/notes/2010/11/9/1/twitter-clone-gem "We need a Twitter API clone Ruby Gem"
@@ -150,9 +150,10 @@ class Twin
150
150
  end
151
151
 
152
152
  def convert_twin_hash(object)
153
- if Hash === object then object
154
- elsif object.respond_to? :to_twin_hash
153
+ if object.respond_to? :to_twin_hash
155
154
  object.to_twin_hash
155
+ elsif Hash === object
156
+ object
156
157
  elsif object.respond_to? :attributes
157
158
  object.attributes
158
159
  else
@@ -172,7 +173,7 @@ class Twin
172
173
  def content_type_from_format(format)
173
174
  case format
174
175
  when 'xml' then 'application/xml'
175
- when 'json' then 'application/x-json'
176
+ when 'json' then 'application/json'
176
177
  end
177
178
  end
178
179
 
@@ -182,7 +183,7 @@ class Twin
182
183
  else
183
184
  case self.content_type
184
185
  when 'application/xml' then body.to_xml
185
- when 'application/x-json' then body.to_json
186
+ when 'application/json' then body.to_json
186
187
  when 'application/x-www-form-urlencoded' then body.to_query
187
188
  else
188
189
  raise "unrecognized content type: #{self.content_type.inspect} (format: #{self.format})"
@@ -73,7 +73,19 @@ class Twin
73
73
  respond_with('saved_searches', [])
74
74
  end
75
75
 
76
- resource 'account/(settings|apple_push_destinations(/(destroy|device))?)' do
76
+ resource 'account/(settings|(apple_)?push_destinations(/(destroy|device))?)' do
77
+ not_implemented
78
+ end
79
+
80
+ resource 'trends/(\d+)' do
81
+ not_implemented
82
+ end
83
+
84
+ resource 'statuses/friends' do
85
+ not_implemented
86
+ end
87
+
88
+ resource 'help/configuration' do
77
89
  not_implemented
78
90
  end
79
91
 
metadata CHANGED
@@ -1,85 +1,62 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: twin
3
- version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
13
- - "Mislav Marohni\xC4\x87"
7
+ authors:
8
+ - Mislav Marohnić
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2010-11-29 00:00:00 +01:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2011-11-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: activesupport
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70155972080040 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 5
30
- segments:
31
- - 2
32
- - 3
33
- version: "2.3"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.3'
34
22
  type: :runtime
35
- version_requirements: *id001
36
- description: Rack middleware to expose a Twitter-like API from your app.
23
+ prerelease: false
24
+ version_requirements: *70155972080040
25
+ description: Rack middleware to expose a Twitter-like API in your app.
37
26
  email: mislav.marohnic@gmail.com
38
27
  executables: []
39
-
40
28
  extensions: []
41
-
42
29
  extra_rdoc_files: []
43
-
44
- files:
30
+ files:
45
31
  - lib/twin/resources.rb
46
32
  - lib/twin.rb
47
33
  - test/app.rb
48
34
  - test/config.ru
49
35
  - test/tmp/restart.txt
50
- has_rdoc: false
36
+ - README.md
51
37
  homepage: http://github.com/mislav/twin
52
38
  licenses: []
53
-
54
39
  post_install_message:
55
40
  rdoc_options: []
56
-
57
- require_paths:
41
+ require_paths:
58
42
  - lib
59
- required_ruby_version: !ruby/object:Gem::Requirement
43
+ required_ruby_version: !ruby/object:Gem::Requirement
60
44
  none: false
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- hash: 3
65
- segments:
66
- - 0
67
- version: "0"
68
- required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
50
  none: false
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- hash: 3
74
- segments:
75
- - 0
76
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
77
55
  requirements: []
78
-
79
56
  rubyforge_project:
80
- rubygems_version: 1.3.7
57
+ rubygems_version: 1.8.11
81
58
  signing_key:
82
59
  specification_version: 3
83
60
  summary: Twitter's twin
84
61
  test_files: []
85
-
62
+ has_rdoc: