built.io 0.7 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,202 @@
1
+ require "uri"
2
+
3
+ module Built
4
+ class Upload < DirtyHashy
5
+ include Built::Timestamps
6
+
7
+ # Get the uid for this upload
8
+ def uid
9
+ self["uid"]
10
+ end
11
+
12
+ # Set the uid for this upload
13
+ # @param [String] uid A valid upload uid
14
+ def uid=(uid)
15
+ self["uid"] = uid
16
+ end
17
+
18
+ # Set a new file
19
+ # @param [File] file The file object to set
20
+ def file=(file)
21
+ Util.type_check("file", file, File)
22
+
23
+ self["upload"] = file
24
+ @file_set = file
25
+
26
+ self
27
+ end
28
+
29
+ # URL for the upload
30
+ # @return [String] url
31
+ def url
32
+ self["url"]
33
+ end
34
+
35
+ # Fetch the latest instance of this upload from built
36
+ # @raise BuiltError If the uid is not set
37
+ # @return [Upload] self
38
+ def sync
39
+ if !uid
40
+ # uid is not set
41
+ raise BuiltError, I18n.t("objects.uid_not_set")
42
+ end
43
+
44
+ instantiate(
45
+ Built.client.request(uri)
46
+ .json["upload"]
47
+ )
48
+
49
+ self
50
+ end
51
+
52
+ # Save / persist the upload to built.io
53
+ # @param [Hash] options Options
54
+ # @raise BuiltAPIError
55
+ # @return [Object] self
56
+ def save(options={})
57
+ header = {"Content-Type" => "multipart/form-data"}
58
+
59
+ if is_new?
60
+ unless @file_set
61
+ raise BuiltError, I18n.t("uploads.file_not_provided")
62
+ end
63
+
64
+ # create
65
+ instantiate(
66
+ Built.client.request(uri, :post, wrap, nil, header)
67
+ .json["upload"]
68
+ )
69
+ else
70
+ # update
71
+ instantiate(
72
+ Built.client.request(uri, :put, wrap, nil, header)
73
+ .json["upload"]
74
+ )
75
+ end
76
+
77
+ if @file_set
78
+ @file_set = nil
79
+ end
80
+
81
+ self
82
+ end
83
+
84
+ # Delete this upload
85
+ # @raise BuiltError If the uid is not set
86
+ # @return [Upload] self
87
+ def destroy
88
+ if !uid
89
+ # uid is not set
90
+ raise BuiltError, I18n.t("objects.uid_not_set")
91
+ end
92
+
93
+ Built.client.request(uri, :delete)
94
+
95
+ self.clear
96
+
97
+ self
98
+ end
99
+
100
+ # Get tags for this upload
101
+ def tags
102
+ self["tags"] || []
103
+ end
104
+
105
+ # Add new tags
106
+ # @param [Array] tags An array of strings. Can also be a single tag.
107
+ def add_tags(tags)
108
+ tags = tags.is_a?(Array) ? tags : [tags]
109
+ self["tags"] ||= []
110
+ self["tags"].concat(tags)
111
+ self
112
+ end
113
+
114
+ # Remove tags
115
+ # @param [Array] tags An array of strings. Can also be a single tag.
116
+ def remove_tags(tags)
117
+ tags = tags.is_a?(Array) ? tags : [tags]
118
+ self["tags"] ||= []
119
+ self["tags"] = self["tags"] - tags
120
+ self
121
+ end
122
+
123
+ # Initialize a new upload
124
+ # @param [String] uid The uid of an existing upload, if this is an existing upload
125
+ def initialize(uid=nil)
126
+ if uid
127
+ self.uid = uid
128
+ end
129
+
130
+ clean_up!
131
+ self
132
+ end
133
+
134
+ # @api private
135
+ def instantiate(data)
136
+ replace(data)
137
+ clean_up!
138
+ self
139
+ end
140
+
141
+ # Get ACL
142
+ # @return [ACL]
143
+ def ACL
144
+ Built::ACL.new(self["ACL"])
145
+ end
146
+
147
+ # Set ACL
148
+ # @param [ACL] acl
149
+ def ACL=(acl)
150
+ self["ACL"] = {
151
+ "disable" => acl.disabled,
152
+ "others" => acl.others,
153
+ "users" => acl.users,
154
+ "roles" => acl.roles
155
+ }
156
+
157
+ self
158
+ end
159
+
160
+ private
161
+
162
+ def uri
163
+ uid ?
164
+ [self.class.uri, uid].join("/") :
165
+ self.class.uri
166
+ end
167
+
168
+ def wrap
169
+ data = {
170
+ "PARAM" => {"upload" => self.select {|key| key != "upload"}}.to_json
171
+ }
172
+
173
+ if @file_set
174
+ data["upload[upload]"] = self["upload"]
175
+ end
176
+
177
+ data
178
+ end
179
+
180
+ def to_s
181
+ "#<Built::Upload uid=#{uid}>"
182
+ end
183
+
184
+ # Is this a new, unsaved object?
185
+ # @return [Boolean]
186
+ def is_new?
187
+ Util.blank?(uid)
188
+ end
189
+
190
+ class << self
191
+ def instantiate(data)
192
+ doc = new
193
+ doc.instantiate(data)
194
+ end
195
+
196
+ # @api private
197
+ def uri
198
+ "/uploads"
199
+ end
200
+ end
201
+ end
202
+ end
data/lib/built/user.rb ADDED
@@ -0,0 +1,147 @@
1
+ module Built
2
+ class User < Built::Object
3
+ # Create a new user object
4
+ # @param [String] uid The uid of an existing user, if this is an existing user
5
+ def initialize(uid=nil)
6
+ super(Built::USER_CLASS_UID, uid)
7
+ end
8
+
9
+ # Assign authtoken for this user
10
+ def authtoken=(token)
11
+ @authtoken = token
12
+ self["authtoken"] = token
13
+ end
14
+
15
+ # Get the authtoken for this user
16
+ def authtoken
17
+ @authtoken
18
+ end
19
+
20
+ # Logout a user
21
+ def logout
22
+ if !authtoken || (authtoken != Built.client.authtoken)
23
+ raise BuiltError, I18n.t("users.not_logged_in")
24
+ end
25
+
26
+ Built.client.request(
27
+ logout_uri,
28
+ :delete
29
+ )
30
+
31
+ authtoken = nil
32
+ Built.client.authtoken = nil
33
+ Built.client.current_user = nil
34
+
35
+ self
36
+ end
37
+
38
+ # Update the user profile
39
+ def update_user
40
+ data = {
41
+ self.class.user_wrapper => wrap.delete("object")
42
+ }
43
+
44
+ response = Built.client.request(uri, :put, data).json[self.class.user_wrapper]
45
+
46
+ instantiate(response)
47
+
48
+ self
49
+ end
50
+
51
+ # Delete / Deactivate the user. USE WITH CAUTION!
52
+ def deactivate
53
+ Built.client.request(uri, :delete)
54
+
55
+ self.clear
56
+
57
+ self
58
+ end
59
+
60
+ private
61
+
62
+ def uri
63
+ self.class.uri + '/#{uid}'
64
+ end
65
+
66
+ class << self
67
+ # Login a user with an email and password
68
+ # Once logged in, the user's authtoken will be used for all further requests
69
+ # @param [String] email The user's email
70
+ # @param [String] password The user's password
71
+ def login(email, password)
72
+ Util.type_check("email", email, String)
73
+ Util.type_check("password", password, String)
74
+
75
+ response = Built.client.request(
76
+ login_uri,
77
+ :post,
78
+ {user_wrapper => {"email" => email, "password" => password}}
79
+ ).json[user_wrapper]
80
+
81
+ user = instantiate(response)
82
+ user.authtoken = response["authtoken"]
83
+ Built.client.authtoken = user.authtoken
84
+ Built.client.current_user = user
85
+
86
+ user
87
+ end
88
+
89
+ # TODO
90
+ def login_with_google
91
+ end
92
+
93
+ # TODO
94
+ def login_with_facebook
95
+ end
96
+
97
+ # TODO
98
+ def login_with_tibbr
99
+ end
100
+
101
+ # TODO
102
+ def login_with_twitter
103
+ end
104
+
105
+ # Fetch the current logged in user
106
+ def current_user
107
+ return Built.client.current_user if Built.client.current_user
108
+
109
+ if Built.client.authtoken
110
+ # we have the authtoken, but not the user
111
+ # we'll fetch the user corresponding to the authtoken
112
+ user = instantiate(Built.client.request(current_uri).json[user_wrapper])
113
+ Built.client.current_user = user
114
+ end
115
+
116
+ Built.client.current_user
117
+ end
118
+
119
+ private
120
+
121
+ def user_wrapper
122
+ "application_user"
123
+ end
124
+
125
+ def wrap_in_user
126
+ changed_keys = self.changes.keys
127
+ {"application_user" => self.select {|o| changed_keys.include?(o)}}
128
+ end
129
+
130
+ def users_uri
131
+ "/application/users"
132
+ end
133
+
134
+ def login_uri
135
+ users_uri + "/login"
136
+ end
137
+
138
+ def logout_uri
139
+ users_uri + "/logout"
140
+ end
141
+
142
+ def current_uri
143
+ users_uri + '/current'
144
+ end
145
+ end
146
+ end
147
+ end
data/lib/built/util.rb CHANGED
@@ -1,7 +1,22 @@
1
1
  module Built
