clavem 1.4.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@
4
4
  <head>
5
5
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
6
  <title>
7
- Documentation by YARD 0.8.6.2
7
+ Documentation by YARD 0.8.7
8
8
 
9
9
  </title>
10
10
 
@@ -56,7 +56,7 @@
56
56
 
57
57
  <iframe id="search_frame"></iframe>
58
58
 
59
- <div id="content"><h1 class="noborder title">Documentation by YARD 0.8.6.2</h1>
59
+ <div id="content"><h1 class="noborder title">Documentation by YARD 0.8.7</h1>
60
60
  <div id="listing">
61
61
  <h1 class="alphaindex">Alphabetic Index</h1>
62
62
 
@@ -84,13 +84,6 @@
84
84
  <li class="letter">A</li>
85
85
  <ul>
86
86
 
87
- <li>
88
- <span class='object_link'><a href="Clavem/Exceptions/AuthorizationDenied.html" title="Clavem::Exceptions::AuthorizationDenied (class)">AuthorizationDenied</a></span>
89
-
90
- <small>(Clavem::Exceptions)</small>
91
-
92
- </li>
93
-
94
87
  <li>
95
88
  <span class='object_link'><a href="Clavem/Authorizer.html" title="Clavem::Authorizer (class)">Authorizer</a></span>
96
89
 
@@ -145,14 +138,14 @@
145
138
  </ul>
146
139
 
147
140
 
148
- <ul id="alpha_T" class="alpha">
149
- <li class="letter">T</li>
141
+ <ul id="alpha_S" class="alpha">
142
+ <li class="letter">S</li>
150
143
  <ul>
151
144
 
152
145
  <li>
153
- <span class='object_link'><a href="Clavem/Exceptions/Timeout.html" title="Clavem::Exceptions::Timeout (class)">Timeout</a></span>
146
+ <span class='object_link'><a href="Clavem/Server.html" title="Clavem::Server (class)">Server</a></span>
154
147
 
155
- <small>(Clavem::Exceptions)</small>
148
+ <small>(Clavem)</small>
156
149
 
157
150
  </li>
158
151
 
@@ -183,9 +176,9 @@
183
176
  </div>
184
177
 
185
178
  <div id="footer">
186
- Generated on Sat Jul 20 13:46:58 2013 by
179
+ Generated on Wed Aug 21 16:17:53 2013 by
187
180
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
188
- 0.8.6.2 (ruby-1.9.3).
181
+ 0.8.7 (ruby-2.0.0).
189
182
  </div>
190
183
 
191
184
  </body>
@@ -46,7 +46,7 @@
46
46
 
47
47
  <ul id="full_list" class="class">
48
48
  <li><span class='object_link'><a href="top-level-namespace.html" title="Top Level Namespace (root)">Top Level Namespace</a></span></li>
49
- <li><a class='toggle'></a> <span class='object_link'><a href="Clavem.html" title="Clavem (module)">Clavem</a></span><small class='search_info'>Top Level Namespace</small></li><ul><li><span class='object_link'><a href="Clavem/Authorizer.html" title="Clavem::Authorizer (class)">Authorizer</a></span> &lt; Object<small class='search_info'>Clavem</small></li><li><a class='toggle'></a> <span class='object_link'><a href="Clavem/Exceptions.html" title="Clavem::Exceptions (module)">Exceptions</a></span><small class='search_info'>Clavem</small></li><ul><li><span class='object_link'><a href="Clavem/Exceptions/AuthorizationDenied.html" title="Clavem::Exceptions::AuthorizationDenied (class)">AuthorizationDenied</a></span> &lt; RuntimeError<small class='search_info'>Clavem::Exceptions</small></li><li><span class='object_link'><a href="Clavem/Exceptions/Failure.html" title="Clavem::Exceptions::Failure (class)">Failure</a></span> &lt; Exception<small class='search_info'>Clavem::Exceptions</small></li><li><span class='object_link'><a href="Clavem/Exceptions/Timeout.html" title="Clavem::Exceptions::Timeout (class)">Timeout</a></span> &lt; Exception<small class='search_info'>Clavem::Exceptions</small></li></ul><li><span class='object_link'><a href="Clavem/Version.html" title="Clavem::Version (module)">Version</a></span><small class='search_info'>Clavem</small></li></ul>
49
+ <li><a class='toggle'></a> <span class='object_link'><a href="Clavem.html" title="Clavem (module)">Clavem</a></span><small class='search_info'>Top Level Namespace</small></li><ul><li><span class='object_link'><a href="Clavem/Authorizer.html" title="Clavem::Authorizer (class)">Authorizer</a></span> &lt; Object<small class='search_info'>Clavem</small></li><li><a class='toggle'></a> <span class='object_link'><a href="Clavem/Exceptions.html" title="Clavem::Exceptions (module)">Exceptions</a></span><small class='search_info'>Clavem</small></li><ul><li><span class='object_link'><a href="Clavem/Exceptions/Failure.html" title="Clavem::Exceptions::Failure (class)">Failure</a></span> &lt; StandardError<small class='search_info'>Clavem::Exceptions</small></li></ul><li><span class='object_link'><a href="Clavem/Server.html" title="Clavem::Server (class)">Server</a></span> &lt; Connection<small class='search_info'>Clavem</small></li><li><span class='object_link'><a href="Clavem/Version.html" title="Clavem::Version (module)">Version</a></span><small class='search_info'>Clavem</small></li></ul>
50
50
 
