alephant-broker 1.3.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54b3da006e03b9b1bb3a4697ef59e5472a872481
4
- data.tar.gz: b6fb65f722c3cb34f5589327dc1478244d1367fb
3
+ metadata.gz: 13b219a0a816af81e147be4076441a641d597094
4
+ data.tar.gz: d1a8e7340e1928c339fded8e0ea1b47cc8cfa3a0
5
5
  SHA512:
6
- metadata.gz: dc7a8957b4a78a63681e211df68fb18a8bd27c48093a858afab1a394bcc07809666c36f3ac5e711e2726372c306385fb53e1099bc274753a6b7b74e3e562554c
7
- data.tar.gz: 6b16b372746c156f6ec2a93c7f906985f88e25e5445df00d4539b1d0567c3408889d2b6f85ab85c99194a820af74c0f187649cb5e4b6341215cb5ae41cd6134a
6
+ metadata.gz: 3493cad83967ed33668d6fd191e57aa3b58c30628f8ffad8e82e75dc72ef0ec888c7ffd16cca35cbfd015d7b70a6e7b7319323ba4f5db06fd6531a8fa30c99d1
7
+ data.tar.gz: 2d398103e43e2945664a7d879578eed73b28b2386f340517a84b4aa107a5645c1c80954555608fdd32512b4d7aa060a0b4fb0684aa5c0403df8166f913a49c75
@@ -37,4 +37,5 @@ Gem::Specification.new do |spec|
37
37
  spec.add_runtime_dependency 'alephant-sequencer'
38
38
  spec.add_runtime_dependency "dalli-elasticache"
39
39
  spec.add_runtime_dependency 'pmap'
40
+ spec.add_runtime_dependency "faraday"
40
41
  end
@@ -0,0 +1,68 @@
1
+ require 'alephant/broker/cache'
2
+ require 'alephant/broker/errors/content_not_found'
3
+ require 'faraday'
4
+
5
+ module Alephant
6
+ module Broker
7
+ module LoadStrategy
8
+ class HTTP
9
+ class RequestFailed < StandardError; end
10
+
11
+ class URL
12
+ def generate
13
+ raise NotImplementedError
14
+ end
15
+ end
16
+
17
+ def initialize(url_generator)
18
+ @url_generator = url_generator
19
+ end
20
+
21
+ def load(component_meta)
22
+ cache_object(component_meta)
23
+ rescue
24
+ cache.set(component_meta.cache_key, content(component_meta))
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :cache, :url_generator
30
+
31
+ def cache
32
+ @cache ||= Cache::Client.new
33
+ end
34
+
35
+ def cache_object(component_meta)
36
+ cache.get(component_meta.cache_key) { content component_meta }
37
+ end
38
+
39
+ def content(component_meta)
40
+ resp = request component_meta
41
+ {
42
+ :content => resp.body,
43
+ :content_type => extract_content_type_from(resp.env.response_headers)
44
+ }
45
+ end
46
+
47
+ def extract_content_type_from(headers)
48
+ headers['content-type'].split(';').first
49
+ end
50
+
51
+ def request(component_meta)
52
+ component_meta.cached = false
53
+ Faraday.get(url_for component_meta).
54
+ tap { |r| raise ContentNotFound unless r.success? }
55
+ rescue => e
56
+ raise RequestFailed, e
57
+ end
58
+
59
+ def url_for(component_meta)
60
+ url_generator.generate(
61
+ component_meta.id,
62
+ component_meta.options
63
+ )
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Broker
3
- VERSION = "1.3.1"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
@@ -0,0 +1,87 @@
1
+ require "spec_helper"
2
+
3
+ describe Alephant::Broker::LoadStrategy::HTTP do
4
+ subject { described_class.new(url_generator) }
5
+
6
+ let(:component_meta) do
7
+ double('Alephant::Broker::ComponentMeta', cache_key: 'cache_key', id: 'test')
8
+ end
9
+ let(:url_generator) { double(generate: "http://foo.bar") }
10
+ let(:cache) { double('Alephant::Broker::Cache::Client') }
11
+ let(:body) { '<h1>Batman!</h1>' }
12
+ let(:content) do
13
+ {
14
+ :content => body,
15
+ :content_type => 'text/html'
16
+ }
17
+ end
18
+
19
+ before :each do
20
+ allow(Alephant::Broker::Cache::Client).to receive(:new) { cache }
21
+ end
22
+
23
+ describe "#load" do
24
+ context "content in cache" do
25
+ before :each do
26
+ allow(cache).to receive(:get) { content }
27
+ end
28
+
29
+ it "gets from cache" do
30
+ expect(subject.load component_meta).to eq content
31
+ end
32
+ end
33
+
34
+ context "content not in cache" do
35
+ before :each do
36
+ allow(cache).to receive(:get).and_yield
37
+ allow(component_meta).to receive(:'cached=').with(false) { false }
38
+ allow(component_meta).to receive(:options).and_return Hash.new
39
+ end
40
+
41
+ context "and available over HTTP" do
42
+ let(:env) { double('env', response_headers: response_headers) }
43
+ let(:response_headers) { {'content-type' => 'text/html; test'} }
44
+
45
+ before :each do
46
+ allow(Faraday).to receive(:get) do
47
+ instance_double(
48
+ 'Faraday::Response',
49
+ body: body,
50
+ :'success?' => true,
51
+ env: env)
52
+ end
53
+ end
54
+
55
+ it "gets from HTTP" do
56
+ expect(subject.load component_meta).to eq content
57
+ end
58
+ end
59
+
60
+ context "and HTTP request fails" do
61
+ before :each do
62
+ allow(Faraday).to receive(:get).and_raise
63
+ end
64
+
65
+ specify do
66
+ expect do
67
+ subject.load component_meta
68
+ end.to raise_error Alephant::Broker::LoadStrategy::HTTP::RequestFailed
69
+ end
70
+ end
71
+
72
+ context "and HTTP request 404s" do
73
+ before :each do
74
+ allow(Faraday).to receive(:get) do
75
+ instance_double('Faraday::Response', body: body, :'success?' => false)
76
+ end
77
+ end
78
+
79
+ specify do
80
+ expect do
81
+ subject.load component_meta
82
+ end.to raise_error Alephant::Broker::LoadStrategy::HTTP::RequestFailed
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -4,6 +4,10 @@ require 'pry'
4
4
  require 'json'