2
2
  class Util
3
- def self.blank?(value)
4
- value.respond_to?(:empty?) ? value.empty? : !value
3
+ class << self
4
+ def blank?(value)
5
+ value.respond_to?(:empty?) ? value.empty? : !value
6
+ end
7
+
8
+ def type_check(key, value, type)
9
+ unless value.is_a?(type)
10
+ raise BuiltError, I18n.t("datatypes.not_match", {
11
+ :key => key,
12
+ :type => type
13
+ })
14
+ end
15
+ end
16
+
17
+ def is_i?(value)
18
+ !!(value =~ /^[-+]?[0-9]+$/)
19
+ end
5
20
  end
6
21
  end
7
22
  end
data/lib/built.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "i18n"
2
- require "httmultiparty"
2
+ require "rest-client"
3
3
  require "dirty_hashy"
4
4
 
5
5
  module Built
@@ -19,8 +19,8 @@ module Built
19
19
  def init(options)
20
20
  options ||= {}
21
21
 
22
- host = options[:host] || API_URI
23
- version = options[:version] || API_VERSION
22
+ host = options[:host] || Built::API_URI
23
+ version = options[:version] || Built::API_VERSION
24
24
  master_key = options[:master_key]
25
25
  api_key = options[:application_api_key]
26
26
  authtoken = options[:authtoken]
