chute 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,203 @@
1
+ module Chute
2
+ class GCChute < GCResource
3
+
4
+ CHUTE_STATUS = {
5
+ "401" => "Password Protected",
6
+ "200" => "Public"
7
+ }
8
+
9
+ attr_accessor :id,
10
+ :name,
11
+ :shortcut,
12
+ :created_at,
13
+ :updated_at,
14
+ :assets_count,
15
+ :members_count,
16
+ :contributors_count,
17
+ :moderate_photos,
18
+ :moderate_members,
19
+ :moderate_comments,
20
+ :permission_view,
21
+ :permission_add_photos,
22
+ :permission_add_members,
23
+ :permission_add_comments
24
+
25
+ def initialize(attributes = {})
26
+ super
27
+ @id = attributes[:id]
28
+ @name = attributes[:name]
29
+ @shortcut = attributes[:shortcut]
30
+
31
+ @created_at = attributes[:created_at]
32
+ @updated_at = attributes[:updated_at]
33
+
34
+ @assets_count = attributes[:assets_count]
35
+ @members_count = attributes[:members_count]
36
+ @contributors_count = attributes[:contributors_count]
37
+
38
+ @moderate_photos = attributes[:moderate_photos]
39
+ @moderate_members = attributes[:moderate_members]
40
+ @moderate_comments = attributes[:moderate_comments]
41
+
42
+ @permission_view = attributes[:permission_view]
43
+ @permission_add_photos = attributes[:permission_add_photos]
44
+ @permission_add_members = attributes[:permission_add_members]
45
+ @permission_add_comments = attributes[:permission_add_comments]
46
+ end
47
+
48
+ # Public: Fetch child chutes for a chute
49
+ #
50
+ # Returns GCCollection of GCChute
51
+
52
+ def children
53
+ self.class.perform(self.class.get("/chutes/#{id}/children"))
54
+ end
55
+
56
+ # Public: Send a join request for a chute.
57
+ #
58
+ # Password - String, required for password protected chutes.
59
+ #
60
+ # Returns membership details and status.
61
+ # Public Chute - Request accepted.
62
+ # Password-protected Chute - Password required.
63
+ # Private Chute - Request for approval sent to admin.
64
+
65
+ def join(password=nil)
66
+ response = self.class.get("/chutes/#{id}/join")
67
+ response.data
68
+ end
69
+
70
+ # Public: Leave a chute.
71
+ def leave
72
+ response = self.class.post("/chutes/#{id}/leave")
73
+ response.data
74
+ end
75
+
76
+ # Public: Fetch list of chute contributors.
77
+ def contributors
78
+ Chute::GCChute.perform(self.class.post("/chutes/#{id}/contributors"))
79
+ end
80
+
81
+ # Public: Fetch assets for a chute
82
+ #
83
+ # Returns GCCollection of GCAsset
84
+ def assets
85
+ Chute::GCAsset.perform(self.class.get("/chutes/#{id}/assets"))
86
+ end
87
+
88
+ # def add_assets(asset_ids)
89
+ # response = self.class.post("/chutes/#{id}/add_assets", :asset_ids => "#{asset_ids}")
90
+ # response.is_success
91
+ # end
92
+
93
+ # Public: Remove assets from a chute
94
+ #
95
+ # Example
96
+ #
97
+ # Chute::GCChute.remove_assets([1, 23, 56])
98
+ # # => true
99
+ #
100
+ # Returns boolean true or false
101
+ def remove_assets(asset_ids)
102
+ response = self.class.post("/chutes/#{id}/assets/remove", :asset_ids => asset_ids)
103
+ response.is_success
104
+ end
105
+
106
+ # Public: Comment on an asset in a chute
107
+ #
108
+ # text - String, comment
109
+ # asset_id - Identifier of the asset
110
+ #
111
+ # Example
112
+ #
113
+ # Chute::GCChute.add_comment("Nice Picture", 24)
114
+ # # => comment
115
+ #
116
+ # Returns GCComment instance
117
+ def add_comment(text, asset_id)
118
+ comment = Chute::GCComment.new
119
+ comment.perform(self.class.post("/chutes/#{id}/assets/#{asset_id}/comments", {:comment => "test comment by payal"}))
120
+ comment
121
+ end
122
+
123
+ # Public: Fetch comments for a asset
124
+ #
125
+ # asset_id - Identifier for asset
126
+ #
127
+ # Returns GCCollection of GCAsset
128
+ def comments(asset_id)
129
+ Chute::GCComment.perform(self.class.get("/chutes/#{id}/assets/#{asset_id}/comments"))
130
+ end
131
+
132
+ # Public: Returns pluralized name of the resource.
133
+ def resource_name
134
+ 'chutes'
135
+ end
136
+
137
+ # Public: Fetch chute status from its shortcut.
138
+ # shortcut - String: shortcut of the chute.
139
+ #
140
+ # Example
141
+ #
142
+ # Chute::GCChute.fetch_status("adsvyh")
143
+ # # => public, password-protected or inaccessible
144
+ # Returns public, password-protected or inaccessibles string
145
+
146
+ def self.fetch_status(shortcut)
147
+ chute = Chute::GCChute.new
148
+ response = get("/chutes/#{shortcut}/public/assets.js")
149
+ CHUTE_STATUS[response.status] || "InAccessible"
150
+ end
151
+
152
+ # Public: Fetch chute from its shortcut.
153
+ # shortcut - String: shortcut of the chute.
154
+ #
155
+ # Example
156
+ #
157
+ # Chute::GCChute.find_by_shortcut("adsvyh")
158
+ # # => chute
159
+ # # => false if not found
160
+ # Returns GCChute.
161
+
162
+ def self.find_by_shortcut(shortcut)
163
+ chute = Chute::GCChute.new
164
+ chute.perform(get("/chutes/#{shortcut}"))
165
+ end
166
+
167
+ # Public: Fetch chute from its id.
168
+ # id - Integer: identifier.
169
+ #
170
+ # Example
171
+ #
172
+ # Chute::GCChute.find_by_id(23)
173
+ # # => chute
174
+ # # => false if not found
175
+ # Returns GCChute.
176
+
177
+ def self.find_by_id(id)
178
+ chute = Chute::GCChute.new
179
+ chute.perform(get("/chutes/#{id}")) ? chute : false
180
+ end
181
+
182
+ # Public: Fetch all chutes for the user.
183
+ #
184
+ # Returns GCCollection of GCChutes.
185
+ def self.all
186
+ self.perform(get("/me/chutes"))
187
+ end
188
+
189
+ # Public: Fetch chute heirarchy for the user
190
+ #
191
+ # Returns GCCollection of GCChute
192
+
193
+ def self.tree
194
+ perform(get("/chutes/tree"))
195
+ end
196
+
197
+ # Public: Returns pluralized name of the resource.
198
+ def self.class_path
199
+ "chutes"
200
+ end
201
+
202
+ end
203
+ end
@@ -0,0 +1,47 @@
1
+ module Chute
2
+ class GCCollection < Array
3
+
4
+ # Public: Append an item to collection
5
+ # single_or_array - Single item or an Array of item
6
+ #
7
+ # Example
8
+ #
9
+ # gc_collection = Chute::GCChute.all
10
+ # chute = Chute.new(:name => "New Chute Added")
11
+ # chute.save
12
+ #
13
+ # gc_collection.append(chute)
14
+ # # => appends the chute to the colection
15
+ # Returns GCCollection.
16
+
17
+ def append(single_or_array)
18
+ if Array === single_or_array
19
+ self.concat(single_or_array)
20
+ else
21
+ self << single_or_array
22
+ end
23
+ self.uniq!
24
+ end
25
+
26
+ # Public: Remove an item to collection
27
+ # single_or_array - Single item or an Array of item
28
+ #
29
+ # Example
30
+ #
31
+ # gc_collection = Chute::GCChute.all
32
+ # chute = Chute.find_by_id(23)
33
+ #
34
+ # gc_collection.remove(chute)
35
+ # # => remove the chute from the colection
36
+ # Returns GCCollection.
37
+
38
+ def remove(single_or_array)
39
+ if Array === single_or_array
40
+ self.reject!{|elem| single_or_array.include?(elem)}
41
+ else
42
+ self.delete(single_or_array)
43
+ end
44
+ self.uniq!
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,32 @@
1
+ module Chute
2
+ class GCComment < GCResource
3
+ attr_accessor :id,
4
+ :status,
5
+ :comment
6
+
7
+ def initialize(attributes = {})
8
+ super
9
+ @id = id
10
+ @status = status
11
+ @comment = comment
12
+ end
13
+
14
+ # Public: Returns pluralized name of the resource.
15
+ def resource_name
16
+ "comments"
17
+ end
18
+
19
+ def save
20
+ raise NotImplementedError
21
+ end
22
+
23
+ def update
24
+ raise NotImplementedError
25
+ end
26
+
27
+ # Public: Returns pluralized name of the resource.
28
+ def class_path
29
+ "comments"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ # lib/exceptions.rb
2
+ module Chute
3
+ module Exceptions
4
+ class UnAuthorized < StandardError; end
5
+ class InValidResponse < StandardError; end
6
+ end
7
+ end
@@ -0,0 +1,114 @@
1
+ module Chute
2
+ class GCRequest
3
+ include HTTParty
4
+
5
+ HEADERS = {"authorization" => API_KEY}
6
+
7
+ base_uri API_URL
8
+
9
+ # Public: Get request to a specific URL.
10
+ #
11
+ # url - relative url.
12
+ # params - parameters hash.
13
+ # headers - headers hash.
14
+ #
15
+ # Returns a GCResponse.
16
+
17
+ def get(url, params=nil, header_options=nil)
18
+ GCResponse.new(GCRequest.get(url, {:body => params, :headers => get_headers(header_options)}))
19
+ end
20
+
21
+ # Public: Post request to a specific URL.
22
+ #
23
+ # url - relative url.
24
+ # params - parameters hash.
25
+ # headers - headers hash.
26
+ #
27
+ # Returns a GCResponse.
28
+
29
+ def post(url, params=nil, header_options=nil)
30
+ GCResponse.new(GCRequest.post(url, {:body => params, :headers => get_headers(header_options)}))
31
+ end
32
+
33
+ # Public: Put request to a specific URL.
34
+ #
35
+ # url - relative url.
36
+ # params - parameters hash.
37
+ # headers - headers hash.
38
+ #
39
+ # Returns a GCResponse.
40
+
41
+ def put(url, params=nil, header_options=nil)
42
+ GCResponse.new(GCRequest.put(url, {:body => params, :headers => get_headers(header_options)}))
43
+ end
44
+
45
+ # Public: Delete request to a specific URL.
46
+ #
47
+ # url - relative url.
48
+ # params - parameters hash.
49
+ # headers - headers hash.
50
+ #
51
+ # Returns a GCResponse.
52
+
53
+ def delete(url, params=nil, header_options=nil)
54
+ GCResponse.new(GCRequest.delete(url, {:body => params, :headers => get_headers(header_options)}))
55
+ end
56
+
57
+ protected
58
+
59
+ # Protected: Add custom headers to default header options.
60
+ #
61
+ # header_options - Hash, header options.
62
+ #
63
+ # Returns final header Hash
64
+
65
+ def get_headers(header_options)
66
+ Hash === header_options ? header_options.merge(HEADERS) : HEADERS
67
+ end
68
+
69
+ end
70
+
71
+ class GCResponse
72
+ attr_reader :raw_response, :data, :errors, :status, :is_success
73
+ def initialize(attributes = {})
74
+ @raw_response = attributes
75
+ @status = attributes.response.code
76
+ @errors = []
77
+ parse(attributes.parsed_response)
78
+ end
79
+
80
+ protected
81
+
82
+ # Protected: Parse the data from the response
83
+ #
84
+ # parsed_response - HTTParty parsed response
85
+ #
86
+ # In case,
87
+ # status code is,
88
+ # < 300 - successful, set GCResponse.data as the returned data
89
+ # 401 - failed, Raise UnAuthorized Exception (like API key invalid or missing)
90
+ # other - failed, set GCResponse.errors as the returned errors
91
+ #
92
+ # Returns a GCResponse.
93
+
94
+ def parse(parsed_response)
95
+ if status.to_i <= 300
96
+ if parsed_response["errors"]
97
+ @is_success = false
98
+ parsed_response["errors"].each do |error|
99
+ @errors << error
100
+ end
101
+ else
102
+ @is_success = true
103
+ @data = parsed_response["data"]
104
+ end
105
+ else
106
+ @is_success = false
107
+ @errors << parsed_response["error"]
108
+ if status == "401"
109
+ raise Chute::Exceptions::UnAuthorized, parsed_response["error"]
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,41 @@
1
+ module Chute
2
+ class GCInbox
3
+
4
+ # Public: Fetch inbox information.
5
+ #
6
+ # Returns photo, comments, requests and invite count.
7
+ def self.info
8
+ request = GCRequest.new()
9
+ response = request.get("/#{class_path}")
10
+ response.data
11
+ end
12
+
13
+ # Public: Fetch list of invitations.
14
+ def self.invitations
15
+ request = GCRequest.new()
16
+ response = request.get("/#{class_path}/invites")
17
+ response.data
18
+ end
19
+
20
+ # Public: Fetch list of parcels.
21
+ def self.parcels
22
+ request = GCRequest.new()
23
+ Chute::GCParcel.perform(request.get("/#{class_path}/parcels"))
24
+ end
25
+
26
+ # Public: Fetch specific parcel data.
27
+ # parcel_id - String, Identifier for parcel
28
+ def self.parcel(parcel_id)
29
+ request = GCRequest.new()
30
+ parcel = Chute::GCParcel.new
31
+ parcel.perform(request.get("/#{class_path}/parcels/#{parcel_id}"))
32
+ parcel
33
+ end
34
+
35
+ # Public: Resource Name
36
+ def self.class_path
37
+ 'inbox'
38
+ end
39
+
40
+ end
41
+ end