5
5
  require 'alephant/broker'
6
6
  require 'alephant/broker/load_strategy/s3'
7
+ require "alephant/broker/load_strategy/http"
8
+ require "alephant/broker/cache"
9
+ require "alephant/broker/errors/content_not_found"
10
+
7
11
  require 'rack/test'
8
12
 
9
13
  ENV['RACK_ENV'] = 'test'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-broker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Jack
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-21 00:00:00.000000000 Z
11
+ date: 2014-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -248,6 +248,20 @@ dependencies:
248
248
  - - '>='
249
249
  - !ruby/object:Gem::Version
250
250
  version: '0'
251
+ - !ruby/object:Gem::Dependency
252
+ requirement: !ruby/object:Gem::Requirement
253
+ requirements:
254
+ - - '>='
255
+ - !ruby/object:Gem::Version
256
+ version: '0'
257
+ name: faraday
258
+ prerelease: false
259
+ type: :runtime
260
+ version_requirements: !ruby/object:Gem::Requirement
261
+ requirements:
262
+ - - '>='
263
+ - !ruby/object:Gem::Version
264
+ version: '0'
251
265
  description: Brokers requests for alephant components
252
266
  email:
253
267
  - stevenmajack@gmail.com
@@ -274,6 +288,7 @@ files:
274
288
  - lib/alephant/broker/errors/content_not_found.rb
275
289
  - lib/alephant/broker/errors/invalid_asset_id.rb
276
290
  - lib/alephant/broker/errors/invalid_cache_key.rb
291
+ - lib/alephant/broker/load_strategy/http.rb
277
292
  - lib/alephant/broker/load_strategy/s3.rb
278
293
  - lib/alephant/broker/request.rb
279
294
  - lib/alephant/broker/request/asset.rb
@@ -290,6 +305,7 @@ files:
290
305
  - lib/alephant/broker/version.rb
291
306
  - spec/fixtures/json/batch.json
292
307
  - spec/fixtures/json/batch_compiled.json
308
+ - spec/http_spec.rb
293
309
  - spec/integration/spec_helper.rb
294
310
  - spec/rack_spec.rb
295
311
  - spec/spec_helper.rb
@@ -320,6 +336,7 @@ summary: Brokers requests for alephant components
320
336
  test_files:
321
337
  - spec/fixtures/json/batch.json
322
338
  - spec/fixtures/json/batch_compiled.json
339
+ - spec/http_spec.rb
323
340
  - spec/integration/spec_helper.rb
324
341
  - spec/rack_spec.rb
325
342
  - spec/spec_helper.rb