@@ -66,4 +66,10 @@ require "built/timestamps"
66
66
  require "built/application"
67
67
  require "built/class"
68
68
  require "built/object"
69
- require "built/query"
69
+ require "built/query"
70
+ require "built/location"
71
+ require "built/acl"
72
+ require "built/user"
73
+ require "built/role"
74
+ require "built/installation"
75
+ require "built/upload"
data/lib/locales/en.yml CHANGED
@@ -5,5 +5,12 @@ en:
5
5
  objects:
6
6
  class_uid: "class_uid needs to be provided when initializing an object"
7
7
  uid_not_set: "uid for this object needs to be set before attempting to fetch it"
8
+ pull_require: "`pull_value` requires either `index` or `value` to be provided"
8
9
  querying:
9
- class_uid: "class_uid needs to be provided to query for objects"
10
+ class_uid: "class_uid needs to be provided to query for objects"
11
+ datatypes:
12
+ not_match: "%{key} is not of the type %{type}"
13
+ users:
14
+ not_logged_in: "The user needs to be logged in order to log out"
15
+ uploads:
16
+ file_not_provided: "Before creating an upload, you need to set a file."
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: built.io
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.7'
4
+ version: '0.8'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-25 00:00:00.000000000 Z
12
+ date: 2014-03-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
@@ -28,13 +28,13 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: 0.6.1
30
30
  - !ruby/object:Gem::Dependency
31
- name: httmultiparty
31
+ name: rest-client
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
35
  - - '='
36
36
  - !ruby/object:Gem::Version
37
- version: 0.3.10
37
+ version: 1.6.7
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - '='
44
44
  - !ruby/object:Gem::Version
45
- version: 0.3.10
45
+ version: 1.6.7
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: dirty_hashy
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -98,15 +98,21 @@ executables: []
98
98
  extensions: []
99
99
  extra_rdoc_files: []
100
100
  files:
101
+ - lib/built/upload.rb
101
102
  - lib/built/constants.rb
102
103
  - lib/built/util.rb
104
+ - lib/built/user.rb
103
105
  - lib/built/error.rb
104
106
  - lib/built/timestamps.rb
105
107
  - lib/built/application.rb
108
+ - lib/built/role.rb
109
+ - lib/built/acl.rb
106
110
  - lib/built/i18n.rb
111
+ - lib/built/installation.rb
107
112
  - lib/built/client.rb
108
113
  - lib/built/class.rb
109
114
  - lib/built/query.rb
115
+ - lib/built/location.rb
110
116
  - lib/built/object.rb
111
117
  - lib/locales/en.yml
112
118
  - lib/built.rb