uploadcare-ruby 1.0.1.rc1

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.
@@ -0,0 +1,41 @@
1
+ require 'ostruct'
2
+
3
+ module Uploadcare
4
+ class Api
5
+ class FileList < OpenStruct
6
+ def initialize api, data
7
+ @api = api
8
+
9
+ unless data["results"].nil?
10
+ data["results"].map! do |file|
11
+ Uploadcare::Api::File.new @api, file["uuid"], file
12
+ end
13
+ end
14
+
15
+ super data
16
+ end
17
+
18
+ # Array-like behavior
19
+ def [] index
20
+ results[index] if defined?(:results)
21
+ end
22
+
23
+ def to_a
24
+ results if defined?(:results)
25
+ end
26
+
27
+ # List navigation
28
+ def next_page
29
+ @api.file_list(page+1) unless send(:next).nil?
30
+ end
31
+
32
+ def go_to index
33
+ @api.file_list(index) unless index > pages
34
+ end
35
+
36
+ def previous_page
37
+ @api.file_list(page-1) unless previous.nil?
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,116 @@
1
+ require 'ostruct'
2
+
3
+ module Uploadcare
4
+ class Api
5
+ class Group < OpenStruct
6
+ def initialize api, uuid_or_cdn_url, data=nil
7
+ result = Uploadcare::Parser.parse(uuid_or_cdn_url)
8
+
9
+ unless result.is_a?(Uploadcare::Parser::Group)
10
+ msg = "invalid CDN URL or UUID was given for group: #{uuid_or_cdn_url}."
11
+ if result.is_a?(Uploadcare::Parser::File)
12
+ msg = msg + "\n File UUID was given. Try call @api.file if it is what you intended."
13
+ end
14
+ raise msg
15
+ end
16
+
17
+ @api = api
18
+ # self.files_count = result["count"]
19
+ group = {uuid: result["uuid"], files_count: result["count"]}
20
+ super group
21
+
22
+ # if data is suplide - just pass it to builder.
23
+ set_data(data) if data
24
+ end
25
+
26
+
27
+ # Loading logic
28
+ def is_loaded?
29
+ !send(:files).nil?
30
+ end
31
+ alias_method :loaded?, :is_loaded?
32
+
33
+ def load_data
34
+ unless is_loaded?
35
+ load_data!
36
+ end
37
+ self
38
+ end
39
+ alias_method :load, :load_data
40
+
41
+ def load_data!
42
+ data = @api.get "/groups/#{uuid}/"
43
+ set_data data
44
+
45
+ self
46
+ end
47
+ alias_method :load!, :load_data!
48
+
49
+ # Store group (and all files in group)
50
+ def store
51
+ unless is_stored?
52
+ store!
53
+ end
54
+ self
55
+ end
56
+
57
+ def store!
58
+ data = @api.put "/groups/#{uuid}/storage/"
59
+ set_data(data)
60
+ self
61
+ end
62
+
63
+ def is_stored?
64
+ return nil unless is_loaded?
65
+ !send(:datetime_stored).nil?
66
+ end
67
+ alias_method :stored?, :is_stored?
68
+
69
+
70
+ ["created", "stored"].each do |dt|
71
+ define_method "datetime_#{dt}" do
72
+ date = @table["datetime_#{dt}".to_sym]
73
+ if date.is_a?(String)
74
+ begin
75
+ parsed = DateTime.parse(date)
76
+ self.send("datetime_#{dt}=", parsed)
77
+ parsed
78
+ rescue Exception => e
79
+ date
80
+ end
81
+ else
82
+ date
83
+ end
84
+ end
85
+ end
86
+
87
+
88
+ private
89
+ def set_data data
90
+ data = map_files(data) unless data["files"].nil?
91
+
92
+ if data.respond_to? (:each)
93
+ data.each do |k, v|
94
+ self.send "#{k}=", v
95
+ end
96
+ end
97
+
98
+ @is_loaded = true
99
+ end
100
+
101
+ # map files (hashes basicly) to
102
+ # actual File objects
103
+ def map_files data
104
+ data["files"].map! do |file|
105
+ unless file.nil?
106
+ Uploadcare::Api::File.new(@api, file["uuid"], file)
107
+ else
108
+ file
109
+ end
110
+ end
111
+
112
+ data
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,31 @@
1
+ require 'ostruct'
2
+
3
+ module Uploadcare
4
+ class Api
5
+ class GroupList < OpenStruct
6
+ def initialize api, data
7
+ @api = api
8
+
9
+ unless data["results"].nil?
10
+ data["results"].map! do |group|
11
+ Uploadcare::Api::Group.new @api, group["id"], group
12
+ end
13
+ end
14
+
15
+ super data
16
+ end
17
+
18
+ def [] index
19
+ results[index] if defined?(:results)
20
+ end
21
+
22
+ def to_a
23
+ results if defined?(:results)
24
+ end
25
+
26
+ def groups
27
+ results if defined?(:results)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ require 'ostruct'
2
+
3
+ module Uploadcare
4
+ class Api
5
+ class Project < OpenStruct
6
+ def initialize api
7
+ @api = api
8
+ data = @api.get "/project/"
9
+ super data
10
+ end
11
+
12
+ # def load data
13
+ # if data.respond_to? :each
14
+ # data.each do |key, value|
15
+ # self.send "#{key}=", value
16
+ # end
17
+ # end
18
+ # end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Uploadcare
2
+ VERSION = "1.0.1.rc1"
3
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+ require 'uri'
3
+ require 'socket'
4
+
5
+ describe Uploadcare::Api::File do
6
+ before :each do
7
+ @api = Uploadcare::Api.new(CONFIG)
8
+ @url = "http://macaw.co/images/macaw-logo.png"
9
+ @list = @api.file_list 1
10
+ end
11
+
12
+ it "should return file list" do
13
+ @list.should be_kind_of(Uploadcare::Api::FileList)
14
+ end
15
+
16
+ it "should respont to results method" do
17
+ @list.should respond_to :results
18
+ end
19
+
20
+ it "results should be an array" do
21
+ @list.results.should be_kind_of(Array)
22
+ end
23
+
24
+ it "resulst should be UC files" do
25
+ @list.results.each do |file|
26
+ file.should be_kind_of(Uploadcare::Api::File)
27
+ end
28
+ end
29
+
30
+ it "results should be not only files, but loaded files" do
31
+ @list.results.each do |file|
32
+ file.is_loaded?.should be_true
33
+ end
34
+ end
35
+
36
+ it "should load next page" do
37
+ next_page = @list.next_page
38
+ next_page.should be_kind_of(Uploadcare::Api::FileList)
39
+ end
40
+
41
+ it "should load prev page" do
42
+ list = @api.file_list 3
43
+ prev_page = list.previous_page
44
+ prev_page.should be_kind_of(Uploadcare::Api::FileList)
45
+ end
46
+
47
+ it "should load custom page" do
48
+ page = @list.go_to(@list.pages - 1)
49
+ page.should be_kind_of(Uploadcare::Api::FileList)
50
+ end
51
+
52
+ it "should not load next page if there isn't one" do
53
+ page= @list.go_to @list.pages
54
+ page.next_page.should be_nil
55
+ end
56
+
57
+ it "should not load prev page if there isn't one" do
58
+ @list.previous_page.should be_nil
59
+ end
60
+
61
+ it "should not load custom page with index more than there is actually page in project" do
62
+ page = @list.go_to @list.pages + 3
63
+ page.should be_nil
64
+ end
65
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+ require 'uri'
3
+ require 'socket'
4
+
5
+ describe Uploadcare::Api::File do
6
+ before :each do
7
+ @api = Uploadcare::Api.new(CONFIG)
8
+ @url = "http://macaw.co/images/macaw-logo.png"
9
+ @file = @api.upload @url
10
+ end
11
+
12
+ it 'file should be an instance of File' do
13
+ @file.should be_an_instance_of Uploadcare::Api::File
14
+ end
15
+
16
+ it 'should not be initialized without correct UUID given' do
17
+ expect {Uploadcare::Api::File.new(@api, "not-uuid")}.to raise_error
18
+ end
19
+
20
+ it 'should have valid url' do
21
+ @file.uuid.should match UUID_REGEX
22
+ end
23
+
24
+ it 'should return public url' do
25
+ @file.should respond_to :cdn_url
26
+ @file.should respond_to :public_url
27
+ end
28
+
29
+ it 'public url should be valid url' do
30
+ url = @file.cdn_url
31
+ uri = URI.parse(url)
32
+ uri.should be_kind_of(URI::HTTP)
33
+ end
34
+
35
+ it 'should be able to load image data' do
36
+ expect {@file.load_data}.to_not raise_error
37
+ end
38
+
39
+ it 'should store itself' do
40
+ expect { @file.store }.to_not raise_error
41
+ end
42
+
43
+ it 'file should respond with nil for :stored? and :deleted? methods unless loaded' do
44
+ @file.is_loaded?.should == false
45
+ @file.is_stored?.should == nil
46
+ @file.is_deleted?.should == nil
47
+ end
48
+
49
+ it 'should be able to tell thenever file was stored' do
50
+ @file.load
51
+ @file.is_stored?.should == false
52
+ @file.store
53
+ @file.is_stored?.should == true
54
+ end
55
+
56
+ it 'should delete itself' do
57
+ expect { @file.delete }.to_not raise_error
58
+ end
59
+
60
+ it 'should be able to tell thenever file was deleted' do
61
+ @file.load
62
+ @file.is_deleted?.should == false
63
+ @file.delete
64
+ @file.is_deleted?.should == true
65
+ end
66
+
67
+ it 'should construct file from uuid' do
68
+ file = @api.file @file.uuid
69
+ file.should be_kind_of(Uploadcare::Api::File)
70
+ end
71
+
72
+ it 'should construct file from cdn url' do
73
+ url = @file.cdn_url + "-/crop/150x150/center/-/format/png/"
74
+ file = @api.file url
75
+ file.should be_kind_of(Uploadcare::Api::File)
76
+ end
77
+
78
+ it 'shoul respond to datetime_ methods' do
79
+ @file.load
80
+ @file.should respond_to(:datetime_original)
81
+ @file.should respond_to(:datetime_uploaded)
82
+ @file.should respond_to(:datetime_stored)
83
+ @file.should respond_to(:datetime_removed)
84
+ end
85
+
86
+ it 'should respond to datetime_uploaded' do
87
+ @file.load
88
+ @file.datetime_uploaded.should be_kind_of(DateTime)
89
+ end
90
+
91
+ it 'should respond to datetime_stored' do
92
+ @file.load
93
+ @file.store
94
+ @file.datetime_stored.should be_kind_of(DateTime)
95
+ end
96
+
97
+ it 'should respond to datetime_removed' do
98
+ @file.load
99
+ @file.delete
100
+ @file.datetime_removed.should be_kind_of(DateTime)
101
+ @file.datetime_deleted.should be_kind_of(DateTime)
102
+ @file.datetime_removed.should == @file.datetime_deleted
103
+ end
104
+
105
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'uri'
3
+ require 'socket'
4
+
5
+ describe Uploadcare::Api::File do
6
+ before :each do
7
+ @api = Uploadcare::Api.new(CONFIG)
8
+ @url = "http://macaw.co/images/macaw-logo.png"
9
+ @list = @api.group_list
10
+ end
11
+
12
+ it "basic group list" do
13
+ @list.should be_kind_of Uploadcare::Api::GroupList
14
+ end
15
+
16
+ it "should contain groups and results" do
17
+ @list.should respond_to(:results)
18
+ @list.should respond_to(:groups)
19
+ @list.groups.should be_kind_of(Array)
20
+ end
21
+
22
+ it "results should contain groups" do
23
+ group = @list.groups.sample
24
+ group.should be_kind_of(Uploadcare::Api::Group)
25
+ end
26
+
27
+ it "group should no be loaded" do
28
+ group = @list.groups.sample
29
+ group.is_loaded?.should == false
30
+ end
31
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+ require 'uri'
3
+ require 'socket'
4
+
5
+ describe Uploadcare::Api::Group do
6
+ before :each do
7
+ @api = Uploadcare::Api.new(CONFIG)
8
+ @file = File.open(File.join(File.dirname(__FILE__), 'view.png'))
9
+ @file2 = File.open(File.join(File.dirname(__FILE__), 'view2.jpg'))
10
+ @files_ary = [@file, @file2]
11
+ @files = @api.upload @files_ary
12
+ end
13
+
14
+ it "should return group object" do
15
+ group = @api.create_group @files
16
+ group.should be_kind_of(Uploadcare::Api::Group)
17
+ end
18
+
19
+ it "should have valid UUID and count of files" do
20
+ group = @api.create_group @files
21
+ group.should respond_to(:uuid)
22
+ group.should respond_to(:files_count)
23
+ end
24
+
25
+ it "should may have files" do
26
+ group = @api.create_group @files
27
+ group.should respond_to(:files)
28
+ group.files.should be_kind_of(Array)
29
+ group.files.each do |file|
30
+ file.should be_kind_of(Uploadcare::Api::File)
31
+ end
32
+ end
33
+
34
+ it "should create group by id" do
35
+ group = @api.create_group @files
36
+
37
+ expect {group_uloaded = @api.group group.uuid}.to_not raise_error
38
+ end
39
+
40
+ it "should create loaded and unloaded groups" do
41
+ group = @api.create_group @files
42
+ group_uloaded = @api.group group.uuid
43
+ group.is_loaded?.should be_true
44
+ group_uloaded.is_loaded?.should be_false
45
+ end
46
+
47
+ it "group should load data" do
48
+ group = @api.create_group @files
49
+ group_uloaded = @api.group group.uuid
50
+ group_uloaded.should respond_to(:load_data)
51
+ expect {group_uloaded.load_data}.to_not raise_error
52
+ group_uloaded.is_loaded?.should be_true
53
+ end
54
+
55
+ it "group should store itself" do
56
+ group = @api.create_group @files
57
+ expect {group.store}.to_not raise_error
58
+ end
59
+
60
+ it "should be able to tell when group is stored" do
61
+ group = @api.create_group @files
62
+ group_unloaded = @api.group group.uuid
63
+
64
+ group_unloaded.is_loaded?.should == false
65
+ group_unloaded.is_stored?.should == nil
66
+
67
+ group_unloaded.load
68
+ group_unloaded.is_stored?.should == false
69
+
70
+ group_unloaded.store
71
+ group_unloaded.is_stored?.should == true
72
+ end
73
+
74
+ it "group should have datetime attributes" do
75
+ group = @api.create_group @files
76
+ group.should respond_to(:datetime_created)
77
+ group.should respond_to(:datetime_stored)
78
+ end
79
+
80
+ it "group should have datetime_created as DateTime object" do
81
+ group = @api.create_group @files
82
+ group.datetime_created.should be_kind_of(DateTime)
83
+ end
84
+
85
+ it "group should have datetime_created as DateTime object" do
86
+ group = @api.create_group @files
87
+ group.store
88
+ group.datetime_stored.should be_kind_of(DateTime)
89
+ end
90
+ end