dm-parse 0.3.2 → 0.3.3

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 CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
data/dm-parse.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "dm-parse"
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Zhi-Qiang Lei"]
@@ -25,7 +25,7 @@ module DataMapper
25
25
  result = engine.create storage_name, params
26
26
 
27
27
  initialize_serial resource, result["objectId"]
28
- resource.created_at = resource.updated_at = result["createdAt"].to_datetime
28
+ resource.created_at = resource.updated_at = result["createdAt"]
29
29
  end.size
30
30
  end
31
31
 
data/lib/dm-parse.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "uri"
1
2
  require "dm-core"
2
3
  require "dm-validations"
3
4
  require "active_support/all"
@@ -4,13 +4,17 @@ module DataMapper
4
4
  class ParseDate < Object
5
5
 
6
6
  def dump(value)
7
- value && {"__type" => "Date", "iso" => value.to_datetime.utc.iso8601(3)}
7
+ value && {"__type" => "Date", "iso" => value.utc.iso8601(3)}
8
8
  end
9
9
 
10
10
  def load(value)
11
11
  value && (value.is_a?(Hash) ? value["iso"].to_datetime : value.to_datetime)
12
12
  end
13
13
 
14
+ def typecast(value)
15
+ value && value.to_datetime
16
+ end
17
+
14
18
  end
15
19
 
16
20
  end
@@ -4,11 +4,11 @@ module DataMapper
4
4
  class ParseFile < Object
5
5
 
6
6
  def dump(value)
7
- value
7
+ value && { "__type" => "File", "name" => File.basename(value.path) }
8
8
  end
9
9
 
10
10
  def load(value)
11
- value
11
+ value && URI(value["url"])
12
12
  end
13
13
 
14
14
  def typecast(value)
@@ -17,9 +17,8 @@ module DataMapper
17
17
  filename = value.original_filename
18
18
  content = value.read
19
19
  content_type = value.content_type
20
- adapter.upload_file(filename, content, content_type).merge("__type" => "File")
21
- else
22
- value
20
+ response = adapter.upload_file(filename, content, content_type)
21
+ URI(response["url"])
23
22
  end
24
23
  end
25
24
 
@@ -1,5 +1,4 @@
1
1
  require "spec_helper"
2
- require "uri"
3
2
  require "net/http"
4
3
  require "net/https"
5
4
 
@@ -32,9 +31,7 @@ describe "resource" do
32
31
  let(:content) { "xx" }
33
32
 
34
33
  describe "its file content" do
35
- subject { Net::HTTP.get URI(url) }
36
-
37
- let(:url) { resource.attachment["url"] }
34
+ subject { Net::HTTP.get resource.attachment }
38
35
 
39
36
  it { should eq(content) }
40
37
  end
@@ -42,4 +42,12 @@ describe DataMapper::Property::ParseDate do
42
42
 
43
43
  it { should be_true }
44
44
  end
45
+
46
+ describe "#typecast" do
47
+ subject { property.typecast value }
48
+
49
+ let(:value) { "2011-08-21T18:02:52.249Z" }
50
+
51
+ it { should be_a(DateTime) }
52
+ end
45
53
  end
@@ -3,7 +3,9 @@ require "spec_helper"
3
3
  describe DataMapper::Property::ParseFile do
4
4
  subject { property }
5
5
 
6
- let(:property) { Article.properties[:attachment] }
6
+ let(:property) { Article.properties[:attachment] }
7
+ let(:url) { "http://files.parse.com/3f10b6f9-bec4-4583-b907-8f2ece6e965a/003ce5ad-06c4-4be6-9475-b074b6bd4dc8-test.png" }
8
+ let(:name) { File.basename URI(url).path }
7
9
 
8
10
  describe "#typecast" do
9
11
  subject { property.typecast value }
@@ -17,21 +19,27 @@ describe DataMapper::Property::ParseFile do
17
19
  context "when value is io" do
18
20
  let(:value) { StringIO.new "xx" }
19
21
 
20
- before { value.stub(original_filename: "xx.txt", content_type: "plain/text") }
21
- before { DataMapper::Adapters::ParseAdapter.any_instance.stub(upload_file: {"name" => "x", "url" => "y"}) }
22
+ before { value.stub(original_filename: "test.png", content_type: "image/png") }
23
+ before { DataMapper::Adapters::ParseAdapter.any_instance.stub(upload_file: { "name" => name, "url" => url }) }
22
24
 
23
- it { should be_has_key("__type") }
24
- it { should be_has_key("name") }
25
- it { should be_has_key("url") }
25
+ it { should eq(URI(url)) }
26
26
  end
27
27
  end
28
28
 
29
+ describe "#dump" do
30
+ subject { property.dump value }
31
+
32
+ let(:value) { URI(url) }
33
+
34
+ it { should eq("__type" => "File", "name" => name) }
35
+ end
36
+
29
37
  describe "#load" do
30
38
  subject { property.load value }
31
39
 
32
- let(:value) { { "__type" => "File", "name" => "a.png", "url" => "http://a.cn/a.png" } }
40
+ let(:value) { { "__type" => "File", "name" => name, "url" => url } }
33
41
 
34
- it { should eq(value) }
42
+ it { should eq(URI(url)) }
35
43
 
36
44
  context "when value is nil" do
37
45
  let(:value) { nil }
@@ -43,7 +51,7 @@ describe DataMapper::Property::ParseFile do
43
51
  describe "#valid?" do
44
52
  subject { property.valid? value }
45
53
 
46
- let(:value) { { "__type" => "File", "name" => "a.png", "url" => "http://a.cn/a.png" } }
54
+ let(:value) { URI(url) }
47
55
 
48
56
  it { should be_true }
49
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-parse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -241,7 +241,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
241
241
  version: '0'
242
242
  segments:
243
243
  - 0
244
- hash: 41573292717956535
244
+ hash: 2256594259984366431
245
245
  required_rubygems_version: !ruby/object:Gem::Requirement
246
246
  none: false
247
247
  requirements: