populr 0.1.4 → 0.1.5
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.
- data/VERSION +1 -1
- data/lib/asset.rb +2 -2
- data/lib/embed_asset.rb +21 -0
- data/lib/pop.rb +27 -16
- data/lib/populr.rb +20 -5
- data/lib/restful_model.rb +9 -4
- data/lib/restful_model_collection.rb +49 -24
- data/lib/template.rb +5 -0
- data/populr.gemspec +3 -2
- data/spec/pop_spec.rb +8 -15
- data/spec/restful_model_collection_spec.rb +97 -58
- metadata +14 -13
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/lib/asset.rb
CHANGED
@@ -6,8 +6,8 @@ class Asset < RestfulModel
|
|
6
6
|
attr_accessor :title
|
7
7
|
|
8
8
|
# as an alternative to calling Asset.new, you can call populr.images.build
|
9
|
-
def initialize(
|
10
|
-
super(
|
9
|
+
def initialize(parent, file, title = nil, description = nil)
|
10
|
+
super(parent)
|
11
11
|
|
12
12
|
@file = file
|
13
13
|
self.title = title
|
data/lib/embed_asset.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'asset'
|
2
|
+
|
3
|
+
class EmbedAsset < Asset
|
4
|
+
|
5
|
+
attr_accessor :source_html
|
6
|
+
|
7
|
+
def self.collection_name
|
8
|
+
"embeds"
|
9
|
+
end
|
10
|
+
|
11
|
+
# as an alternative to calling Asset.new, you can call populr.images.build
|
12
|
+
def initialize(parent, source_html, title = nil, description = nil)
|
13
|
+
super(parent, nil)
|
14
|
+
|
15
|
+
@source_html = source_html
|
16
|
+
self.title = title
|
17
|
+
self.description = description
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
data/lib/pop.rb
CHANGED
@@ -21,36 +21,42 @@ class Pop < RestfulModel
|
|
21
21
|
attr_reader :newly_populated_regions
|
22
22
|
attr_reader :newly_populated_tags
|
23
23
|
|
24
|
-
def initialize(
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
self.
|
29
|
-
self.
|
30
|
-
self.
|
31
|
-
|
32
|
-
|
24
|
+
def initialize(parent)
|
25
|
+
if parent.is_a?(Template)
|
26
|
+
@_api = parent.instance_variable_get :@_api
|
27
|
+
self.template_id = parent._id
|
28
|
+
self.title = parent.title
|
29
|
+
self.name = parent.name
|
30
|
+
self.unpopulated_api_regions = parent.api_regions
|
31
|
+
self.unpopulated_api_tags = parent.api_tags
|
32
|
+
|
33
|
+
elsif parent.is_a?(RestfulModelCollection)
|
34
|
+
@_api = parent.instance_variable_get :@_api
|
35
|
+
|
36
|
+
elsif parent.is_a?(Populr)
|
37
|
+
@_api = parent
|
33
38
|
else
|
34
|
-
|
39
|
+
raise "You must create a pop with a template, collection, or API model."
|
35
40
|
end
|
36
41
|
|
42
|
+
# We could choose to make parent the restfulModelCollection that is passed to us,
|
43
|
+
# but that would result in PUTs and POSTs to /templates/:id/pops, which isn't
|
44
|
+
# how the API is currently set up. For now, all pop PUTS, POSTs, etc... go to /pops/
|
45
|
+
@_parent = @_api.pops
|
46
|
+
|
37
47
|
@newly_populated_regions = {}
|
38
48
|
@newly_populated_tags = {}
|
39
49
|
end
|
40
50
|
|
41
51
|
def inflate(json)
|
42
52
|
super(json)
|
43
|
-
|
44
|
-
collection = RestfulModelCollection.new(Tracer, @_api, self)
|
45
|
-
collection.inflate_collection(self.tracers) if self.tracers
|
46
|
-
self.tracers = collection
|
47
|
-
|
53
|
+
self.tracers = RestfulModelCollection.new(Tracer, @_api, self)
|
48
54
|
@newly_populated_regions = {}
|
49
55
|
@newly_populated_tags = {}
|
50
56
|
end
|
51
57
|
|
52
58
|
def as_json(options = {})
|
53
|
-
raise TemplateRequired.new
|
59
|
+
raise TemplateRequired.new if options[:api_representation] && !template_id
|
54
60
|
|
55
61
|
if options[:api_representation]
|
56
62
|
hash = {}
|
@@ -76,6 +82,11 @@ class Pop < RestfulModel
|
|
76
82
|
self.unpopulated_api_regions.include?(region_identifier)
|
77
83
|
end
|
78
84
|
|
85
|
+
def type_of_unpopulated_region(region_identifier)
|
86
|
+
return false unless self.has_unpopulated_region(region_identifier)
|
87
|
+
self.unpopulated_api_regions[region_identifier]['type']
|
88
|
+
end
|
89
|
+
|
79
90
|
def populate_region(region_identifier, assets)
|
80
91
|
assets = [assets] unless assets.is_a?(Array)
|
81
92
|
|
data/lib/populr.rb
CHANGED
@@ -3,6 +3,7 @@ require 'restful_model_collection'
|
|
3
3
|
require 'template'
|
4
4
|
require 'document_asset'
|
5
5
|
require 'image_asset'
|
6
|
+
require 'embed_asset'
|
6
7
|
require 'json'
|
7
8
|
require 'pop'
|
8
9
|
require 'domain'
|
@@ -51,26 +52,40 @@ class Populr
|
|
51
52
|
@api_server = api_server
|
52
53
|
@api_version = 'v0'
|
53
54
|
@api_key = api_key
|
55
|
+
|
56
|
+
RestClient.add_before_execution_proc do |req, params|
|
57
|
+
req.add_field('X-Populr-API-Wrapper', 'ruby')
|
58
|
+
end
|
54
59
|
end
|
55
60
|
|
56
61
|
def templates
|
57
|
-
RestfulModelCollection.new(Template, self)
|
62
|
+
@templates ||= RestfulModelCollection.new(Template, self)
|
63
|
+
@templates
|
58
64
|
end
|
59
65
|
|
60
66
|
def pops
|
61
|
-
RestfulModelCollection.new(Pop, self)
|
67
|
+
@pops ||= RestfulModelCollection.new(Pop, self)
|
68
|
+
@pops
|
62
69
|
end
|
63
70
|
|
64
71
|
def domains
|
65
|
-
RestfulModelCollection.new(Domain, self)
|
72
|
+
@domains ||= RestfulModelCollection.new(Domain, self)
|
73
|
+
@domains
|
66
74
|
end
|
67
75
|
|
68
76
|
def documents
|
69
|
-
RestfulModelCollection.new(DocumentAsset, self)
|
77
|
+
@documents ||= RestfulModelCollection.new(DocumentAsset, self)
|
78
|
+
@documents
|
70
79
|
end
|
71
80
|
|
72
81
|
def images
|
73
|
-
RestfulModelCollection.new(ImageAsset, self)
|
82
|
+
@images ||= RestfulModelCollection.new(ImageAsset, self)
|
83
|
+
@images
|
84
|
+
end
|
85
|
+
|
86
|
+
def embeds
|
87
|
+
@embeds ||= RestfulModelCollection.new(EmbedAsset, self)
|
88
|
+
@embeds
|
74
89
|
end
|
75
90
|
|
76
91
|
def url_for_path(path)
|
data/lib/restful_model.rb
CHANGED
@@ -8,8 +8,13 @@ class RestfulModel
|
|
8
8
|
end
|
9
9
|
|
10
10
|
|
11
|
-
def initialize(
|
12
|
-
|
11
|
+
def initialize(parent)
|
12
|
+
if parent.is_a?(Populr)
|
13
|
+
@_api = parent
|
14
|
+
else
|
15
|
+
@_parent = parent
|
16
|
+
@_api = parent.instance_variable_get :@_api
|
17
|
+
end
|
13
18
|
end
|
14
19
|
|
15
20
|
def ==(comparison_object)
|
@@ -22,7 +27,7 @@ class RestfulModel
|
|
22
27
|
property_name = setter.to_s[0..setter.to_s.index('=')-1]
|
23
28
|
self.send(setter, json[property_name]) if json.has_key?(property_name)
|
24
29
|
end
|
25
|
-
self.created_at = Time.
|
30
|
+
self.created_at = Time.parse(self.created_at) if self.created_at
|
26
31
|
end
|
27
32
|
|
28
33
|
def save!
|
@@ -61,7 +66,7 @@ class RestfulModel
|
|
61
66
|
def path(action = "")
|
62
67
|
action = "/#{action}" unless action.empty?
|
63
68
|
prefix = @_parent ? @_parent.path : ''
|
64
|
-
"#{prefix}
|
69
|
+
"#{prefix}#{_id}#{action}"
|
65
70
|
end
|
66
71
|
|
67
72
|
|
@@ -5,17 +5,51 @@ class RestfulModelCollection
|
|
5
5
|
def initialize(model_class, api, parent = nil)
|
6
6
|
@model_class = model_class
|
7
7
|
@_parent = parent
|
8
|
-
@_collection = nil
|
9
8
|
@_api = api
|
10
9
|
end
|
11
10
|
|
12
|
-
def
|
13
|
-
|
14
|
-
|
11
|
+
def each
|
12
|
+
offset = 0
|
13
|
+
finished = false
|
14
|
+
while (!finished) do
|
15
|
+
items = get_restful_model_collection(offset)
|
16
|
+
break if items.length == 0
|
17
|
+
items.each { |item|
|
18
|
+
yield item
|
19
|
+
}
|
20
|
+
offset += items.length
|
21
|
+
end
|
15
22
|
end
|
16
23
|
|
17
24
|
def first
|
18
|
-
|
25
|
+
get_restful_model_collection.first
|
26
|
+
end
|
27
|
+
|
28
|
+
def all
|
29
|
+
range(0, Float::INFINITY)
|
30
|
+
end
|
31
|
+
|
32
|
+
def range(offset = 0, count = 50)
|
33
|
+
accumulated = []
|
34
|
+
finished = false
|
35
|
+
chunk_size = 50
|
36
|
+
|
37
|
+
while (!finished && accumulated.length < count) do
|
38
|
+
results = get_restful_model_collection(offset + accumulated.length, chunk_size)
|
39
|
+
accumulated = accumulated.concat(results)
|
40
|
+
|
41
|
+
# we're done if we have more than 'count' items, or if we asked for 50 and got less than 50...
|
42
|
+
finished = accumulated.length >= count || results.length == 0 || (results.length % chunk_size != 0)
|
43
|
+
end
|
44
|
+
|
45
|
+
accumulated = accumulated[0..count] if count < Float::INFINITY
|
46
|
+
accumulated
|
47
|
+
end
|
48
|
+
|
49
|
+
def delete(item_or_id)
|
50
|
+
item_or_id = item_or_id._id if item_or_id.is_a?(RestfulModel)
|
51
|
+
url = @_api.url_for_path(self.path(item_or_id))
|
52
|
+
RestClient.delete(url)
|
19
53
|
end
|
20
54
|
|
21
55
|
def find(id)
|
@@ -24,32 +58,23 @@ class RestfulModelCollection
|
|
24
58
|
end
|
25
59
|
|
26
60
|
def build(*args)
|
27
|
-
@model_class.new(
|
28
|
-
end
|
29
|
-
|
30
|
-
def as_json(options = {})
|
31
|
-
objects = []
|
32
|
-
for model in self.all
|
33
|
-
objects.push(model.as_json(options))
|
34
|
-
end
|
35
|
-
objects
|
61
|
+
@model_class.new(self, *args)
|
36
62
|
end
|
37
63
|
|
38
64
|
def inflate_collection(items = [])
|
39
|
-
|
65
|
+
models = []
|
40
66
|
|
41
67
|
return unless items.is_a?(Array)
|
42
68
|
items.each do |json|
|
43
69
|
if @model_class < RestfulModel
|
44
70
|
model = @model_class.new(self)
|
45
|
-
model.instance_variable_set(:@_parent, @_parent)
|
46
71
|
model.inflate(json)
|
47
72
|
else
|
48
73
|
model = @model_class.new(json)
|
49
74
|
end
|
50
|
-
|
75
|
+
models.push(model)
|
51
76
|
end
|
52
|
-
|
77
|
+
models
|
53
78
|
end
|
54
79
|
|
55
80
|
def path(id = "")
|
@@ -66,8 +91,7 @@ class RestfulModelCollection
|
|
66
91
|
RestClient.get(url){ |response,request,result|
|
67
92
|
json = Populr.interpret_response(result, {:expected_class => Object})
|
68
93
|
if @model_class < RestfulModel
|
69
|
-
model = @model_class.new(
|
70
|
-
model.instance_variable_set(:@_parent, @_parent)
|
94
|
+
model = @model_class.new(self)
|
71
95
|
model.inflate(json)
|
72
96
|
else
|
73
97
|
model = @model_class.new(json)
|
@@ -76,14 +100,15 @@ class RestfulModelCollection
|
|
76
100
|
model
|
77
101
|
end
|
78
102
|
|
79
|
-
def get_restful_model_collection
|
80
|
-
url = @_api.url_for_path(self.path)
|
103
|
+
def get_restful_model_collection(offset = 0, count = 50)
|
104
|
+
url = @_api.url_for_path("#{self.path}?offset=#{offset}&count=#{count}")
|
105
|
+
models = []
|
81
106
|
|
82
107
|
RestClient.get(url){ |response,request,result|
|
83
108
|
items = Populr.interpret_response(result, {:expected_class => Array})
|
84
|
-
inflate_collection(items)
|
109
|
+
models = inflate_collection(items)
|
85
110
|
}
|
86
|
-
|
111
|
+
models
|
87
112
|
end
|
88
113
|
|
89
114
|
end
|
data/lib/template.rb
CHANGED
@@ -2,9 +2,14 @@ require 'restful_model'
|
|
2
2
|
|
3
3
|
class Template < RestfulModel
|
4
4
|
|
5
|
+
attr_accessor :title
|
5
6
|
attr_accessor :name
|
6
7
|
attr_accessor :label_names
|
7
8
|
attr_accessor :api_tags
|
8
9
|
attr_accessor :api_regions
|
9
10
|
|
11
|
+
def pops
|
12
|
+
@pops ||= RestfulModelCollection.new(Pop, @_api, self)
|
13
|
+
end
|
14
|
+
|
10
15
|
end
|
data/populr.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "populr"
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ben Gotow"]
|
12
|
-
s.date = "2013-04-
|
12
|
+
s.date = "2013-04-24"
|
13
13
|
s.description = "Gem for interacting with the Populr.me API that allows you to create and publish one-page websites, subscribe to web hooks and receive events when those pages are interacted with. Visit http://www.populr.me/ for more information. "
|
14
14
|
s.email = "ben@populr.me"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
"lib/asset.rb",
|
28
28
|
"lib/document_asset.rb",
|
29
29
|
"lib/domain.rb",
|
30
|
+
"lib/embed_asset.rb",
|
30
31
|
"lib/image_asset.rb",
|
31
32
|
"lib/pop.rb",
|
32
33
|
"lib/populr.rb",
|
data/spec/pop_spec.rb
CHANGED
@@ -12,29 +12,22 @@ describe 'Pop' do
|
|
12
12
|
it "should accept an API connection followed by a template" do
|
13
13
|
template = Template.new(@api)
|
14
14
|
template._id = '123'
|
15
|
-
pop = Pop.new(
|
15
|
+
pop = Pop.new(template)
|
16
16
|
pop.template_id.should == '123'
|
17
|
+
pop.instance_variable_get('@_api').should == @api
|
17
18
|
end
|
18
19
|
|
19
|
-
it "should accept
|
20
|
-
|
21
|
-
|
22
|
-
pop
|
23
|
-
pop.template_id.should == '123'
|
20
|
+
it "should accept a RestfulModelCollection as a parent" do
|
21
|
+
pop = Pop.new(@api.pops)
|
22
|
+
pop.template_id.should == nil
|
23
|
+
pop.instance_variable_get('@_api').should == @api
|
24
24
|
end
|
25
25
|
|
26
26
|
end
|
27
27
|
|
28
28
|
describe "#inflate" do
|
29
|
-
it "should
|
30
|
-
pop = Pop.new(@api)
|
31
|
-
pop.inflate(JSON.parse("{\"_id\":\"5107089add02dcaecc000003\",\"created_at\":\"2013-01-28T23:24:10Z\",\"domain\":\"generic\",\"name\":\"Untitled\",\"password\":null,\"slug\":\"\",\"tracers\":[{\"_id\":\"5109b5e0dd02dc5976000001\",\"created_at\":\"2013-01-31T00:08:00Z\",\"name\":\"Facebook\"},{\"_id\":\"5109b5f5dd02dc4c43000002\",\"created_at\":\"2013-01-31T00:08:21Z\",\"name\":\"Twitter\"}],\"published_pop_url\":\"http://group3.lvh.me\",\"unpopulated_api_tags\":[],\"unpopulated_api_regions\":[],\"label_names\":[]}"))
|
32
|
-
pop.tracers.first.is_a?(Tracer).should == true
|
33
|
-
pop.tracers.first.name.should == 'Facebook'
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should set the tracer collection's _parent so the tracer's path returns the full nested path" do
|
37
|
-
pop = Pop.new(@api)
|
29
|
+
it "should create a tracer collection whose _parent is set so the tracer's path returns the full nested path" do
|
30
|
+
pop = Pop.new(@api.pops)
|
38
31
|
pop.inflate(JSON.parse("{\"_id\":\"5107089add02dcaecc000003\",\"created_at\":\"2013-01-28T23:24:10Z\",\"domain\":\"generic\",\"name\":\"Untitled\",\"password\":null,\"slug\":\"\",\"tracers\":[{\"_id\":\"5109b5e0dd02dc5976000001\",\"created_at\":\"2013-01-31T00:08:00Z\",\"name\":\"Facebook\"},{\"_id\":\"5109b5f5dd02dc4c43000002\",\"created_at\":\"2013-01-31T00:08:21Z\",\"name\":\"Twitter\"}],\"published_pop_url\":\"http://group3.lvh.me\",\"unpopulated_api_tags\":[],\"unpopulated_api_regions\":[],\"label_names\":[]}"))
|
39
32
|
pop.tracers.path.should == "/pops/#{pop._id}/tracers/"
|
40
33
|
end
|
@@ -11,51 +11,11 @@ describe 'RestfulModelCollection' do
|
|
11
11
|
@collection = @populr.pops
|
12
12
|
end
|
13
13
|
|
14
|
-
describe "#all" do
|
15
|
-
it "should call interpret_response to handle standard error cases" do
|
16
|
-
Populr.should_receive(:interpret_response).and_return([])
|
17
|
-
templates = @populr.templates.all
|
18
|
-
end
|
19
|
-
|
20
|
-
context "when the model class is a RestfulModel" do
|
21
|
-
context "when the server responds correctly" do
|
22
|
-
before (:each) do
|
23
|
-
result = double('result')
|
24
|
-
result.stub(:body).and_return("[{\"_id\":\"5107089add02dcaecc000003\",\"created_at\":\"2013-01-28T23:24:10Z\",\"domain\":\"generic\",\"name\":\"Untitled\",\"password\":null,\"slug\":\"\",\"tracers\":[{\"_id\":\"5109b5e0dd02dc5976000001\",\"created_at\":\"2013-01-31T00:08:00Z\",\"name\":\"Facebook\"},{\"_id\":\"5109b5f5dd02dc4c43000002\",\"created_at\":\"2013-01-31T00:08:21Z\",\"name\":\"Twitter\"}],\"published_pop_url\":\"http://group3.lvh.me\",\"unpopulated_api_tags\":[],\"unpopulated_api_regions\":[],\"label_names\":[]}]")
|
25
|
-
result.stub(:code).and_return(200)
|
26
|
-
RestClient.should_receive(:get).and_yield(nil, nil, result)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should return an array of model objects" do
|
30
|
-
pops = @collection.all
|
31
|
-
pops.count.should == 1
|
32
|
-
pops.first.is_a?(RestfulModel).should == true
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should call inflate on each model object" do
|
36
|
-
Pop.any_instance.should_receive(:inflate)
|
37
|
-
pops = @collection.all
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
14
|
|
42
|
-
context "when the model class is not a restfulModel" do
|
43
|
-
it "should return the same data type in the JSON" do
|
44
|
-
result = double('result')
|
45
|
-
result.stub(:body).and_return("[\"a\",\"b\",\"c\"]")
|
46
|
-
result.stub(:code).and_return(200)
|
47
|
-
RestClient.should_receive(:get).and_yield(nil, nil, result)
|
48
|
-
@populr.domains.all.should == ['a','b','c']
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
15
|
|
53
16
|
describe "#first" do
|
54
17
|
it "should return the first item in the all collection" do
|
55
|
-
|
56
|
-
result.stub(:body).and_return("[{\"_id\":\"5107089add02dcaecc000003\",\"created_at\":\"2013-01-28T23:24:10Z\",\"domain\":\"generic\",\"name\":\"Untitled\",\"password\":null,\"slug\":\"\",\"tracers\":[{\"_id\":\"5109b5e0dd02dc5976000001\",\"created_at\":\"2013-01-31T00:08:00Z\",\"name\":\"Facebook\"},{\"_id\":\"5109b5f5dd02dc4c43000002\",\"created_at\":\"2013-01-31T00:08:21Z\",\"name\":\"Twitter\"}],\"published_pop_url\":\"http://group3.lvh.me\",\"unpopulated_api_tags\":[],\"unpopulated_api_regions\":[],\"label_names\":[]}]")
|
57
|
-
result.stub(:code).and_return(200)
|
58
|
-
RestClient.should_receive(:get).and_yield(nil, nil, result)
|
18
|
+
@collection.stub(:get_restful_model_collection).and_return(['a','b'])
|
59
19
|
@collection.first.should == @collection.all.first
|
60
20
|
end
|
61
21
|
end
|
@@ -80,6 +40,24 @@ describe 'RestfulModelCollection' do
|
|
80
40
|
pop._id.should == '5107089add02dcaecc000003'
|
81
41
|
end
|
82
42
|
end
|
43
|
+
|
44
|
+
it "should return nil and not throw an exception if you fail to pass an ID" do
|
45
|
+
@populr.pops.find(nil).should == nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#delete" do
|
50
|
+
it "should accept a model to delete" do
|
51
|
+
RestClient.should_receive(:delete).with(@populr.url_for_path(@populr.images.path('123')))
|
52
|
+
a = ImageAsset.new(@api, nil)
|
53
|
+
a._id = '123'
|
54
|
+
@populr.images.delete(a)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should accept a string ID to delete" do
|
58
|
+
RestClient.should_receive(:delete).with(@populr.url_for_path(@populr.images.path('123')))
|
59
|
+
@populr.images.delete('123')
|
60
|
+
end
|
83
61
|
end
|
84
62
|
|
85
63
|
describe "#build" do
|
@@ -89,34 +67,95 @@ describe 'RestfulModelCollection' do
|
|
89
67
|
image.is_a?(ImageAsset).should == true
|
90
68
|
end
|
91
69
|
|
92
|
-
it "should pass the
|
70
|
+
it "should pass the collection, and then any provided arguments" do
|
93
71
|
parameter1 = double('parameter1')
|
94
|
-
|
95
|
-
|
72
|
+
images = @populr.images
|
73
|
+
ImageAsset.any_instance.should_receive(:initialize).with(images, parameter1)
|
74
|
+
images.build(parameter1)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#all" do
|
79
|
+
it "should be a shorthand for requesting the entire range" do
|
80
|
+
@populr.images.should_receive(:range).with(0, Float::INFINITY)
|
81
|
+
@populr.images.all
|
96
82
|
end
|
97
83
|
end
|
98
84
|
|
99
|
-
describe "#
|
85
|
+
describe "#each" do
|
86
|
+
it "should yield each item, starting with the first one" do
|
87
|
+
@populr.images.should_receive(:get_restful_model_collection).with(0).and_return(['a','b','c'])
|
88
|
+
@populr.images.should_receive(:get_restful_model_collection).with(3).and_return(['d','e','f'])
|
89
|
+
@populr.images.should_receive(:get_restful_model_collection).with(6).and_return([])
|
90
|
+
|
91
|
+
yields = []
|
92
|
+
@populr.images.each do |item|
|
93
|
+
yields.push(item)
|
94
|
+
end
|
95
|
+
yields.should == ['a','b','c','d','e','f']
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "#path" do
|
100
|
+
it "should prepend it's parent's path if one exists" do
|
101
|
+
parent = double('parent')
|
102
|
+
parent.stub('path').and_return('/parent/50')
|
103
|
+
@populr.images.instance_variable_set(:@_parent, parent)
|
104
|
+
@populr.images.path.should == '/parent/50/images/'
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should generate the path using it's model class' collection name" do
|
108
|
+
ImageAsset.should_receive(:collection_name).and_return('collection')
|
109
|
+
@populr.images.path.should == '/collection/'
|
110
|
+
end
|
111
|
+
|
112
|
+
context "when provided with an ID" do
|
113
|
+
it "should append that ID to the end of the collection URL" do
|
114
|
+
@populr.images.path(5).should == '/images/5'
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "#range" do
|
100
120
|
before (:each) do
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
121
|
+
@items = []
|
122
|
+
200.times do
|
123
|
+
@items.push((65 + rand(25)).chr)
|
124
|
+
end
|
105
125
|
end
|
106
126
|
|
107
|
-
it "should
|
108
|
-
|
109
|
-
@
|
127
|
+
it "should return the first fifty rows by default" do
|
128
|
+
@populr.images.should_receive(:get_restful_model_collection).with(0,50).and_return(@items[0..49])
|
129
|
+
@populr.images.range.should == @items[0..49]
|
110
130
|
end
|
111
131
|
|
112
|
-
|
113
|
-
|
114
|
-
|
132
|
+
context "when a count is provided" do
|
133
|
+
it "should fetch chunks until it has enough rows, and then return exactly the number requested" do
|
134
|
+
@populr.images.should_receive(:get_restful_model_collection).with(0,50).and_return(@items[0..49])
|
135
|
+
@populr.images.should_receive(:get_restful_model_collection).with(50,50).and_return(@items[50..99])
|
136
|
+
@populr.images.range(0,100).count.should == 100
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should return early if a request for a chunk returns fewer rows than requested" do
|
140
|
+
@populr.images.should_receive(:get_restful_model_collection).with(0,50).and_return(@items[0..49])
|
141
|
+
@populr.images.should_receive(:get_restful_model_collection).with(50,50).and_return(@items[50..59])
|
142
|
+
@populr.images.range(0,100).count.should == 60
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should return early if a request for a chunk returns 0 rows" do
|
146
|
+
@populr.images.should_receive(:get_restful_model_collection).with(0,50).and_return(@items[0..49])
|
147
|
+
@populr.images.should_receive(:get_restful_model_collection).with(50,50).and_return([])
|
148
|
+
@populr.images.range(0,100).count.should == 50
|
149
|
+
end
|
115
150
|
end
|
116
151
|
|
117
|
-
|
118
|
-
|
119
|
-
|
152
|
+
context "when an offset is provided" do
|
153
|
+
it "should return results from that offset forward" do
|
154
|
+
@populr.images.should_receive(:get_restful_model_collection).with(100,50).and_return(@items[100..149])
|
155
|
+
@populr.images.should_receive(:get_restful_model_collection).with(150,50).and_return(@items[150..199])
|
156
|
+
@populr.images.should_receive(:get_restful_model_collection).with(200,50).and_return([])
|
157
|
+
@populr.images.range(100, 200).should == @items[100..200]
|
158
|
+
end
|
120
159
|
end
|
121
160
|
end
|
122
161
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: populr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rest-client
|
16
|
-
requirement: &
|
16
|
+
requirement: &70343202086340 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '1.6'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70343202086340
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: shoulda
|
27
|
-
requirement: &
|
27
|
+
requirement: &70343202085160 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70343202085160
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rdoc
|
38
|
-
requirement: &
|
38
|
+
requirement: &70343202084200 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '3.12'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70343202084200
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bundler
|
49
|
-
requirement: &
|
49
|
+
requirement: &70343202082840 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.3.5
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70343202082840
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: jeweler
|
60
|
-
requirement: &
|
60
|
+
requirement: &70343202081860 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: 1.8.4
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70343202081860
|
69
69
|
description: ! 'Gem for interacting with the Populr.me API that allows you to create
|
70
70
|
and publish one-page websites, subscribe to web hooks and receive events when those
|
71
71
|
pages are interacted with. Visit http://www.populr.me/ for more information. '
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- lib/asset.rb
|
87
87
|
- lib/document_asset.rb
|
88
88
|
- lib/domain.rb
|
89
|
+
- lib/embed_asset.rb
|
89
90
|
- lib/image_asset.rb
|
90
91
|
- lib/pop.rb
|
91
92
|
- lib/populr.rb
|
@@ -115,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
116
|
version: '0'
|
116
117
|
segments:
|
117
118
|
- 0
|
118
|
-
hash: -
|
119
|
+
hash: -4567027092822148073
|
119
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
121
|
none: false
|
121
122
|
requirements:
|