51
51
  </ul>
52
52
  </div>
@@ -2,11 +2,11 @@
2
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
3
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
4
  <head>
5
- <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII" />
5
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6
6
  <title>
7
7
  File: README
8
8
 
9
- &mdash; Documentation by YARD 0.8.6.2
9
+ &mdash; Documentation by YARD 0.8.7
10
10
 
11
11
  </title>
12
12
 
@@ -82,23 +82,24 @@
82
82
  <p>Simply instantiate the authorizer and run the authorize method with the URL:</p>
83
83
 
84
84
  <p>```ruby
85
- require &#8220;clavem&#8221;</p>
85
+ require clavem”</p>
86
86
 
87
87
  <h1 id="initialize-your-oauth-access">Initialize your oAuth access.</h1>
88
88
 
89
89
  <p>authorizer = Clavem::Authorizer.new</p>
90
90
 
91
- <h1 id="get-your-authorization-url-and-append-the-callback">Get your authorization URL and append the callback.</h1>
92
-
93
- <p>url += &#8220;?oauth_callback=#authorizerauthorizer.callback_url&#8221;
91
+ <h1 id="get-the-token">Get the token</h1>
92
+ <p># You can also handle callback parameter by yourself.
93
+ # url += “?oauth_callback=#authorizerauthorizer.callback_url
94
+ # authorizer.authorize(url, false)
94
95
  authorizer.authorize(url)</p>
95
96
 
96
- <p>if authorizer.status == :succeeded then
97
+ <p>if authorizer.succeeded? then
97
98
  access_token = authorizer.token</p>
98
99
 
99
100
  <p># Go on!
100
101
  else
101
- # Authorization denied
102
+ # Authorization denied or failed
102
103
  end
103
104
  ```</p>
104
105
 
@@ -106,29 +107,35 @@ end
106
107
 
107
108
  <p>See the <a href="http://rdoc.info/gems/clavem">documentation</a> for more information.</p>
108
109
 
110
+ <h2 id="use-on-jruby">Use on jRuby</h2>
111
+
112
+ <p>To use on jRuby, you need to install a gem with C extensions which must be compiled.</p>
113
+
114
+ <p>See jRuby documentation to see how to enabled extensions compilation.</p>
115
+
109
116
  <h2 id="contributing-to-clavem">Contributing to clavem</h2>
110
117
 
111
118
  <ul>
112
- <li>Check out the latest master to make sure the feature hasn&#8217;t been implemented or the bug hasn&#8217;t been fixed yet.</li>
113
- <li>Check out the issue tracker to make sure someone already hasn&#8217;t requested it and/or contributed it.</li>
119
+ <li>Check out the latest master to make sure the feature hasnt been implemented or the bug hasnt been fixed yet.</li>
120
+ <li>Check out the issue tracker to make sure someone already hasnt requested it and/or contributed it.</li>
114
121
  <li>Fork the project.</li>
115
122
  <li>Start a feature/bugfix branch.</li>
116
123
  <li>Commit and push until you are happy with your contribution.</li>
117
- <li>Make sure to add tests for it. This is important so I don&#8217;t break it in a future version unintentionally.</li>
124
+ <li>Make sure to add tests for it. This is important so I dont break it in a future version unintentionally.</li>
118
125
  <li>Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.</li>
