crate_api 0.1.0 → 0.1.1
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 +1 -1
- data/crate_api.gemspec +2 -2
- data/lib/crate_api/base.rb +36 -1
- data/lib/crate_api/crate.rb +14 -0
- data/lib/crate_api/crateobject.rb +17 -2
- data/lib/crate_api/crates.rb +18 -0
- data/lib/crate_api/item.rb +11 -0
- data/lib/crate_api/items.rb +6 -1
- data/lib/crate_api.rb +21 -0
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/crate_api.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{crate_api}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Brian Michel"]
|
12
|
-
s.date = %q{2011-04-
|
12
|
+
s.date = %q{2011-04-24}
|
13
13
|
s.description = %q{This gem will allow for easy interaction with the Crate API to share your files!}
|
14
14
|
s.email = %q{brian.michel@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/crate_api/base.rb
CHANGED
@@ -1,25 +1,55 @@
|
|
1
1
|
module CrateAPI
|
2
|
+
# NotValidUserError class which is raised due to bad credentials.
|
2
3
|
class NotValidUserError < Exception
|
4
|
+
|
3
5
|
end
|
4
|
-
|
6
|
+
|
7
|
+
# Base class which is used to authenticate user and perform calls.
|
8
|
+
#
|
5
9
|
class Base
|
6
10
|
include HTTMultiParty
|
7
11
|
|
12
|
+
# @attr [Hash] :auth auth hash.
|
8
13
|
attr_accessor :auth
|
14
|
+
# Current API Version.
|
9
15
|
API_VERSION = 1
|
16
|
+
# Base URL Endpoint
|
10
17
|
BASE_URL = "https://api.letscrate.com/#{API_VERSION}"
|
18
|
+
# Authorization URL Endpoint.
|
11
19
|
AUTH_URL = "#{BASE_URL}/users/authenticate.json"
|
20
|
+
# Items URL Endpoint.
|
12
21
|
ITEMS_URL = "#{BASE_URL}/files"
|
22
|
+
# Crates URL Endpoint.
|
13
23
|
CRATES_URL = "#{BASE_URL}/crates"
|
24
|
+
# Short URL Placeholder String.
|
14
25
|
SHORT_URL = "http://lts.cr/%s"
|
26
|
+
|
27
|
+
# Default initializer for getting a Crates instance.
|
28
|
+
# @return [CrateAPI::Crates] a new Crates instance.
|
15
29
|
def crates(); @crates || CrateAPI::Crates.new(); end
|
30
|
+
|
31
|
+
# Default initializer for getting a Items instance.
|
32
|
+
# @return [CrateAPI::Items] a new Items instance.
|
16
33
|
def items(); @items || CrateAPI::Items.new(); end
|
17
34
|
|
35
|
+
# Default initializer for the CrateAPI Client
|
36
|
+
#
|
37
|
+
# @param [String] username username for the Crate user.
|
38
|
+
# @param [String] password password for the Crate user.
|
39
|
+
# @return [CrateAPI::Base] this will return the base class instance.
|
40
|
+
# @raise [NotValidUserError] this will occur if the username/password pair is not valid.
|
18
41
|
def initialize(username, password)
|
19
42
|
raise NotValidUserError unless CrateAPI::Base.authorized?(username, password)
|
20
43
|
@@auth = {:username => username, :password => password}
|
21
44
|
end
|
22
45
|
|
46
|
+
|
47
|
+
# Class method that return the response body of the call.
|
48
|
+
#
|
49
|
+
# @param [String] url URL endpoint to make the call to.
|
50
|
+
# @param [Symbol] verb HTTP verb used for call.
|
51
|
+
# @param [Optional] params Hash of params used for call.
|
52
|
+
# @return [Object] body object from the response.
|
23
53
|
def self.call(url, verb, params={})
|
24
54
|
params.merge!({:basic_auth => @@auth})
|
25
55
|
resp = nil
|
@@ -34,6 +64,11 @@ module CrateAPI
|
|
34
64
|
end
|
35
65
|
end
|
36
66
|
|
67
|
+
# Class method that will return a boolean whether or not the user is authorized.
|
68
|
+
#
|
69
|
+
# @param [String] username Username to test for authorization.
|
70
|
+
# @param [String] pass Password to test for authorization.
|
71
|
+
# @return [Boolean] whether or not the user is authorized.
|
37
72
|
def self.authorized?(user, pass)
|
38
73
|
resp = self.get("#{AUTH_URL}", {:basic_auth => {:username => user, :password => pass}})
|
39
74
|
if resp.code == 401
|
data/lib/crate_api/crate.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
module CrateAPI
|
2
|
+
# CrateRenameError class which is raised when there is a problem renaming a given Crate.
|
2
3
|
class CrateRenameError < Exception
|
3
4
|
end
|
4
5
|
|
6
|
+
# CrateDestroyError class which is raised when there is a problem destroying a given Crate.
|
5
7
|
class CrateDestroyError < Exception
|
6
8
|
end
|
7
9
|
|
10
|
+
# CrateFileAlreadyExistsError class which is raised when there is a problem uploading a file to a given Crate.
|
8
11
|
class CrateFileAlreadyExistsError < Exception
|
9
12
|
end
|
10
13
|
|
14
|
+
# Crate object class which is used to manipulate the single Crate object.
|
11
15
|
class Crate < CrateObject
|
12
16
|
attr_reader :files
|
13
17
|
def initialize(hash)
|
@@ -15,16 +19,26 @@ module CrateAPI
|
|
15
19
|
@files = CrateAPI::Items.from_array(hash["files"])
|
16
20
|
end
|
17
21
|
|
22
|
+
# Destroys the given crate object.
|
23
|
+
#
|
24
|
+
# @return [CrateDestroyError, nil] if there is an issue destroying the crate, an error will be raised with the message explaining why.
|
18
25
|
def destroy
|
19
26
|
response = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CrateAPI::Crates::CRATE_ACTIONS[:destroy] % ["#{self.id}"]}", :post))
|
20
27
|
raise CrateDestroyError, response["message"] unless response["status"] != "failure"
|
21
28
|
end
|
22
29
|
|
30
|
+
# Renamed the given crate object.
|
31
|
+
#
|
32
|
+
# @return [CrateRenameError, nil] if there is an issue with renaming the crate, an error will be raised with the message explaining why.
|
23
33
|
def rename(name)
|
24
34
|
response = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CrateAPI::Crates::CRATE_ACTIONS[:rename] % ["#{self.id}"]}", :post, {:body => {:name => name}}))
|
25
35
|
raise CrateRenameError, response["message"] unless response["status"] != "failure"
|
26
36
|
end
|
27
37
|
|
38
|
+
# Add a file to the given crate object.
|
39
|
+
#
|
40
|
+
# @param [String] This is the path to the file that you wish to upload.
|
41
|
+
# @return [CrateFileAlreadyExistsError, nil] if there is an issue uploading the file to the crate, an error will be raised with the message explaining why.
|
28
42
|
def add_file(path)
|
29
43
|
file = File.new(path)
|
30
44
|
response = CrateAPI::Base.call("#{CrateAPI::Base::ITEMS_URL}/#{CrateAPI::Items::ITEM_ACTIONS[:upload]}", :post, {:body => {:file => file, :crate_id => @id}})
|
@@ -1,13 +1,28 @@
|
|
1
1
|
module CrateAPI
|
2
|
+
# Base class for Crate and Item objects
|
3
|
+
# @abstract Subclass in order to get the basic +:short_code+ , +:name+ , +:id+ params that every object should have.
|
2
4
|
class CrateObject
|
5
|
+
# @attr_reader [String] short_code short code for constructing the short url.
|
6
|
+
# @attr_reader [String] name name of the CrateObject that is being accessed.
|
7
|
+
# @attr_reader [Integer] id the numeric id of the CrateObject that is being accessed.
|
3
8
|
attr_reader :short_code, :name, :id
|
4
|
-
|
9
|
+
|
10
|
+
# The default initializer for the CrateObject class
|
11
|
+
#
|
12
|
+
# @param [Hash] a hash of values used to initialize the CrateObject.
|
13
|
+
# @option opts [String] :short_code short code for short url
|
14
|
+
# @option opts [String] :name name of the object
|
15
|
+
# @option opts [Integer] :id id of the object
|
16
|
+
# @return [CrateObject] this will return an initialized CrateObject instance.
|
5
17
|
def initialize(hash)
|
6
18
|
@short_code = hash["short_code"]
|
7
19
|
@name = hash["name"]
|
8
20
|
@id = hash["id"]
|
9
21
|
end
|
10
|
-
|
22
|
+
|
23
|
+
# Will return the shortened url of a given object
|
24
|
+
#
|
25
|
+
# @return [String] shortened url string for a given object.
|
11
26
|
def short_url
|
12
27
|
return "#{CrateAPI::Base::SHORT_URL}" % ["#{@short_code}"]
|
13
28
|
end
|
data/lib/crate_api/crates.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
module CrateAPI
|
2
|
+
# CrateLimitReachedError class which is raised when the user can no longer add a crate to their account due to restriction.
|
2
3
|
class CrateLimitReachedError < Exception
|
3
4
|
end
|
4
5
|
|
6
|
+
# Crates class which is used to add, list, manipulate the Crates on a given account.
|
5
7
|
class Crates
|
8
|
+
# Hash of available actions to take upon the Crates endpoint.
|
6
9
|
CRATE_ACTIONS = {
|
7
10
|
:add => "add.json",
|
8
11
|
:list => "list.json",
|
@@ -10,21 +13,36 @@ module CrateAPI
|
|
10
13
|
:destroy => "destroy/%s.json"
|
11
14
|
}
|
12
15
|
|
16
|
+
# Add a new crate.
|
17
|
+
#
|
18
|
+
# @param [String] name of the crate to add.
|
19
|
+
# @return [nil] no output from this method is expected if everything goes right.
|
13
20
|
def add(name)
|
14
21
|
response = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CRATE_ACTIONS[:add]}", :post, {:body => {:name => name}}))
|
15
22
|
raise CrateLimitReachedError, response["message"] unless response["status"] != "failure"
|
16
23
|
end
|
17
24
|
|
25
|
+
# Get a list of crates (w/o files).
|
26
|
+
#
|
27
|
+
# @return [Array] an array of crate objects.
|
28
|
+
# @note This will not return crate objects with the files they contain.
|
29
|
+
# @see Crates#all
|
18
30
|
def list
|
19
31
|
hash = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CRATE_ACTIONS[:list]}", :get))
|
20
32
|
return Crates.from_array(hash["crates"])
|
21
33
|
end
|
22
34
|
|
35
|
+
# Get a list of crates (w/ files).
|
36
|
+
#
|
37
|
+
# @return [Array] an array of crate objects.
|
23
38
|
def all
|
24
39
|
hash = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::ITEMS_URL}/#{CRATE_ACTIONS[:list]}", :get))
|
25
40
|
return Crates.from_array(hash["crates"])
|
26
41
|
end
|
27
42
|
|
43
|
+
# Class method to return an array of crate objects.
|
44
|
+
#
|
45
|
+
# @return [Array] an array of initialized Crate objects.
|
28
46
|
def self.from_array(array)
|
29
47
|
return [] unless array != nil
|
30
48
|
crates = Array.new
|
data/lib/crate_api/item.rb
CHANGED
@@ -1,13 +1,24 @@
|
|
1
1
|
module CrateAPI
|
2
|
+
# FileDestroyError which is raised when there is an issue destroying the file.
|
2
3
|
class FileDestroyError < Exception
|
3
4
|
end
|
5
|
+
|
6
|
+
# Item class which is used to manipulate and represent a file blob which is inside of a Crate.
|
4
7
|
class Item < CrateObject
|
5
8
|
attr_reader :size
|
9
|
+
# Default initializer for the Item object.
|
10
|
+
#
|
11
|
+
# @param [Hash] hash an item hash.
|
12
|
+
# @return [CrateAPI::Item] a fully initialized Item object.
|
6
13
|
def initialize(hash)
|
7
14
|
super(hash)
|
8
15
|
@size = hash["size"]
|
9
16
|
end
|
10
17
|
|
18
|
+
# Will destroy the given file.
|
19
|
+
#
|
20
|
+
# @return [nil] this method should return nil if there are no issues.
|
21
|
+
# @raise [FileDestroyError] an error and message describing what happened.
|
11
22
|
def destroy
|
12
23
|
response = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::ITEMS_URL}/#{CrateAPI::Items::ITEM_ACTIONS[:destroy] % ["#{self.id}"]}", :post))
|
13
24
|
raise FileDestroyError, response["message"] unless response["status"] != "failure"
|
data/lib/crate_api/items.rb
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
module CrateAPI
|
2
|
+
# Items class which is used to get a set of items from the service.
|
2
3
|
class Items
|
4
|
+
# Hash of available actions to take upon the Items endpoint.
|
3
5
|
ITEM_ACTIONS = {
|
4
6
|
:upload => "upload.json",
|
5
7
|
:list => "list.json",
|
6
8
|
:show => "show/%s.json",
|
7
9
|
:destroy => "destroy/%s.json"
|
8
10
|
}
|
9
|
-
|
11
|
+
# Creates an array of Item objects.
|
12
|
+
#
|
13
|
+
# @param [Array] array to be used as the generation source.
|
14
|
+
# @return [Array] either an empty array if the input array is nil or a fully inialized array of Item objects.
|
10
15
|
def self.from_array(array)
|
11
16
|
return [] unless array != nil
|
12
17
|
files = Array.new
|
data/lib/crate_api.rb
CHANGED
@@ -5,7 +5,28 @@ require "JSON"
|
|
5
5
|
require File.join(File.dirname(__FILE__), "crate_api", inc)
|
6
6
|
end
|
7
7
|
|
8
|
+
# The CrateAPI Gem is a lightweight Ruby interface to the Crate file-sharing site.
|
9
|
+
# @author Brian Michel
|
10
|
+
# A few examples will follow in order to get everyone up to speed.
|
11
|
+
#
|
12
|
+
# @example Create a new Crate client.
|
13
|
+
# client = CrateAPI.new("username", "password")
|
14
|
+
#
|
15
|
+
# @example Retreive all crates for a user.
|
16
|
+
# crates = client.crates.all
|
17
|
+
#
|
18
|
+
# @example Create a new crate.
|
19
|
+
# client.crate.add("YourNewAwesomeCrateName")
|
20
|
+
#
|
21
|
+
# @example Upload a new file to a crate.
|
22
|
+
# crates[0].add_file("/Path/to/your/file")
|
23
|
+
#
|
8
24
|
module CrateAPI
|
25
|
+
# Default initializer for a new CrateAPI instace
|
26
|
+
#
|
27
|
+
# @param [String] Username for the client.
|
28
|
+
# @param [String] password Password for the client.
|
29
|
+
# @return [CrateAPI::Base] newly initialized CrateAPI::Base instace.
|
9
30
|
def self.new(username, password)
|
10
31
|
CrateAPI::Base.new(username, password)
|
11
32
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: crate_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Brian Michel
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-24 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: shoulda
|
@@ -118,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
118
118
|
requirements:
|
119
119
|
- - ">="
|
120
120
|
- !ruby/object:Gem::Version
|
121
|
-
hash:
|
121
|
+
hash: 4422052285909506318
|
122
122
|
segments:
|
123
123
|
- 0
|
124
124
|
version: "0"
|