ahab 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +8 -0
- data/LICENSE +176 -0
- data/README.md +52 -0
- data/bin/ahab +5 -0
- data/features/cli.feature +78 -0
- data/features/step_definitions/cli_steps.rb +13 -0
- data/features/support/env.rb +7 -0
- data/features/support/webmock.rb +2 -0
- data/lib/ahab.rb +2 -0
- data/lib/ahab/asset.rb +76 -0
- data/lib/ahab/cli.rb +39 -0
- data/lib/ahab/configuration.rb +22 -0
- data/lib/ahab/fetch_task.rb +58 -0
- data/lib/ahab/fetcher.rb +73 -0
- metadata +164 -0
data/HISTORY.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# AHAB Client
|
2
|
+
|
3
|
+
Client Library for the AHAB asset packaging system: [ahab.io](http://ahab.io).
|
4
|
+
|
5
|
+
## Includes
|
6
|
+
|
7
|
+
* Command-Line Interface
|
8
|
+
* `Ahab::Client` Ruby API
|
9
|
+
* `Ahab::Task` Rake task to fetch assets
|
10
|
+
|
11
|
+
## Will Include
|
12
|
+
|
13
|
+
* `Ahab::Guard` Guard recipe to fetch assets
|
14
|
+
* Asset registries
|
15
|
+
* Optimistic version matching
|
16
|
+
|
17
|
+
## ahab.js
|
18
|
+
|
19
|
+
Ahab depends on an `ahab.json` file in your project's root directory. Its
|
20
|
+
structure looks like this:
|
21
|
+
|
22
|
+
```json
|
23
|
+
{
|
24
|
+
"assets": [
|
25
|
+
{
|
26
|
+
"url": "http://cdnjs.cloudflare.com/ajax/libs/960gs/0/960.css"
|
27
|
+
},
|
28
|
+
{
|
29
|
+
"url": "http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.js",
|
30
|
+
"filename": "jQuery.js"
|
31
|
+
}
|
32
|
+
]
|
33
|
+
}
|
34
|
+
```
|
35
|
+
|
36
|
+
## Command-Line Interface
|
37
|
+
|
38
|
+
Run `ahab fetch` once you have your `ahab.json` set up.
|
39
|
+
|
40
|
+
## Rake Task
|
41
|
+
|
42
|
+
Usage:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
namespace :assets do
|
46
|
+
Ahab::FetchTask.new(:fetch)
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
```bash
|
51
|
+
rake assets:fetch
|
52
|
+
```
|
data/bin/ahab
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
@client
|
3
|
+
Feature: AHAB Command-Line Interface
|
4
|
+
|
5
|
+
Scenario: No ahab.json
|
6
|
+
Given the file "ahab.json" does not exist
|
7
|
+
When I run `ahab fetch`
|
8
|
+
Then the exit status should be 1
|
9
|
+
And the output should contain "Could not find ahab.json"
|
10
|
+
|
11
|
+
Scenario: Empty ahab.json
|
12
|
+
Given a file named "ahab.json" with:
|
13
|
+
"""
|
14
|
+
{}
|
15
|
+
"""
|
16
|
+
When I run `ahab fetch`
|
17
|
+
Then the exit status should be 0
|
18
|
+
And the output should contain "Fetched 0 assets"
|
19
|
+
|
20
|
+
Scenario: Fetch asset via URL; infer filename
|
21
|
+
Given a file named "ahab.json" with:
|
22
|
+
"""
|
23
|
+
{ "assets": [ { "url": "http://example.com/my/asset.txt" } ] }
|
24
|
+
"""
|
25
|
+
And the URL "http://example.com/my/asset.txt" returns:
|
26
|
+
"""
|
27
|
+
content from URL
|
28
|
+
"""
|
29
|
+
When I run `ahab fetch`
|
30
|
+
Then the exit status should be 0
|
31
|
+
And the file "vendor/assets/asset.txt" should contain:
|
32
|
+
"""
|
33
|
+
content from URL
|
34
|
+
"""
|
35
|
+
And the output should contain "Fetched 1 asset"
|
36
|
+
|
37
|
+
Scenario: Fetch asset via URL; specify filename
|
38
|
+
Given a file named "ahab.json" with:
|
39
|
+
"""
|
40
|
+
{
|
41
|
+
"assets": [
|
42
|
+
{
|
43
|
+
"url": "http://example.com/my/asset.txt",
|
44
|
+
"filename": "my-asset.txt"
|
45
|
+
}
|
46
|
+
]
|
47
|
+
}
|
48
|
+
"""
|
49
|
+
And the URL "http://example.com/my/asset.txt" returns:
|
50
|
+
"""
|
51
|
+
content from URL
|
52
|
+
"""
|
53
|
+
When I run `ahab fetch`
|
54
|
+
Then the exit status should be 0
|
55
|
+
And the file "vendor/assets/my-asset.txt" should contain:
|
56
|
+
"""
|
57
|
+
content from URL
|
58
|
+
"""
|
59
|
+
And the output should contain "Fetched 1 asset"
|
60
|
+
|
61
|
+
Scenario: Fetch asset via URL; cannot infer filename
|
62
|
+
Given a file named "ahab.json" with:
|
63
|
+
"""
|
64
|
+
{ "assets": [ { "url": "http://example.com" } ] }
|
65
|
+
"""
|
66
|
+
When I run `ahab fetch`
|
67
|
+
Then the exit status should be 1
|
68
|
+
And the output should contain "Cannot infer filename from http://example.com"
|
69
|
+
|
70
|
+
Scenario: Error fetching from URL
|
71
|
+
Given a file named "ahab.json" with:
|
72
|
+
"""
|
73
|
+
{ "assets": [ { "url": "http://example.com/my/asset.txt" } ] }
|
74
|
+
"""
|
75
|
+
And the URL "http://example.com/my/asset.txt" returns a 404 error
|
76
|
+
When I run `ahab fetch`
|
77
|
+
Then the exit status should be 1
|
78
|
+
And the output should contain "Error fetching http://example.com/my/asset.txt"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Given /^the file "(.+)" does not exist$/ do |file_name|
|
2
|
+
in_current_dir do
|
3
|
+
FileUtils.rm_f file_name
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
Given /^the URL "(.+)" returns:$/ do |url, content|
|
8
|
+
stub_request(:get, url).with(body: content)
|
9
|
+
end
|
10
|
+
|
11
|
+
Given /^the URL "(.+)" returns a 404 error$/ do |url|
|
12
|
+
stub_request(:get, url).with(status: 404)
|
13
|
+
end
|
data/lib/ahab.rb
ADDED
data/lib/ahab/asset.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'uri'
|
3
|
+
require 'ahab'
|
4
|
+
|
5
|
+
module Ahab
|
6
|
+
|
7
|
+
class NoAssetURL < StandardError
|
8
|
+
def initialize(asset)
|
9
|
+
super("No URL in asset #{asset}")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class InvalidAssetURL < StandardError
|
14
|
+
def initialize(url)
|
15
|
+
super("#{url} is not a valid URL")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class CannotInferFilename < StandardError
|
20
|
+
def initialize(url)
|
21
|
+
super("Cannot infer filename from #{url}")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Asset
|
26
|
+
|
27
|
+
attr_reader :filename, :full_path, :status
|
28
|
+
|
29
|
+
def initialize(hash)
|
30
|
+
@uri = parse_uri(hash['url'])
|
31
|
+
@filename = hash.fetch('filename') { infer_filename(@uri) }
|
32
|
+
# TODO: inject this:
|
33
|
+
@destination_directory = 'vendor/assets'
|
34
|
+
@full_path = File.join(@destination_directory, @filename)
|
35
|
+
@status = :unfetched
|
36
|
+
end
|
37
|
+
|
38
|
+
def url
|
39
|
+
@uri.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
def write!(content)
|
43
|
+
FileUtils.mkdir_p @destination_directory
|
44
|
+
File.write @full_path, content
|
45
|
+
@status = :fetched
|
46
|
+
end
|
47
|
+
|
48
|
+
def error!
|
49
|
+
@status = :error
|
50
|
+
end
|
51
|
+
|
52
|
+
def success?
|
53
|
+
@status == :fetched
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def destination_directory
|
59
|
+
# todo: pass this in from the configuration
|
60
|
+
'vendor/assets'
|
61
|
+
end
|
62
|
+
|
63
|
+
def parse_uri(url)
|
64
|
+
raise NoAssetURL.new(self) unless url
|
65
|
+
URI.parse(url).tap do |uri|
|
66
|
+
raise InvalidAssetURL.new(url) unless uri.is_a?(URI::HTTP)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def infer_filename(uri)
|
71
|
+
uri.path.split('/').last or raise CannotInferFilename.new(uri)
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/lib/ahab/cli.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'ahab/configuration'
|
3
|
+
require 'ahab/fetcher'
|
4
|
+
|
5
|
+
module Ahab
|
6
|
+
class CLI < Thor
|
7
|
+
|
8
|
+
include Thor::Actions
|
9
|
+
|
10
|
+
desc 'fetch', 'fetch assets'
|
11
|
+
def fetch
|
12
|
+
summary = Ahab::Fetcher.new(load_config).fetch do |a|
|
13
|
+
if a.success?
|
14
|
+
say_status 'fetch', "Wrote #{a.filename}"
|
15
|
+
else
|
16
|
+
say_status 'fetch', "Error fetching #{a.url}", :red
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
say_status 'fetch', summary.to_s, summary.success? ? :green : :red
|
21
|
+
|
22
|
+
exit 1 unless summary.success?
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def load_config
|
28
|
+
path = 'ahab.json'
|
29
|
+
|
30
|
+
unless File.exists?(path)
|
31
|
+
say_status 'fetch', 'Could not find ahab.json', :red
|
32
|
+
exit 1
|
33
|
+
end
|
34
|
+
|
35
|
+
Ahab::Configuration.from_file path
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'ahab/asset'
|
2
|
+
require 'multi_json'
|
3
|
+
|
4
|
+
module Ahab
|
5
|
+
|
6
|
+
class Configuration
|
7
|
+
|
8
|
+
def self.from_file(path)
|
9
|
+
new MultiJson.decode(File.read(path))
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :assets
|
13
|
+
|
14
|
+
def initialize(hash)
|
15
|
+
@assets = hash.fetch('assets', []).map do |h|
|
16
|
+
Ahab::Asset.new(h)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
require 'rake/tasklib'
|
3
|
+
require 'ahab'
|
4
|
+
require 'ahab/configuration'
|
5
|
+
require 'ahab/fetcher'
|
6
|
+
|
7
|
+
module Ahab
|
8
|
+
|
9
|
+
class FetchTask < Rake::TaskLib
|
10
|
+
|
11
|
+
attr_accessor :config_path
|
12
|
+
|
13
|
+
def initialize(name = :fetch_assets)
|
14
|
+
@name = name
|
15
|
+
@config_path = 'ahab.json'
|
16
|
+
yield self if block_given?
|
17
|
+
define
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def define
|
23
|
+
task @name => asset_tasks(Ahab::Configuration.from_file(config_path))
|
24
|
+
end
|
25
|
+
|
26
|
+
def asset_tasks(configuration)
|
27
|
+
perform_task = perform_fetch_task(Ahab::Fetcher.new(configuration))
|
28
|
+
|
29
|
+
configuration.assets.map do |asset|
|
30
|
+
file asset.full_path => config_path do
|
31
|
+
perform_task.invoke
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def perform_fetch_task(fetcher)
|
37
|
+
task(:__perform_fetch) do
|
38
|
+
summary = fetcher.fetch do |asset|
|
39
|
+
message = if asset.success?
|
40
|
+
"Fetched #{asset.full_path}"
|
41
|
+
else
|
42
|
+
"Error fetching from #{asset.url}"
|
43
|
+
end
|
44
|
+
|
45
|
+
puts message
|
46
|
+
end
|
47
|
+
|
48
|
+
if summary.success?
|
49
|
+
puts summary.to_s
|
50
|
+
else
|
51
|
+
abort summary.to_s
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/lib/ahab/fetcher.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'typhoeus'
|
2
|
+
require 'ahab'
|
3
|
+
|
4
|
+
module Ahab
|
5
|
+
|
6
|
+
class Fetcher
|
7
|
+
|
8
|
+
def initialize(configuration)
|
9
|
+
@configuration = configuration
|
10
|
+
end
|
11
|
+
|
12
|
+
def fetch(&on_fetched)
|
13
|
+
fetch_all @configuration.assets, &on_fetched
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def client
|
19
|
+
@client ||= Typhoeus::Hydra.hydra
|
20
|
+
end
|
21
|
+
|
22
|
+
def fetch_all(assets, &on_fetched)
|
23
|
+
requests(assets, &on_fetched).each { |request| client.queue request }
|
24
|
+
client.run
|
25
|
+
FetchSummary.new(assets)
|
26
|
+
end
|
27
|
+
|
28
|
+
def requests(assets, &on_fetched)
|
29
|
+
assets.map do |asset|
|
30
|
+
Typhoeus::Request.new(asset.url).tap do |request|
|
31
|
+
request.on_complete do |response|
|
32
|
+
if response.success?
|
33
|
+
asset.write! response.body
|
34
|
+
else
|
35
|
+
asset.error!
|
36
|
+
end
|
37
|
+
|
38
|
+
on_fetched.call(asset) if on_fetched
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
class FetchSummary
|
47
|
+
MESSAGE = 'Fetched %s. Failed to fetch %s.'
|
48
|
+
|
49
|
+
def initialize(assets)
|
50
|
+
@assets = assets
|
51
|
+
end
|
52
|
+
|
53
|
+
def success?
|
54
|
+
@success ||= @assets.all?(&:success?)
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_s
|
58
|
+
@message = begin
|
59
|
+
successful_count = @assets.select(&:success?).length
|
60
|
+
failed_count = @assets.length - successful_count
|
61
|
+
|
62
|
+
MESSAGE % [ asset_count(successful_count), asset_count(failed_count) ]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def asset_count(count)
|
69
|
+
count == 1 ? '1 asset' : "#{count} assets"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ahab
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- James A. Rosen
|
9
|
+
- Stefan Vizzari
|
10
|
+
- Ana Martínez
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2013-10-20 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: multi_json
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ! '>='
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '0'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ! '>='
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '0'
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: typhoeus
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: cucumber
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
name: aruba
|
66
|
+
requirement: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
type: :development
|
73
|
+
prerelease: false
|
74
|
+
version_requirements: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: rspec
|
82
|
+
requirement: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
- !ruby/object:Gem::Dependency
|
97
|
+
name: webmock
|
98
|
+
requirement: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
description: Ruby client for the AHAB asset packaging system.
|
113
|
+
email:
|
114
|
+
- james.a.rosen+ahab@gmail.com
|
115
|
+
executables:
|
116
|
+
- ahab
|
117
|
+
extensions: []
|
118
|
+
extra_rdoc_files: []
|
119
|
+
files:
|
120
|
+
- bin/ahab
|
121
|
+
- lib/ahab/asset.rb
|
122
|
+
- lib/ahab/cli.rb
|
123
|
+
- lib/ahab/configuration.rb
|
124
|
+
- lib/ahab/fetch_task.rb
|
125
|
+
- lib/ahab/fetcher.rb
|
126
|
+
- lib/ahab.rb
|
127
|
+
- README.md
|
128
|
+
- HISTORY.md
|
129
|
+
- LICENSE
|
130
|
+
- features/cli.feature
|
131
|
+
- features/step_definitions/cli_steps.rb
|
132
|
+
- features/support/env.rb
|
133
|
+
- features/support/webmock.rb
|
134
|
+
homepage: http://github.com/zendesk/ahab.rb
|
135
|
+
licenses:
|
136
|
+
- Apache License Version 2.0
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ! '>='
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
149
|
+
requirements:
|
150
|
+
- - ! '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 1.3.6
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 1.8.25
|
156
|
+
signing_key:
|
157
|
+
specification_version: 3
|
158
|
+
summary: Ruby client for the AHAB asset packaging system.
|
159
|
+
test_files:
|
160
|
+
- features/cli.feature
|
161
|
+
- features/step_definitions/cli_steps.rb
|
162
|
+
- features/support/env.rb
|
163
|
+
- features/support/webmock.rb
|
164
|
+
has_rdoc:
|