Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.

Commit f09764a

Browse files
authored
Merge pull request #5 from creatubbles/contents
Api expanded to support ctb-api-compare
2 parents f050035 + a63fea1 commit f09764a

File tree

7 files changed

+125
-12
lines changed

7 files changed

+125
-12
lines changed

lib/creatubbles.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def self.register_type_object_class(type_name, object_class)
2020
@type_object_classes[type_name] = object_class
2121
end
2222

23+
def self.type_object_classes
24+
@type_object_classes
25+
end
26+
2327
def self.object_class(type_name)
2428
@type_object_classes[type_name] || raise("unknown type: #{type_name}")
2529
end
@@ -38,6 +42,8 @@ def self.instantiate_objects_from_response(response, connection)
3842

3943
end
4044

45+
require "creatubbles/content"
46+
require "creatubbles/contents"
4147
require "creatubbles/creation"
4248
require "creatubbles/creations"
4349
require "creatubbles/upload"

lib/creatubbles/base_collection.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,13 @@ def find(id)
1717
res = @connection.get("#{self.class.type_name}/#{id}")
1818
Creatubbles.instantiate_object_from_response(res, @connection)
1919
end
20+
21+
def init_objects response
22+
Creatubbles.instantiate_objects_from_response(response, @connection)
23+
end
24+
25+
def handle_params params_hash={}, allowed_params=['query','filter']
26+
params_hash.stringify_keys!.slice!(allowed_params).to_param
27+
end
28+
2029
end

lib/creatubbles/content.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require "creatubbles/base_object"
2+
3+
class Creatubbles::Content < Creatubbles::BaseObject
4+
define_type_name 'contents'
5+
6+
define_attributes %w[
7+
type
8+
]
9+
10+
define_relationships %w[creation gallery user partner_application]
11+
12+
def sub_object
13+
send type
14+
end
15+
16+
end

lib/creatubbles/contents.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require "creatubbles/base_collection"
2+
3+
class Creatubbles::Contents < Creatubbles::BaseCollection
4+
5+
define_type_name 'contents'
6+
7+
def index params = {}
8+
init_objects @connection.get("contents?#{handle_params(params)}")
9+
end
10+
11+
def recent
12+
init_objects @connection.get('contents/recent')
13+
end
14+
15+
def trending
16+
init_objects @connection.get('contents/trending')
17+
end
18+
19+
def connected
20+
init_objects @connection.get('contents/connected')
21+
end
22+
23+
def followed
24+
init_objects @connection.get('contents/followed')
25+
end
26+
27+
def by_user user_id
28+
init_objects @connection.get("users/#{user_id}/contents")
29+
end
30+
31+
def bubbled_by_user user_id
32+
init_objects @connection.get("users/#{user_id}/bubbled_contents")
33+
end
34+
35+
end

lib/creatubbles/creations.rb

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,39 @@ class Creatubbles::Creations < Creatubbles::BaseCollection
44

55
define_type_name 'creations'
66

7-
# TODO update API endpoint
7+
def index params={}
8+
init_objects @connection.get("creations?#{handle_params(params)}")
9+
end
10+
811
def recent
9-
res = @connection.get("creations")
10-
Creatubbles.instantiate_objects_from_response(res, @connection)
12+
init_objects @connection.get("creations")
13+
end
14+
15+
def from_gallery gallery_id
16+
init_objects @connection.get("galleries/#{gallery_id}/creations")
17+
end
18+
19+
def by_user user_id
20+
init_objects @connection.get("users/#{user_id}/creations")
21+
end
22+
23+
def by_partner_application partner_application_id
24+
init_objects @connection.get("partner_applications/#{partner_application_id}/creations")
25+
end
26+
27+
def recommended_for_creation creation_id
28+
init_objects @connection.get("creations/#{creation_id}/recommended_creations")
29+
end
30+
31+
def recommended_for_user user_id
32+
init_objects @connection.get("users/#{user_id}/recommended_creations")
1133
end
1234

1335
def create(name: nil, description: nil)
14-
res = @connection.post('creations', :params => { 'name' => name, 'reflection_text' => description })
15-
Creatubbles.instantiate_object_from_response(res, @connection)
36+
init_objects @connection.post('creations', :params => { 'name' => name, 'reflection_text' => description })
1637
end
17-
38+
1839
def find_by_ids(ids)
19-
res = @connection.get("creations?filter[ids]=#{ids.join(',')}")
20-
Creatubbles.instantiate_objects_from_response(res, @connection)
40+
init_objects @connection.get("creations?filter[ids]=#{ids.join(',')}")
2141
end
2242
end

lib/creatubbles/galleries.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,25 @@
33
class Creatubbles::Galleries < Creatubbles::BaseCollection
44

55
define_type_name 'galleries'
6+
7+
def featured
8+
init_objects @connection.get('featured_galleries')
9+
end
10+
11+
def owned_by_user user_id
12+
init_objects @connection.get("users/#{user_id}/galleries")
13+
end
14+
15+
def mine
16+
init_objects @connection.get("users/me/galleries")
17+
end
18+
19+
def mine_favorite
20+
init_objects @connection.get("users/me/favorite_galleries")
21+
end
22+
23+
def containing_creation creation_id
24+
init_objects @connection.get("creations/#{creation_id}/galleries")
25+
end
26+
627
end

lib/creatubbles/user.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,18 @@ class Creatubbles::User < Creatubbles::BaseObject
3636
define_relationships %w[school custom_style]
3737

3838
def creators
39-
res = @connection.get("users/#{id}/creators")
40-
Creatubbles.instantiate_objects_from_response(res, @connection)
39+
init_objects @connection.get("users/#{id}/creators")
4140
end
4241

4342
def creations
44-
res = @connection.get("users/#{id}/creations")
45-
Creatubbles.instantiate_objects_from_response(res, @connection)
43+
init_objects @connection.get("users/#{id}/creations")
44+
end
45+
46+
def contents
47+
init_objects @connection.get("users/#{id}/contents")
48+
end
49+
50+
def bubbled_contents
51+
init_objects @connection.get("users/#{id}/bubbled_contents")
4652
end
4753
end

0 commit comments

Comments
 (0)