restify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d43e9fc7edcf380453e49cb076f14b118370e09
4
+ data.tar.gz: bcbc69d8dab7eff64f174f3beadccd68fa64c19e
5
+ SHA512:
6
+ metadata.gz: 27b2c9babe03123f84e6da7ef102c9e07231862a28ee6bd25f76bba6a8a8417af1cb58a81486c7652239f2ae1ca6b5ab03c93593ae2bd022c75b2dfb05d0532e
7
+ data.tar.gz: 3537927275ddbe60a53a610dc30573f4879d5a9b1784c31f8db844bc8a972acaeaa67ac6ac9b21701c1f418c2b773cb650177a87e11be3cf5c6c977172af6024
data/LICENSE.txt ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # Restify
2
+
3
+ Restify is an experimental hypermedia REST client that uses parallel, keep-alive and pipelined requests by default.
4
+
5
+ Restify scans Link headers and returned resource for links and relations to other resources, represented as RFC6570 URI Templates, and exposes those to the developer.
6
+
7
+ Restify can be used to consume hypermedia REST APIs (like GitHubs), to build a site-specific library or to use within your own backend services.
8
+
9
+ Restify is build upon
10
+
11
+ * [obligation](https://github.com/jgraichen/obligation)
12
+ * [addressable](https://github.com/sporkmonger/addressable)
13
+
14
+ provided HTTP adapters for
15
+
16
+ * [em-http-request](https://github.com/igrigorik/em-http-request)
17
+
18
+ and can decode and encode
19
+
20
+ * [JSON](https://github.com/intridea/multi_json)
21
+
22
+ ## Restify is still pre-alpha!
23
+
24
+ * It does not cover all HTTP methods (not even all basic ones)
25
+ * It is build on pre-alpha obligation library.
26
+ * It has to less tests.
27
+
28
+ Planned features:
29
+
30
+ * API versions via header/URI
31
+ * Content-Type and Language negotiation
32
+ * Encode and decode MessagePack
33
+ * ZeroMQ FLP backend
34
+ * Eventmachine integration (see obligation library)
35
+ * Alternative HTTP backends
36
+
37
+ ## Installation
38
+
39
+ Add it to your Gemfile or install it manually: `$ gem install restify`
40
+
41
+ ## Usage
42
+
43
+ Create new Restify object (actually returns '/' resource):
44
+
45
+ ```ruby
46
+ client = Restify.new('http://api.github.com').value
47
+ ```
48
+
49
+ The `value` call resolves the returned `Obligation` (like a Future object) by blocking the thread until the resource is there.
50
+
51
+ Get a relation described by the root resource. Restify supports Link headers as well as JSON encoded relations (`*_url` fields).
52
+
53
+ ```ruby
54
+ repositories = gh.rel(:repository)
55
+ ```
56
+
57
+ Send a GET request for a specific repository using given parameters. They will be used to expand the URI template behind the `repositories` relation.
58
+
59
+ ```ruby
60
+ repo = repositories.get(owner: 'jgraichen', repo: 'restify').value
61
+ ```
62
+
63
+ Now fetch a list of commits for this repo and get this first one:
64
+
65
+ ```ruby
66
+ commit = repo.rel(:commits).get.value.first
67
+ ```
68
+
69
+ And print it:
70
+
71
+ ```ruby
72
+ puts "Last commit: #{commit[:sha]}"
73
+ puts "By #{commit[:commit][:author][:name]} <#{commit[:commit][:author][:email]}>"
74
+ puts "#{commit[:commit][:message]}"
75
+ ```
76
+
77
+ See commented example in main spec [`spec/restify_spec.rb`](https://github.com/jgraichen/restify/blob/master/spec/restify_spec.rb#L100) or in the `examples` directory.
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it ( http://github.com/jgraichen/restify/fork )
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit specs for your feature so that I do not break it later
84
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 5. Push to the branch (`git push origin my-new-feature`)
86
+ 6. Create new Pull Request
87
+
88
+ ## License
89
+
90
+ Copyright (C) 2014 Jan Graichen
91
+
92
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
93
+
94
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
95
+
96
+ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -0,0 +1,107 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6
+ <title>
7
+ File: README
8
+
9
+ &mdash; Documentation by YARD 0.8.7.4
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ hasFrames = window.top.frames.main ? true : false;
19
+ relpath = '';
20
+ framesUrl = "frames.html#!file.README.html";
21
+ </script>
22
+
23
+
24
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
25
+
26
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
27
+
28
+
29
+ </head>
30
+ <body>
31
+ <div id="header">
32
+ <div id="menu">
33
+
34
+ <a href="_index.html">Index</a> &raquo;
35
+ <span class="title">File: README</span>
36
+
37
+
38
+ <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
39
+ </div>
40
+
41
+ <div id="search">
42
+
43
+ <a class="full_list_link" id="class_list_link"
44
+ href="class_list.html">
45
+ Class List
46
+ </a>
47
+
48
+ <a class="full_list_link" id="method_list_link"
49
+ href="method_list.html">
50
+ Method List
51
+ </a>
52
+
53
+ <a class="full_list_link" id="file_list_link"
54
+ href="file_list.html">
55
+ File List
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <iframe id="search_frame"></iframe>
63
+
64
+ <div id="content"><div id='filecontents'><h1>Restify</h1>
65
+
66
+ <p>TODO: Write a gem description</p>
67
+
68
+ <h2>Installation</h2>
69
+
70
+ <p>Add this line to your application&#39;s Gemfile:</p>
71
+
72
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_gem'>gem</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>restify</span><span class='tstring_end'>&#39;</span></span>
73
+ </code></pre>
74
+
75
+ <p>And then execute:</p>
76
+
77
+ <pre class="code ruby"><code class="ruby">$ bundle
78
+ </code></pre>
79
+
80
+ <p>Or install it yourself as:</p>
81
+
82
+ <pre class="code ruby"><code class="ruby">$ gem install restify
83
+ </code></pre>
84
+
85
+ <h2>Usage</h2>
86
+
87
+ <p>TODO: Write usage instructions here</p>
88
+
89
+ <h2>Contributing</h2>
90
+
91
+ <ol>
92
+ <li>Fork it ( <a href="http://github.com/">http://github.com/</a><my-github-username>/restify/fork )</li>
93
+ <li>Create your feature branch (<code>git checkout -b my-new-feature</code>)</li>
94
+ <li>Commit your changes (<code>git commit -am &#39;Add some feature&#39;</code>)</li>
95
+ <li>Push to the branch (<code>git push origin my-new-feature</code>)</li>
96
+ <li>Create new Pull Request</li>
97
+ </ol>
98
+ </div></div>
99
+
100
+ <div id="footer">
101
+ Generated on Wed May 7 13:45:16 2014 by
102
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
103
+ 0.8.7.4 (ruby-2.1.1).
104
+ </div>
105
+
106
+ </body>
107
+ </html>
data/lib/restify.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'restify/version'
2
+ require 'addressable/uri'
3
+ require 'addressable/template'
4
+ require 'active_support'
5
+ require 'active_support/core_ext'
6
+ require 'active_support/hash_with_indifferent_access'
7
+ require 'active_support/core_ext/module/delegation'
8
+ require 'multi_json'
9
+ require 'obligation'
10
+
11
+ #
12
+ module Restify
13
+ require 'restify/adapter'
14
+ require 'restify/client'
15
+ require 'restify/relations'
16
+ require 'restify/collection'
17
+ require 'restify/link'
18
+ require 'restify/relation'
19
+ require 'restify/request'
20
+ require 'restify/resource'
21
+ require 'restify/response'
22
+
23
+ class << self
24
+ def new(url)
25
+ Client.new(url).request(:GET, nil)
26
+ end
27
+
28
+ def adapter
29
+ @adapter ||= Adapter::EM.new
30
+ end
31
+
32
+ def decode(response)
33
+ decoder = decoders.find { |d| d.accept?(response) }
34
+ if decoder
35
+ decoder.decode(response)
36
+ else
37
+ UnsupportedFormatError.new \
38
+ "Cannot decode `#{response.content_type}' response."
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,74 @@
1
+ require 'eventmachine'
2
+ require 'em-http-request'
3
+
4
+ module Restify
5
+ #
6
+ module Adapter
7
+ #
8
+ class EM
9
+
10
+ def call(request)
11
+ Obligation.create do |w|
12
+ next_tick do
13
+ conn = ConnectionPool.get(request.uri)
14
+ req = conn.send request.method.downcase,
15
+ keepalive: true,
16
+ redirects: 3,
17
+ path: request.uri.normalized_path,
18
+ query: request.uri.normalized_query,
19
+ body: request.body,
20
+ head: request.headers
21
+
22
+ req.callback do
23
+ w.fulfill Response.new(
24
+ request,
25
+ req.response_header.status,
26
+ req.response_header,
27
+ req.response
28
+ )
29
+ end
30
+
31
+ req.errback do
32
+ w.reject RuntimeError.new
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def next_tick(&block)
41
+ ensure_running
42
+ EventMachine.next_tick(&block)
43
+ end
44
+
45
+ def ensure_running
46
+ Thread.new do
47
+ begin
48
+ EventMachine.run {}
49
+ rescue => e
50
+ puts "Resitfy::Adapter::EM -> #{e}\n#{e.backtrace.join("\n")}"
51
+ raise e
52
+ end
53
+ end unless EventMachine.reactor_running?
54
+ end
55
+
56
+ #
57
+ class ConnectionPool
58
+ class << self
59
+ #
60
+ def get(uri)
61
+ connections[uri.origin] ||= begin
62
+ EventMachine::HttpRequest.new(uri.origin)
63
+ end
64
+ end
65
+
66
+ #
67
+ def connections
68
+ @connections ||= {}
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end