parsecom 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,24 @@
1
+ # encoding:utf-8
1
2
  module Parse
2
- class Date < Time
3
+ class ParseDate
4
+ include Util
5
+
6
+ class <<self
7
+ def parse str
8
+ new :iso => str
9
+ end
10
+ end
11
+
12
+ def initialize hash={}
13
+ hash = string_keyed_hash hash
14
+ @raw_hash = hash
15
+ @time = ::Time.parse hash['iso'] if hash.has_key? 'iso'
16
+ end
17
+
3
18
  def to_h
4
19
  {
5
20
  "__type" => "Date",
6
- "iso" => strftime('%Y-%m-%dT%H:%M:%SZ')
21
+ "iso" => @time.strftime('%Y-%m-%dT%H:%M:%SZ')
7
22
  }
8
23
  end
9
24
 
@@ -14,5 +29,9 @@ module Parse
14
29
  def to_s
15
30
  to_json
16
31
  end
32
+
33
+ def method_missing name, *args, &block
34
+ @time.__send__ name, *args, &block
35
+ end
17
36
  end
18
37
  end
@@ -48,7 +48,7 @@ module Parse
48
48
  def initialize hash={}
49
49
  hash = string_keyed_hash hash
50
50
  @at = hash.delete 'at'
51
- @at = Parse::Date.parse @at if @at.is_a?(String)
51
+ @at = ParseDate.parse @at if @at.is_a?(String)
52
52
  @dimensions = hash.dup
53
53
  end
54
54
 
@@ -1,6 +1,6 @@
1
1
  # encoding:utf-8
2
2
  module Parse
3
- class File
3
+ class ParseFile
4
4
  include Util
5
5
 
6
6
  attr_accessor :name, :url, :content, :type
@@ -18,13 +18,13 @@ module Parse
18
18
  '.jpeg' => 'image/jpeg',
19
19
  '.png' => 'image/png',
20
20
  '.gif' => 'image/gif'
21
- }[::File.extname(@name).downcase]
21
+ }[File.extname(@name).downcase]
22
22
  @client = hash['parce_client'] || Parse::Client.default
23
23
  end
24
24
 
25
25
  def save
26
26
  raise "Files cannot be updated." if @url
27
- @content = ::File.read @content if @type =~ %r|^image/|
27
+ @content = File.read @content if @type =~ %r|^image/|
28
28
  @client.call_api :post, "files/#{@name}", @content, 'Content-Type' => @type, 'Accept' => nil do |resp_body|
29
29
  @name = resp_body['name']
30
30
  @url = resp_body['url']
@@ -40,14 +40,15 @@ module Parse
40
40
 
41
41
  def load &block
42
42
  open @url do |content| @content = content.read end unless @content
43
- block.call @content
43
+ block.call @content if block
44
+ @content
44
45
  end
45
46
 
46
47
  def store filepath=nil
47
48
  filepath ||= @name
48
49
  raise 'filepath is mandatory' unless filepath
49
50
 
50
- FileUtils.mkdir_p ::File.dirname(filepath)
51
+ FileUtils.mkdir_p File.dirname(filepath)
51
52
  load do |content|
52
53
  open filepath, 'wb' do |file|
53
54
  file.write content
@@ -92,9 +92,9 @@ module Parse
92
92
  when nil
93
93
  Parse::ACL.new v
94
94
  when 'Date'
95
- Parse::Date.parse v['iso']
95
+ ParseDate.parse v['iso']
96
96
  when 'File'
97
- Parse::File.new v
97
+ ParseFile.new v
98
98
  when 'Pointer'
99
99
  Parse::Pointer.new v, self
100
100
  when 'Relation'
@@ -150,14 +150,14 @@ module Parse
150
150
  def create hash, use_master_key=false
151
151
  check_deleted!
152
152
  hash = string_keyed_hash hash
153
- @updated_hash.update hash
153
+ @updated_hash = @raw_hash.dup.update(@updated_hash).update hash
154
154
  @updated_hash.reject! do |k, v|
155
155
  v.is_a?(Parse::Relation) && !v.changed?
156
156
  end
157
157
  method = use_master_key ? :create! : :create
158
158
  parse_client.send(method, self.parse_class_name, @updated_hash) do |response|
159
159
  @parse_object_id = response['objectId']
160
- @created_at = Parse::Date.parse response['createdAt']
160
+ @created_at = ParseDate.parse response['createdAt']
161
161
  @updated_at = @created_at
162
162
  @raw_hash.update @updated_hash
163
163
  @raw_hash.update response
@@ -165,7 +165,7 @@ module Parse
165
165
  end
166
166
  end
167
167
 
168
- def create! hash
168
+ def create! hash={}
169
169
  create hash, true
170
170
  end
171
171
 
@@ -176,7 +176,7 @@ module Parse
176
176
  parse_client.send(method, parse_class_name, parse_object_id, hash) do |response|
177
177
  @raw_hash.update @updated_hash
178
178
  @raw_hash.update response
179
- @updated_at = Parse::Date.parse response['updatedAt']
179
+ @updated_at = ParseDate.parse response['updatedAt']
180
180
  @updated_hash.clear
181
181
  end
182
182
  end
@@ -1,3 +1,3 @@
1
1
  module Parse
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -0,0 +1,18 @@
1
+ # coding:utf-8
2
+ require 'spec_helper'
3
+ require 'parsecom'
4
+
5
+ describe Parse::ParseDate, 'when converting from and to json' do
6
+ it 'should be converted to a correct json' do
7
+ date = Parse::ParseDate.parse '2013-10-18T20:53:25Z'
8
+ date.year.should == 2013
9
+ date.month.should == 10
10
+ date.to_json.should == '{"__type":"Date","iso":"2013-10-18T20:53:25Z"}'
11
+ end
12
+
13
+ it 'should be converted from a json' do
14
+ date = Parse::ParseDate.new :iso => '2013-10-18T20:53:25Z'
15
+ date.year.should == 2013
16
+ date.month.should == 10
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsecom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
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: 2013-10-22 00:00:00.000000000 Z
12
+ date: 2013-10-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -112,6 +112,7 @@ files:
112
112
  - spec/fixtures/vcr_cassettes/user_sign_up.yml
113
113
  - spec/parse_batch_spec.rb
114
114
  - spec/parse_client_spec.rb
115
+ - spec/parse_date_spec.rb
115
116
  - spec/parse_object_spec.rb
116
117
  - spec/parse_query_spec.rb
117
118
  - spec/parse_user_spec.rb
@@ -151,6 +152,7 @@ test_files:
151
152
  - spec/fixtures/vcr_cassettes/user_sign_up.yml
152
153
  - spec/parse_batch_spec.rb
153
154
  - spec/parse_client_spec.rb
155
+ - spec/parse_date_spec.rb
154
156
  - spec/parse_object_spec.rb
155
157
  - spec/parse_query_spec.rb
156
158
  - spec/parse_user_spec.rb