119
126
  </ul>
120
127
 
121
128
  <h2 id="copyright">Copyright</h2>
122
129
 
123
- <p>Copyright (C) 2013 and above Shogun (shogun_panda@me.com).</p>
130
+ <p>Copyright (C) 2013 and above Shogun (shogun@cowtech.it).</p>
124
131
 
125
132
  <p>Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.</p>
126
133
  </div></div>
127
134
 
128
135
  <div id="footer">
129
- Generated on Sat Jul 20 13:46:58 2013 by
136
+ Generated on Wed Aug 21 16:17:53 2013 by
130
137
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
131
- 0.8.6.2 (ruby-1.9.3).
138
+ 0.8.7 (ruby-2.0.0).
132
139
  </div>
133
140
 
134
141
  </body>
@@ -4,7 +4,7 @@
4
4
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
5
  <head>
6
6
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
- <title>Documentation by YARD 0.8.6.2</title>
7
+ <title>Documentation by YARD 0.8.7</title>
8
8
  </head>
9
9
  <script type="text/javascript" charset="utf-8">
10
10
  window.onload = function() {
@@ -2,11 +2,11 @@
2
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
3
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
4
  <head>
5
- <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII" />
5
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6
6
  <title>
7
7
  File: README
8
8
 
9
- &mdash; Documentation by YARD 0.8.6.2
9
+ &mdash; Documentation by YARD 0.8.7
10
10
 
11
11
  </title>
12
12
 
@@ -82,23 +82,24 @@
82
82
  <p>Simply instantiate the authorizer and run the authorize method with the URL:</p>
83
83
 
84
84
  <p>```ruby
85
- require &#8220;clavem&#8221;</p>
85
+ require clavem”</p>
86
86
 
87
87
  <h1 id="initialize-your-oauth-access">Initialize your oAuth access.</h1>
88
88
 
89
89
  <p>authorizer = Clavem::Authorizer.new</p>
90
90
 
91
- <h1 id="get-your-authorization-url-and-append-the-callback">Get your authorization URL and append the callback.</h1>
92
-
93
- <p>url += &#8220;?oauth_callback=#authorizerauthorizer.callback_url&#8221;
91
+ <h1 id="get-the-token">Get the token</h1>
92
+ <p># You can also handle callback parameter by yourself.
93
+ # url += “?oauth_callback=#authorizerauthorizer.callback_url
94
+ # authorizer.authorize(url, false)
94
95
  authorizer.authorize(url)</p>
95
96
 
96
- <p>if authorizer.status == :succeeded then
97
+ <p>if authorizer.succeeded? then
97
98
  access_token = authorizer.token</p>
98
99
 
99
100
  <p># Go on!
100
101
  else
101
- # Authorization denied
102
+ # Authorization denied or failed
102
103
  end
103
104
  ```</p>
104
105
 
@@ -106,29 +107,35 @@ end
106
107
 
107
108
  <p>See the <a href="http://rdoc.info/gems/clavem">documentation</a> for more information.</p>
108
109
 
110
+ <h2 id="use-on-jruby">Use on jRuby</h2>
111
+
112
+ <p>To use on jRuby, you need to install a gem with C extensions which must be compiled.</p>
113
+
114
+ <p>See jRuby documentation to see how to enabled extensions compilation.</p>
115
+
109
116
  <h2 id="contributing-to-clavem">Contributing to clavem</h2>
110
117
 
111
118
  <ul>
112
- <li>Check out the latest master to make sure the feature hasn&#8217;t been implemented or the bug hasn&#8217;t been fixed yet.</li>
113
- <li>Check out the issue tracker to make sure someone already hasn&#8217;t requested it and/or contributed it.</li>
119
+ <li>Check out the latest master to make sure the feature hasnt been implemented or the bug hasnt been fixed yet.</li>
120
+ <li>Check out the issue tracker to make sure someone already hasnt requested it and/or contributed it.</li>
114
121
  <li>Fork the project.</li>
115
122
  <li>Start a feature/bugfix branch.</li>
116
123
  <li>Commit and push until you are happy with your contribution.</li>
117
- <li>Make sure to add tests for it. This is important so I don&#8217;t break it in a future version unintentionally.</li>
124
+ <li>Make sure to add tests for it. This is important so I dont break it in a future version unintentionally.</li>
118
125
  <li>Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.</li>
119
126
  </ul>
120
127
 
121
128
  <h2 id="copyright">Copyright</h2>
122
129
 
123
- <p>Copyright (C) 2013 and above Shogun (shogun_panda@me.com).</p>
130
+ <p>Copyright (C) 2013 and above Shogun (shogun@cowtech.it).</p>
124
131
 
125
132
  <p>Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.</p>
126
133
  </div></div>
127
134
 
128
135
  <div id="footer">
129
- Generated on Sat Jul 20 13:46:58 2013 by
136
+ Generated on Wed Aug 21 16:17:53 2013 by
130
137
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
131
- 0.8.6.2 (ruby-1.9.3).
138
+ 0.8.7 (ruby-2.0.0).
132
139
  </div>
133
140
 
134
141
  </body>
@@ -48,25 +48,37 @@
48
48
 
49
49
 
50
50
  <li class="r1 ">
51
- <span class='object_link'><a href="Clavem/Authorizer.html#authorize-instance_method" title="Clavem::Authorizer#authorize (method)">#authorize</a></span>
51
+ <span class='object_link'><a href="Clavem/Authorizer.html#%3Ai18n-instance_method" title="Clavem::Authorizer#:i18n (method)">#:i18n</a></span>
52
52
  <small>Clavem::Authorizer</small>
53
53
  </li>
54
54
 
55
55
 
56
56
  <li class="r2 ">
57
- <span class='object_link'><a href="Clavem/Authorizer.html#callback_url-instance_method" title="Clavem::Authorizer#callback_url (method)">#callback_url</a></span>
57
+ <span class='object_link'><a href="Clavem/Authorizer.html#authorize-instance_method" title="Clavem::Authorizer#authorize (method)">#authorize</a></span>
58
58
  <small>Clavem::Authorizer</small>
59
59
  </li>
60
60
 
61
61
 
62
62
  <li class="r1 ">
63
+ <span class='object_link'><a href="Clavem/Authorizer.html#callback_url-instance_method" title="Clavem::Authorizer#callback_url (method)">#callback_url</a></span>
64
+ <small>Clavem::Authorizer</small>
65
+ </li>
66
+
67
+
68
+ <li class="r2 ">
63
69
  <span class='object_link'><a href="Clavem/Authorizer.html#command-instance_method" title="Clavem::Authorizer#command (method)">#command</a></span>
64
70
  <small>Clavem::Authorizer</small>
65
71
  </li>
66
72
 
67
73
 
74
+ <li class="r1 ">
75
+ <span class='object_link'><a href="Clavem/Authorizer.html#denied%3F-instance_method" title="Clavem::Authorizer#denied? (method)">#denied?</a></span>
76
+ <small>Clavem::Authorizer</small>
77
+ </li>
78
+
79
+
68
80
  <li class="r2 ">
69
- <span class='object_link'><a href="Clavem/Authorizer.html#default_response_handler-instance_method" title="Clavem::Authorizer#default_response_handler (method)">#default_response_handler</a></span>
81
+ <span class='object_link'><a href="Clavem/Authorizer.html#failed%3F-instance_method" title="Clavem::Authorizer#failed? (method)">#failed?</a></span>
70
82
  <small>Clavem::Authorizer</small>
71
83
  </li>
72
84
 
@@ -84,25 +96,25 @@
84
96
 
85
97
 
86
98
  <li class="r1 ">
87
- <span class='object_link'><a href="Clavem/Authorizer.html#initialize-instance_method" title="Clavem::Authorizer#initialize (method)">#initialize</a></span>
88
- <small>Clavem::Authorizer</small>
99
+ <span class='object_link'><a href="Clavem/Server.html#initialize-instance_method" title="Clavem::Server#initialize (method)">#initialize</a></span>
100
+ <small>Clavem::Server</small>
89
101
  </li>
90
102
 
91
103
 
92
104
  <li class="r2 ">
93
- <span class='object_link'><a href="Clavem/Authorizer.html#instance-class_method" title="Clavem::Authorizer.instance (method)">instance</a></span>
105
+ <span class='object_link'><a href="Clavem/Authorizer.html#initialize-instance_method" title="Clavem::Authorizer#initialize (method)">#initialize</a></span>
94
106
  <small>Clavem::Authorizer</small>
95
107
  </li>
96
108
 
97
109
 
98
110
  <li class="r1 ">
99
- <span class='object_link'><a href="Clavem/Authorizer.html#localize-instance_method" title="Clavem::Authorizer#localize (method)">#localize</a></span>
111
+ <span class='object_link'><a href="Clavem/Authorizer.html#instance-class_method" title="Clavem::Authorizer.instance (method)">instance</a></span>
100
112
  <small>Clavem::Authorizer</small>
101
113
  </li>
102
114
 
103
115
 
104
116
  <li class="r2 ">
105
- <span class='object_link'><a href="Clavem/Authorizer.html#localizer-instance_method" title="Clavem::Authorizer#localizer (method)">#localizer</a></span>
117
+ <span class='object_link'><a href="Clavem/Authorizer.html#localize-instance_method" title="Clavem::Authorizer#localize (method)">#localize</a></span>
106
118
  <small>Clavem::Authorizer</small>
107
119
  </li>
108
120
 
@@ -114,31 +126,31 @@
114
126
 
115
127
 
116
128
  <li class="r2 ">
117
- <span class='object_link'><a href="Clavem/Authorizer.html#response_handler-instance_method" title="Clavem::Authorizer#response_handler (method)">#response_handler</a></span>
118
- <small>Clavem::Authorizer</small>
129
+ <span class='object_link'><a href="Clavem/Server.html#process_http_request-instance_method" title="Clavem::Server#process_http_request (method)">#process_http_request</a></span>
130
+ <small>Clavem::Server</small>
119
131
  </li>
120
132
 
121
133
 
122
134
  <li class="r1 ">
123
- <span class='object_link'><a href="Clavem/Authorizer.html#status-instance_method" title="Clavem::Authorizer#status (method)">#status</a></span>
135
+ <span class='object_link'><a href="Clavem/Authorizer.html#response_handler-instance_method" title="Clavem::Authorizer#response_handler (method)">#response_handler</a></span>
124
136
  <small>Clavem::Authorizer</small>
125
137
  </li>
126
138
 
127
139
 
128
140
  <li class="r2 ">
129
- <span class='object_link'><a href="Clavem/Authorizer.html#template-instance_method" title="Clavem::Authorizer#template (method)">#template</a></span>
141
+ <span class='object_link'><a href="Clavem/Authorizer.html#status-instance_method" title="Clavem::Authorizer#status (method)">#status</a></span>
130
142
  <small>Clavem::Authorizer</small>
131
143
  </li>
132
144
 
133
145
 
134
146
  <li class="r1 ">
135
- <span class='object_link'><a href="Clavem/Authorizer.html#timeout-instance_method" title="Clavem::Authorizer#timeout (method)">#timeout</a></span>
147
+ <span class='object_link'><a href="Clavem/Authorizer.html#succeeded%3F-instance_method" title="Clavem::Authorizer#succeeded? (method)">#succeeded?</a></span>
136
148
  <small>Clavem::Authorizer</small>
137
149
  </li>
138
150
 
139
151
 
140
152
  <li class="r2 ">
141
- <span class='object_link'><a href="Clavem/Authorizer.html#title-instance_method" title="Clavem::Authorizer#title (method)">#title</a></span>
153
+ <span class='object_link'><a href="Clavem/Authorizer.html#timeout-instance_method" title="Clavem::Authorizer#timeout (method)">#timeout</a></span>
142
154
  <small>Clavem::Authorizer</small>
143
155
  </li>
144
156
 
@@ -155,6 +167,12 @@
155
167
  </li>
156
168
 
157
169
 
170
+ <li class="r1 ">
171
+ <span class='object_link'><a href="Clavem/Authorizer.html#waiting%3F-instance_method" title="Clavem::Authorizer#waiting? (method)">#waiting?</a></span>
172
+ <small>Clavem::Authorizer</small>
173
+ </li>
174
+
175
+
158
176
  </ul>
159
177
  </div>
160
178
  </body>
@@ -6,7 +6,7 @@
6
6
  <title>
7
7
  Top Level Namespace
8
8
 
9
- &mdash; Documentation by YARD 0.8.6.2
9
+ &mdash; Documentation by YARD 0.8.7
10
10
 
11
11
  </title>
12
12
 
@@ -103,9 +103,9 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Sat Jul 20 13:46:58 2013 by
106
+ Generated on Wed Aug 21 16:17:53 2013 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
- 0.8.6.2 (ruby-1.9.3).
108
+ 0.8.7 (ruby-2.0.0).
109
109
  </div>
110
110
 
111
111
  </body>
@@ -1,13 +1,14 @@
1
1
  # encoding: utf-8
2
2
  #
3
- # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
3
+ # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
7
7
  require "r18n-desktop"
8
8
  require "lazier"
9
- require "webrick"
10
- require "erb"
9
+ require "eventmachine"
10
+ require "evma_httpserver"
11
11
 
12
12
  require "clavem/version" if !defined?(Clavem::Version)
13
+ require "clavem/server"
13
14
  require "clavem/authorizer"
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  #
3
- # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
3
+ # This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
@@ -11,15 +11,7 @@ module Clavem
11
11
  # Exceptions used by {Authorizer Authorizer}.
12
12
  module Exceptions
13
13
  # This exception is raised whether an error occurs.
14
- class Failure < ::Exception
15
- end
16
-
17
- # This exception is raised if the timeout expired.
18
- class Timeout < ::Exception
19
- end
20
-
21
- # This exception is raised if the authorization was denied.
22
- class AuthorizationDenied < ::RuntimeError
14
+ class Failure < ::StandardError
23
15
  end
24
16
  end
25
17
 
@@ -30,22 +22,18 @@ module Clavem
30
22
  # @attribute host
31
23
  # @return [String] The host address on which listening for replies. Default is `localhost`.
32
24
  # @attribute port
33
- # @return [Fixnum] The port on which listening for replies. Default is `2501`.
25
+ # @return [Fixnum] The port on which listening for replies. Default is `7772`.
34
26
  # @attribute command
35
27
  # @return [String] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
36
- # @attribute title
37
- # @return [String] The title for response template. Default is `Clavem Authorization`.
38
- # @attribute template
39
- # @return [String] Alternative template to show progress in user's browser.
40
28
  # @attribute timeout
41
- # @return [Fixnum] The amount of milliseconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
29
+ # @return [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
42
30
  # @attribute response_handler
43
- # @return [Proc] A Ruby block to handle response and check for success. @see {#default_response_handler}.
31
+ # @return [Proc] A Ruby block to handle response and check for success. The block must accept a querystring hash (which all values are arrays) and return a token or `nil` if the authentication was denied.
44
32
  # @attribute token
45
33
  # @return [String] The token obtained by the remote endpoint.
46
- # @attribute token
47
- # @return [Symbol] The status of the request. Can be `:success`, `:denied`, `:failure` and `:waiting`.
48
- # @attribute localizer
34
+ # @attribute status
35
+ # @return [Symbol] The status of the request. Can be `:succeeded`, `:denied`, `:failed` and `:waiting`.
36
+ # @attribute :i18n
49
37
  # @return [R18N::Translation] A localizer object.
50
38
  class Authorizer
51
39
  include R18n::Helpers
@@ -53,8 +41,6 @@ module Clavem
53
41
  attr_accessor :host
54
42
  attr_accessor :port
55
43
  attr_accessor :command
56
- attr_accessor :title
57
- attr_accessor :template
58
44
  attr_accessor :timeout
59
45
  attr_accessor :response_handler
60
46
  attr_accessor :token
@@ -64,83 +50,60 @@ module Clavem
64
50
  # Returns a unique (singleton) instance of the authorizer.
65
51
  #
66
52
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
67
- # @param port [Fixnum] The port on which listening for replies. Default is `2501`.
53
+ # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
68
54
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
69
- # @param title [String|nil] The title for response template. Default is `Clavem Authorization`
70
- # @param template [String|nil] Alternative template to show progress in user's browser.
71
- # @param timeout [Fixnum] The amount of milliseconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
72
- # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#default_response_handler}.
55
+ # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
56
+ # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
73
57
  # @param force [Boolean] If to force recreation of the instance.
74
58
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
75
- def self.instance(host = "localhost", port = 2501, command = nil, title = nil, template = nil, timeout = 0, force = false, &response_handler)
59
+ def self.instance(host = "localhost", port = 7772, command = nil, timeout = 0, force = false, &response_handler)
76
60
  @instance = nil if force
77
- @instance ||= Clavem::Authorizer.new(host, port, command, title, template, timeout, &response_handler)
61
+ @instance ||= Clavem::Authorizer.new(host, port, command, timeout, &response_handler)
78
62
  @instance
79
63
  end
80
64
 
81
65
  # Creates a new authorizer.
82
66
  #
83
67
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
84
- # @param port [Fixnum] The port on which listening for replies. Default is `2501`.
68
+ # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
85
69
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
86
- # @param title [String|nil] The title for response template. Default is `Clavem Authorization`
87
- # @param template [String|nil] Alternative template to show progress in user's browser.
88
- # @param timeout [Fixnum] The amount of milliseconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
89
- # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#default_response_handler}.
70
+ # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
71
+ # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
90
72
  # @return [Authorizer] The new authorizer.
91
- def initialize(host = "localhost", port = 2501, command = nil, title = nil, template = nil, timeout = 0, &response_handler)
73
+ def initialize(host = "localhost", port = 7772, command = nil, timeout = 0, &response_handler)
92
74
  @i18n = self.localize
93
-
94
75
  @host = host.ensure_string
95
76
  @port = port.to_integer
96
77
  @command = command.ensure_string
97
- @title = title.ensure_string
98
- @template = template.ensure_string
99
78
  @timeout = timeout.to_integer
100
79
  @response_handler = response_handler
101
-
102
- sanitize_arguments
103
-
104
80
  @token = nil
105
81
  @status = :waiting
106
- @compiled_template ||= ::ERB.new(@template)
107
- @timeout_expired = false
108
- @timeout_thread = nil
109
82
 
83
+ sanitize_arguments
110
84
  self
111
85
  end
112
86
 
113
87
  # Starts the authorization flow.
114
88
  #
115
89
  # @param url [String] The URL where to send the user to start authorization.
116
- # @return [Authorizer] The authorizer.
117
- def authorize(url)
90
+ # @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
91
+ # @return [Boolean] `true` if authorization succeeded, `false` otherwise.
92
+ def authorize(url, append_callback = true)
93
+ url += "#{url.index("?") ? "&" : "?"}oauth_callback=#{callback_url}" if append_callback
118
94
  @url = url
119
95
  @status = :waiting
96
+ @token = nil
120
97
 
121
98
  begin
122
- # Setup stuff
123
- setup_webserver
124
- setup_interruptions_handling
125
- setup_timeout_handling
126
-
127
- # Open the endpoint then start the server
128
- open_endpoint
129
- @server.start
130
-
131
- raise Clavem::Exceptions::Timeout.new if @timeout_expired
132
- rescue Clavem::Exceptions::Timeout => t
133
- @status = :failure
134
- raise t
99
+ perform_request
100
+ process_response
135
101
  rescue => e
136
- @status = :failure
102
+ @status = :failed
137
103
  raise Clavem::Exceptions::Failure.new(@i18n.errors.response_failure(e.to_s))
138
- ensure
139
- cleanup
140
104
  end
141
105
 
142
- raise Clavem::Exceptions::AuthorizationDenied.new if @status == :denied
143
- self
106
+ succeeded?
144
107
  end
145
108
 
146
109
  # Returns the callback_url for this authorizer.
@@ -150,14 +113,10 @@ module Clavem
150
113
  "http://#{host}:#{port}/"
151
114
  end
152
115
 
153
- # Handles a response from the remote endpoint.
116
+ # Returns the response handler for the authorizer.
154
117
  #
155
- # @param [Authorizer] authorizer The current authorizer.
156
- # @param [WEBrick::HTTPRequest] request The request that the remote endpoint made to notify authorization status.
157
- # @param [WEBrick::HTTPResponse] response The request to send to the browser.
158
- # @return [String|nil] The oAuth access token. Returning `nil` means *authorization denied*.
159
- def default_response_handler(authorizer, request, response)
160
- request.query['oauth_token'].ensure_string
118
+ def response_handler
119
+ @response_handler || Proc.new {|querystring| (querystring || {}).fetch("oauth_token", []).ensure_array.first }
161
120
  end
162
121
 
163
122
  # Set the current locale for messages.
@@ -169,19 +128,45 @@ module Clavem
169
128
  R18n::I18n.new([locale || :en, ENV["LANG"], R18n::I18n.system_locale].compact, @i18n_locales_path).t.clavem
170
129
  end
171
130
 
131
+ # Checks if authentication succeeded.
132
+ #
133
+ # @return [Boolean] `true` if authorization succeeded, `false otherwise`.
134
+ def succeeded?
135
+ @status == :succeeded
136
+ end
137
+
138
+ # Checks if authentication was denied.
139
+ #
140
+ # @return [Boolean] `true` if authorization was denied, `false otherwise`.
141
+ def denied?
142
+ @status == :denied
143
+ end
144
+
145
+ # Checks if authentication failed (which means that some error occurred).
146
+ #
147
+ # @return [Boolean] `true` if authorization failed, `false otherwise`.
148
+ def failed?
149
+ @status == :failed
150
+ end
151
+
152
+ # Checks if authentication is still pending.
153
+ #
154
+ # @return [Boolean] `true` if authorization is still pending, `false otherwise`.
155
+ def waiting?
156
+ @status == :waiting
157
+ end
158
+
172
159
  private
173
160
  # sanitize_arguments
174
161
  def sanitize_arguments
175
162
  @host = "localhost" if @host.blank?
176
- @port = 2501 if @port.to_integer < 1
163
+ @port = 7772 if @port.to_integer < 1
177
164
  @command = "open \"{{URL}}\"" if @command.blank?
178
- @title = @i18n.default_title if @title.blank?
179
- @template = File.read(File.dirname(__FILE__) + "/template.html.erb") if @template.blank?
180
165
  @timeout = 0 if @timeout < 0
181
166
  end
182
167
 
183
- # Open the remote endpoint
184
- def open_endpoint
168
+ # Performs the authentication request.
169
+ def perform_request
185
170
  # Open the oAuth endpoint into the browser
186
171
  begin
187
172
  Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
@@ -190,54 +175,23 @@ module Clavem
190
175
  end
191
176
  end
192
177
 
193
- # Handle interruptions for the process.
194
- def setup_interruptions_handling
195
- ["USR2", "INT", "TERM", "KILL"].each {|signal| Kernel.trap(signal){ @server.shutdown if @server } }
196
- end
197
-
198
- # Handle timeout for the response.
199
- def setup_timeout_handling
200
- if @timeout > 0 then
201
- @timeout_thread = Thread.new do
202
- Kernel.sleep(@timeout.to_f / 1000)
203
- @timeout_expired = true
204
- Process.kill("USR2", 0)
205
- end
206
- end
207
- end
208
-
209
- # Prepare the webserver for handling the response.
210
- def setup_webserver
211
- @server = ::WEBrick::HTTPServer.new(BindAddress: @host, Port: @port, Logger: WEBrick::Log.new("/dev/null"), AccessLog: [nil, nil])
212
- @server.mount_proc("/"){ |request, response| dispatch_request(request, response) }
213
- end
214
-
215
- # Handles a response from the remote endpoint.
216
- #
217
- # @param [WEBrick::HTTPRequest] request The request that the remote endpoint made to notify authorization status.
218
- # @param [WEBrick::HTTPResponse] response The request to send to the browser.
219
- def dispatch_request(request, response)
220
- @token = @response_handler ? @response_handler.call(self, request, response) : default_response_handler(self, request, response)
221
-
222
- if @status == :waiting then
223
- if @token.present? then
224
- @status = :success
225
- response.status = 200
226
- else
227
- @status = :denied
228
- response.status = 403
229
- end
230
-
231
- response.body = @compiled_template.result(binding)
232
- @server.shutdown
178
+ # Processes the authentication response.
179
+ def process_response
180
+ # Signal handling
181
+ ["INT", "TERM", "KILL"].each {|signal|
182
+ Kernel.trap(signal){ EM.stop }
183
+ }
184
+
185
+ # Start eventmachine
186
+ EM.run do
187
+ EM.add_timer(@timeout){ handle_timeout } if @timeout > 0
188
+ EM.start_server(@host, @port, Clavem::Server, self)
233
189
  end
234
190
  end
235
191
 
236
- # Cleans up resources
237
- def cleanup
238
- @timeout_thread.exit if @timeout_thread
239
- @server.shutdown if @server
240
- ["USR2", "INT", "TERM", "KILL"].each {|signal| Kernel.trap(signal, "DEFAULT") }
192
+ # Handle timeouts.
193
+ def handle_timeout
194
+ EM.stop
241
195
  end
242
196
  end
243
197
  end