copy-ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +10 -0
- data/LICENSE +23 -0
- data/README.md +232 -0
- data/Rakefile +8 -0
- data/copy.gemspec +24 -0
- data/lib/copy.rb +85 -0
- data/lib/copy/base.rb +43 -0
- data/lib/copy/client.rb +70 -0
- data/lib/copy/file.rb +134 -0
- data/lib/copy/link.rb +52 -0
- data/lib/copy/operations/activity.rb +27 -0
- data/lib/copy/operations/all.rb +31 -0
- data/lib/copy/operations/base.rb +47 -0
- data/lib/copy/operations/create.rb +20 -0
- data/lib/copy/operations/delete.rb +25 -0
- data/lib/copy/operations/find.rb +21 -0
- data/lib/copy/operations/meta.rb +20 -0
- data/lib/copy/operations/show.rb +20 -0
- data/lib/copy/operations/update.rb +21 -0
- data/lib/copy/request/base.rb +41 -0
- data/lib/copy/request/connection.rb +120 -0
- data/lib/copy/request/helpers.rb +36 -0
- data/lib/copy/request/info.rb +41 -0
- data/lib/copy/request/validator.rb +53 -0
- data/lib/copy/revision.rb +25 -0
- data/lib/copy/session.rb +56 -0
- data/lib/copy/user.rb +24 -0
- data/lib/copy/version.rb +3 -0
- data/spec/copy/base_spec.rb +12 -0
- data/spec/copy/client_spec.rb +69 -0
- data/spec/copy/file_spec.rb +306 -0
- data/spec/copy/link_spec.rb +238 -0
- data/spec/copy/request/base_spec.rb +53 -0
- data/spec/copy/request/connection_spec.rb +73 -0
- data/spec/copy/request/info_spec.rb +27 -0
- data/spec/copy/request/validator_spec.rb +13 -0
- data/spec/copy/revision_spec.rb +42 -0
- data/spec/copy/user_spec.rb +119 -0
- data/spec/copy_spec.rb +52 -0
- data/spec/fixtures/hola.txt +1 -0
- data/spec/spec_helper.rb +12 -0
- metadata +170 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a48afe7aa5c5ea2b0abd8e11f0270c4bd5920388
|
4
|
+
data.tar.gz: 74493f67ce12f9f779c3d81f989688ebabd781dd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b7a180c4ece7c8b4705a57247e6a6123ae4ae48bb003512f5fd136b9fcb52cb865286bf4f9e7a957d70bc0270a5f45b4e65d953a1ccb428eaad9733428a774ad
|
7
|
+
data.tar.gz: 2a919a45e70748b9d88b351451472ef8a24c9100a06841771eab38bdb74b7fd357c2a519e6392ffe191fa161f01bf235b4a92deba8ece48150c611fa1b2ecf51
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2012 Andres Bravo
|
4
|
+
Copyright (c) 2012 Andres Bravo Internet Service GmbH
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
a copy of this software and associated documentation files (the
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
20
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
21
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
22
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
23
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,232 @@
|
|
1
|
+
Copy [![Build Status](https://secure.travis-ci.org/andresbravog/copy-ruby.png)](https://travis-ci.org/andresbravog/copy-ruby) [![Code Climate](https://codeclimate.com/github/andresbravog/copy-ruby.png)](https://codeclimate.com/github/andresbravog/copy-ruby) [![Coverage Status](https://coveralls.io/repos/andresbravog/copy-ruby/badge.png)](https://coveralls.io/r/andresbravog/copy-ruby)
|
2
|
+
======
|
3
|
+
|
4
|
+
This is a Ruby wrapper for copy's API.
|
5
|
+
|
6
|
+
Documentation
|
7
|
+
=====
|
8
|
+
|
9
|
+
We use RubyDoc for documentation.
|
10
|
+
|
11
|
+
Usage
|
12
|
+
======
|
13
|
+
|
14
|
+
First, you've to install the gem
|
15
|
+
|
16
|
+
```Ruby
|
17
|
+
gem install copy
|
18
|
+
```
|
19
|
+
|
20
|
+
and require it
|
21
|
+
|
22
|
+
```Ruby
|
23
|
+
require "copy"
|
24
|
+
```
|
25
|
+
|
26
|
+
and set up your app credentials
|
27
|
+
|
28
|
+
|
29
|
+
```Ruby
|
30
|
+
Copy.config do |configuration|
|
31
|
+
configuration[:consumer_key] = '_your_consumer_key_'
|
32
|
+
configuration[:consumer_secret] = '_your_consumer_secret_'
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
in fact this last step is optional (yes! we support multiple applications) but if as most fo the humans you use only one copy app, this is the easyest way to go.
|
37
|
+
|
38
|
+
|
39
|
+
Oauth
|
40
|
+
=====
|
41
|
+
|
42
|
+
*[Copy oauth2 API documentation](https://www.copy.com/developer/documentation#authentication)*
|
43
|
+
|
44
|
+
using omniauth? :+1: good choice, just try this gem
|
45
|
+
|
46
|
+
*[plexinc/omniauth-copy](https://github.com/plexinc/omniauth-copy)*
|
47
|
+
|
48
|
+
not using omniauth,? no prob oauth implementation comming soon
|
49
|
+
|
50
|
+
...
|
51
|
+
|
52
|
+
|
53
|
+
Client
|
54
|
+
======
|
55
|
+
|
56
|
+
Everything starts with the client, once you have the user credentials you should create a session and a client to start interaction with the API
|
57
|
+
|
58
|
+
```Ruby
|
59
|
+
session = Copy::Session.new(
|
60
|
+
token: '_your_user_token_',
|
61
|
+
secret: '_your_user_secret_'
|
62
|
+
)
|
63
|
+
client = Copy::Client.new(session)
|
64
|
+
```
|
65
|
+
|
66
|
+
Now you can perform any user api call inside the clien wrapper
|
67
|
+
|
68
|
+
```Ruby
|
69
|
+
client.user(:show)
|
70
|
+
```
|
71
|
+
|
72
|
+
If you have multiple applications or you just want to ve explicit use the application credentials inside the session creation
|
73
|
+
|
74
|
+
```Ruby
|
75
|
+
session = Copy::Session.new(
|
76
|
+
token: '_your_user_token_',
|
77
|
+
secret: '_your_user_secret_',
|
78
|
+
consumer_key: '_your_app_key_',
|
79
|
+
consumer_secret: '_your_app_secret'
|
80
|
+
)
|
81
|
+
client = Copy::Client.new(session)
|
82
|
+
```
|
83
|
+
|
84
|
+
Users
|
85
|
+
=====
|
86
|
+
|
87
|
+
*[Copy users API documentation](https://www.copy.com/developer/documentation#api-calls/profile)*
|
88
|
+
|
89
|
+
Showing user profile:
|
90
|
+
|
91
|
+
```Ruby
|
92
|
+
user = client.user(:show)
|
93
|
+
```
|
94
|
+
|
95
|
+
Updating user (only last_name or first_name)
|
96
|
+
|
97
|
+
```Ruby
|
98
|
+
user = client.user(:update, { first_name: 'New name', last_name: 'New last name'})
|
99
|
+
```
|
100
|
+
|
101
|
+
Files
|
102
|
+
=====
|
103
|
+
|
104
|
+
*[Copy files API documentation](https://www.copy.com/developer/documentation#api-calls/filesystem)*
|
105
|
+
|
106
|
+
Showing root dir:
|
107
|
+
|
108
|
+
```Ruby
|
109
|
+
file = client.file(:show)
|
110
|
+
```
|
111
|
+
|
112
|
+
listing dir children:
|
113
|
+
|
114
|
+
files has children if is a dir and is not sutbbed (already being listed form his father)
|
115
|
+
|
116
|
+
```Ruby
|
117
|
+
if file.is_dir?
|
118
|
+
file = client.file(:show, id: file.id ) if file.stubbed?
|
119
|
+
file.children
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
get file revisions
|
124
|
+
|
125
|
+
```Ruby
|
126
|
+
file = client.file(:activity, id: '/copy/readme.txt')
|
127
|
+
revisions = file.revisions
|
128
|
+
file_revision = client.file(:show, id: revisions.first.id )
|
129
|
+
```
|
130
|
+
|
131
|
+
delete a file
|
132
|
+
|
133
|
+
```Ruby
|
134
|
+
client.file(:delete, id: '/test/readme.txt')
|
135
|
+
```
|
136
|
+
|
137
|
+
create a file. You need to provide a valid copy directory and a file (currently we accept File, Tempfile, String or StringIO objects )
|
138
|
+
|
139
|
+
```Ruby
|
140
|
+
file = client.file(:create, path: '/test/readme.txt', file: File.open('path/to/file'))
|
141
|
+
```
|
142
|
+
|
143
|
+
Links
|
144
|
+
=====
|
145
|
+
|
146
|
+
*[Copy links API documentation](https://www.copy.com/developer/documentation#api-calls/links)*
|
147
|
+
|
148
|
+
List all links created by the user
|
149
|
+
|
150
|
+
```Ruby
|
151
|
+
links = client.file(:all)
|
152
|
+
```
|
153
|
+
|
154
|
+
Show a link
|
155
|
+
|
156
|
+
```Ruby
|
157
|
+
link = client.file(:show, id: links.first.id )
|
158
|
+
```
|
159
|
+
|
160
|
+
List link recipients. Returns a list of users
|
161
|
+
|
162
|
+
```Ruby
|
163
|
+
link.recipients if !link.public
|
164
|
+
```
|
165
|
+
|
166
|
+
Create a new link
|
167
|
+
|
168
|
+
```Ruby
|
169
|
+
link = client.link(:create,
|
170
|
+
name: 'My new fancy link',
|
171
|
+
public: true,
|
172
|
+
paths: [
|
173
|
+
'/path/to/the/file_1.txt',
|
174
|
+
'/path/to/the/file_2.txt'
|
175
|
+
]
|
176
|
+
)
|
177
|
+
```
|
178
|
+
|
179
|
+
Delete a existing link
|
180
|
+
|
181
|
+
```Ruby
|
182
|
+
client.link(:delete, id: link.id )
|
183
|
+
```
|
184
|
+
|
185
|
+
Get metadata of a link
|
186
|
+
|
187
|
+
```Ruby
|
188
|
+
link = client.link(:meta, id: link.id )
|
189
|
+
files = link.children
|
190
|
+
```
|
191
|
+
|
192
|
+
Get a download link url
|
193
|
+
|
194
|
+
```Ruby
|
195
|
+
link = client.link(:show, id: link.id )
|
196
|
+
files = link.download_url
|
197
|
+
```
|
198
|
+
|
199
|
+
|
200
|
+
Documentation
|
201
|
+
=====
|
202
|
+
|
203
|
+
*[Copy developers page](https://www.copy.com/developer/signup/)*
|
204
|
+
|
205
|
+
*[Copy API documentation](https://www.copy.com/developer/documentation)*
|
206
|
+
|
207
|
+
|
208
|
+
Requirements
|
209
|
+
=====
|
210
|
+
|
211
|
+
This gem requires at least Ruby 1.9 and faces version 1 of Copy's API.
|
212
|
+
|
213
|
+
Bugs
|
214
|
+
======
|
215
|
+
|
216
|
+
Please report bugs at http://github.com/andresbravog/copy-ruby/issues.
|
217
|
+
|
218
|
+
Note on Patches/Pull Requests
|
219
|
+
======
|
220
|
+
|
221
|
+
* Fork the project from http://github.com/andresbravog/copy-ruby.
|
222
|
+
* Make your feature addition or bug fix.
|
223
|
+
* Add tests for it. This is important so I don't break it in a
|
224
|
+
future version unintentionally.
|
225
|
+
* Commit, do not mess with rakefile, version, or history.
|
226
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
227
|
+
* Send me a pull request. Bonus points for topic branches.
|
228
|
+
|
229
|
+
Copyright
|
230
|
+
======
|
231
|
+
|
232
|
+
Copyright (c) 2012-2013 andresbravog Internet Service, Andres Bravo. See LICENSE for details.
|
data/Rakefile
ADDED
data/copy.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "copy/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "copy-ruby"
|
7
|
+
s.version = Copy::VERSION
|
8
|
+
s.authors = ["Andres Bravo"]
|
9
|
+
s.email = ["hola@andresbravo.com"]
|
10
|
+
s.homepage = "https://github.com/andresbravog/copy-ruby"
|
11
|
+
s.summary = %q{API wrapper for Copy.}
|
12
|
+
s.description = %q{API wrapper for Copy.}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_dependency "json"
|
20
|
+
s.add_dependency 'oauth'
|
21
|
+
s.add_dependency(%q<multipart-post>, [">= 1.1.0"])
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_development_dependency "pry"
|
24
|
+
end
|
data/lib/copy.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "net/https"
|
3
|
+
require "json"
|
4
|
+
require "copy/version"
|
5
|
+
|
6
|
+
module Copy
|
7
|
+
DOMAIN_BASE = 'api'
|
8
|
+
API_BASE = 'copy.com'
|
9
|
+
API_BASE_PATH = 'rest'
|
10
|
+
API_VERSION = '1'
|
11
|
+
ROOT_PATH = File.dirname(__FILE__)
|
12
|
+
|
13
|
+
@@configuration = {}
|
14
|
+
|
15
|
+
autoload :Base, "copy/base"
|
16
|
+
autoload :User, "copy/user"
|
17
|
+
autoload :File, "copy/file"
|
18
|
+
autoload :Revision, "copy/revision"
|
19
|
+
autoload :Client, "copy/client"
|
20
|
+
autoload :Link, "copy/link"
|
21
|
+
autoload :Session, "copy/session"
|
22
|
+
|
23
|
+
module Operations
|
24
|
+
autoload :Base, "copy/operations/base"
|
25
|
+
autoload :All, "copy/operations/all"
|
26
|
+
autoload :Create, "copy/operations/create"
|
27
|
+
autoload :Delete, "copy/operations/delete"
|
28
|
+
autoload :Find, "copy/operations/find"
|
29
|
+
autoload :Show, "copy/operations/show"
|
30
|
+
autoload :Update, "copy/operations/update"
|
31
|
+
autoload :Activity, "copy/operations/activity"
|
32
|
+
autoload :Meta, "copy/operations/meta"
|
33
|
+
end
|
34
|
+
|
35
|
+
module Request
|
36
|
+
autoload :Base, "copy/request/base"
|
37
|
+
autoload :Connection, "copy/request/connection"
|
38
|
+
autoload :Helpers, "copy/request/helpers"
|
39
|
+
autoload :Info, "copy/request/info"
|
40
|
+
autoload :Validator, "copy/request/validator"
|
41
|
+
end
|
42
|
+
|
43
|
+
class CopyError < StandardError; end
|
44
|
+
class AuthenticationError < CopyError; end
|
45
|
+
class NotFound < CopyError; end
|
46
|
+
class APIError < CopyError; end
|
47
|
+
class ObjectNotFound < APIError; end
|
48
|
+
class BadRequest < APIError; end
|
49
|
+
|
50
|
+
|
51
|
+
# Gives configuration abilities
|
52
|
+
# to setup api_key and api_secret
|
53
|
+
#
|
54
|
+
# @example
|
55
|
+
# Copy.config do |configuration|
|
56
|
+
# configuration[:api_key] = '_your_api_key'
|
57
|
+
# configuration[:api_secret] = '_your_api_secret'
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# @return [String] The api key
|
61
|
+
def self.config(&block)
|
62
|
+
yield(@@configuration)
|
63
|
+
@@configuration
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.configuration
|
67
|
+
@@configuration
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.configuration=(value)
|
71
|
+
@@configuration = value
|
72
|
+
end
|
73
|
+
|
74
|
+
# Makes a request against the Copy API
|
75
|
+
#
|
76
|
+
# @param [Symbol] http_method The http method to use, must be one of :get, :post, :put and :delete
|
77
|
+
# @param [String] domain The API domain to use
|
78
|
+
# @param [String] api_url The API url to use
|
79
|
+
# @param [Hash] data The data to send, e.g. used when creating new objects.
|
80
|
+
# @return [Array] The parsed JSON response.
|
81
|
+
def self.request(http_method, domain, api_url, data, options={})
|
82
|
+
info = Request::Info.new(http_method, domain, api_url, data, options)
|
83
|
+
Request::Base.new(info).perform
|
84
|
+
end
|
85
|
+
end
|
data/lib/copy/base.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
module Copy
|
2
|
+
class Base
|
3
|
+
include Copy::Operations::Base
|
4
|
+
|
5
|
+
attr_accessor :created_time
|
6
|
+
|
7
|
+
# Initializes the object using the given attributes
|
8
|
+
#
|
9
|
+
# @param [Hash] attributes The attributes to use for initialization
|
10
|
+
def initialize(attributes = {})
|
11
|
+
set_attributes(attributes)
|
12
|
+
parse_timestamps
|
13
|
+
end
|
14
|
+
|
15
|
+
# Model validations
|
16
|
+
#
|
17
|
+
# @return [Boolean]
|
18
|
+
def valid?
|
19
|
+
self.errors.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
# Accesor for the errors
|
23
|
+
#
|
24
|
+
def errors
|
25
|
+
@errors || []
|
26
|
+
end
|
27
|
+
|
28
|
+
# Sets the attributes
|
29
|
+
#
|
30
|
+
# @param [Hash] attributes The attributes to initialize
|
31
|
+
def set_attributes(attributes)
|
32
|
+
attributes.each_pair do |key, value|
|
33
|
+
instance_variable_set("@#{key}", value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Parses UNIX timestamps and creates Time objects.
|
38
|
+
def parse_timestamps
|
39
|
+
@created_time = created_time.to_i if created_time.is_a? String
|
40
|
+
@created_time = Time.at(created_time) if created_time
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|