crate_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.2)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Brian Michel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ Crate API
2
+ ----------
3
+
4
+ This is a light ruby wrapper for the Crate filesharing API
5
+
6
+ Contributing to Crate API
7
+ -------------------------
8
+
9
+ * You know the drill, fork, change, pull request if you so like!
10
+
11
+ Things undone
12
+ -------------
13
+
14
+ * The file part of the api is as of yet undone.
15
+ * Proper documentation which I'll be doing soon
16
+ * Tests to verify that everything is working
17
+ * There is probably some weirdness with the exception handling, will take another look
18
+
19
+ Usage
20
+ -----
21
+ Require
22
+
23
+ require 'crate_api'
24
+ Create a client!
25
+
26
+ client = CrateAPI.new("username", "password")
27
+ Get some crates!
28
+
29
+ crates = client.crates.all
30
+ Look at their files!
31
+
32
+ crates[0].files
33
+ All crate and item object support getting their short url so...
34
+
35
+ crates[0].short_url || crates[0].files[0].short_url
36
+ Add a file to a crate!
37
+
38
+ crates[0].add_file("/path/to/your/file")
39
+ Destroy a crate or file!
40
+
41
+ crates[0].destroy - NOTE: this will destroy all of the files contained within without warning
42
+ crates[0].files[0].destroy
43
+ Add a crate!
44
+
45
+ client.crates.add("NameOfYourAwesomeCrate")
46
+ That's about it for now!
47
+
48
+ Copyright
49
+ ---------
50
+
51
+ Copyright (c) 2011 Brian Michel. See LICENSE.txt for
52
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "crate_api"
16
+ gem.homepage = "http://github.com/brianmichel/crate_api"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Interface with the Crate API}
19
+ gem.description = %Q{This gem will allow for easy interaction with the Crate API to share your files!}
20
+ gem.email = "brian.michel@gmail.com"
21
+ gem.authors = ["Brian Michel"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ gem.add_runtime_dependency 'httmultiparty', '>= 0.3.0'
27
+ gem.add_runtime_dependency 'json', '>= 1.5.1'
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ require 'rcov/rcovtask'
39
+ Rcov::RcovTask.new do |test|
40
+ test.libs << 'test'
41
+ test.pattern = 'test/**/test_*.rb'
42
+ test.verbose = true
43
+ end
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "crate_api #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/crate_api.gemspec ADDED
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{crate_api}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brian Michel"]
12
+ s.date = %q{2011-04-23}
13
+ s.description = %q{This gem will allow for easy interaction with the Crate API to share your files!}
14
+ s.email = %q{brian.michel@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "crate_api.gemspec",
28
+ "lib/crate_api.rb",
29
+ "lib/crate_api/base.rb",
30
+ "lib/crate_api/crate.rb",
31
+ "lib/crate_api/crateobject.rb",
32
+ "lib/crate_api/crates.rb",
33
+ "lib/crate_api/item.rb",
34
+ "lib/crate_api/items.rb",
35
+ "test/helper.rb",
36
+ "test/test_crate_api.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/brianmichel/crate_api}
39
+ s.licenses = ["MIT"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.7.2}
42
+ s.summary = %q{Interface with the Crate API}
43
+ s.test_files = [
44
+ "test/helper.rb",
45
+ "test/test_crate_api.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
53
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
55
+ s.add_development_dependency(%q<rcov>, [">= 0"])
56
+ s.add_runtime_dependency(%q<httmultiparty>, [">= 0.3.0"])
57
+ s.add_runtime_dependency(%q<json>, [">= 1.5.1"])
58
+ else
59
+ s.add_dependency(%q<shoulda>, [">= 0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ s.add_dependency(%q<httmultiparty>, [">= 0.3.0"])
64
+ s.add_dependency(%q<json>, [">= 1.5.1"])
65
+ end
66
+ else
67
+ s.add_dependency(%q<shoulda>, [">= 0"])
68
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
69
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
70
+ s.add_dependency(%q<rcov>, [">= 0"])
71
+ s.add_dependency(%q<httmultiparty>, [">= 0.3.0"])
72
+ s.add_dependency(%q<json>, [">= 1.5.1"])
73
+ end
74
+ end
75
+
@@ -0,0 +1,45 @@
1
+ module CrateAPI
2
+ class NotValidUserError < Exception
3
+ end
4
+
5
+ class Base
6
+ include HTTMultiParty
7
+
8
+ attr_accessor :auth
9
+ API_VERSION = 1
10
+ BASE_URL = "https://api.letscrate.com/#{API_VERSION}"
11
+ AUTH_URL = "#{BASE_URL}/users/authenticate.json"
12
+ ITEMS_URL = "#{BASE_URL}/files"
13
+ CRATES_URL = "#{BASE_URL}/crates"
14
+ SHORT_URL = "http://lts.cr/%s"
15
+ def crates(); @crates || CrateAPI::Crates.new(); end
16
+ def items(); @items || CrateAPI::Items.new(); end
17
+
18
+ def initialize(username, password)
19
+ raise NotValidUserError unless CrateAPI::Base.authorized?(username, password)
20
+ @@auth = {:username => username, :password => password}
21
+ end
22
+
23
+ def self.call(url, verb, params={})
24
+ params.merge!({:basic_auth => @@auth})
25
+ resp = nil
26
+ case verb
27
+ when :get
28
+ resp = self.get(url, params)
29
+ when :post
30
+ resp = self.post(url, params)
31
+ end
32
+ if resp.code == 200
33
+ return resp.body
34
+ end
35
+ end
36
+
37
+ def self.authorized?(user, pass)
38
+ resp = self.get("#{AUTH_URL}", {:basic_auth => {:username => user, :password => pass}})
39
+ if resp.code == 401
40
+ return false
41
+ end
42
+ return true
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,34 @@
1
+ module CrateAPI
2
+ class CrateRenameError < Exception
3
+ end
4
+
5
+ class CrateDestroyError < Exception
6
+ end
7
+
8
+ class CrateFileAlreadyExistsError < Exception
9
+ end
10
+
11
+ class Crate < CrateObject
12
+ attr_reader :files
13
+ def initialize(hash)
14
+ super(hash)
15
+ @files = CrateAPI::Items.from_array(hash["files"])
16
+ end
17
+
18
+ def destroy
19
+ response = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CrateAPI::Crates::CRATE_ACTIONS[:destroy] % ["#{self.id}"]}", :post))
20
+ raise CrateDestroyError, response["message"] unless response["status"] != "failure"
21
+ end
22
+
23
+ def rename(name)
24
+ response = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CrateAPI::Crates::CRATE_ACTIONS[:rename] % ["#{self.id}"]}", :post, {:body => {:name => name}}))
25
+ raise CrateRenameError, response["message"] unless response["status"] != "failure"
26
+ end
27
+
28
+ def add_file(path)
29
+ file = File.new(path)
30
+ response = CrateAPI::Base.call("#{CrateAPI::Base::ITEMS_URL}/#{CrateAPI::Items::ITEM_ACTIONS[:upload]}", :post, {:body => {:file => file, :crate_id => @id}})
31
+ raise CrateFileAlreadyExistsError, response["message"] unless response["status"] != "failure"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ module CrateAPI
2
+ class CrateObject
3
+ attr_reader :short_code, :name, :id
4
+
5
+ def initialize(hash)
6
+ @short_code = hash["short_code"]
7
+ @name = hash["name"]
8
+ @id = hash["id"]
9
+ end
10
+
11
+ def short_url
12
+ return "#{CrateAPI::Base::SHORT_URL}" % ["#{@short_code}"]
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ module CrateAPI
2
+ class CrateLimitReachedError < Exception
3
+ end
4
+
5
+ class Crates
6
+ CRATE_ACTIONS = {
7
+ :add => "add.json",
8
+ :list => "list.json",
9
+ :rename => "rename/%s.json",
10
+ :destroy => "destroy/%s.json"
11
+ }
12
+
13
+ def add(name)
14
+ response = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CRATE_ACTIONS[:add]}", :post, {:body => {:name => name}}))
15
+ raise CrateLimitReachedError, response["message"] unless response["status"] != "failure"
16
+ end
17
+
18
+ def list
19
+ hash = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::CRATES_URL}/#{CRATE_ACTIONS[:list]}", :get))
20
+ return Crates.from_array(hash["crates"])
21
+ end
22
+
23
+ def all
24
+ hash = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::ITEMS_URL}/#{CRATE_ACTIONS[:list]}", :get))
25
+ return Crates.from_array(hash["crates"])
26
+ end
27
+
28
+ def self.from_array(array)
29
+ return [] unless array != nil
30
+ crates = Array.new
31
+ array.each do |crate|
32
+ crates.push(Crate.new(crate))
33
+ end
34
+ return crates
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,16 @@
1
+ module CrateAPI
2
+ class FileDestroyError < Exception
3
+ end
4
+ class Item < CrateObject
5
+ attr_reader :size
6
+ def initialize(hash)
7
+ super(hash)
8
+ @size = hash["size"]
9
+ end
10
+
11
+ def destroy
12
+ response = JSON.parse(CrateAPI::Base.call("#{CrateAPI::Base::ITEMS_URL}/#{CrateAPI::Items::ITEM_ACTIONS[:destroy] % ["#{self.id}"]}", :post))
13
+ raise FileDestroyError, response["message"] unless response["status"] != "failure"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module CrateAPI
2
+ class Items
3
+ ITEM_ACTIONS = {
4
+ :upload => "upload.json",
5
+ :list => "list.json",
6
+ :show => "show/%s.json",
7
+ :destroy => "destroy/%s.json"
8
+ }
9
+
10
+ def self.from_array(array)
11
+ return [] unless array != nil
12
+ files = Array.new
13
+ array.each do |file|
14
+ files.push(Item.new(file))
15
+ end
16
+ return files
17
+ end
18
+ end
19
+ end
data/lib/crate_api.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "httmultiparty"
2
+ require "JSON"
3
+
4
+ ["base", "crateobject", "crate", "crates", "item", "items"].each do |inc|
5
+ require File.join(File.dirname(__FILE__), "crate_api", inc)
6
+ end
7
+
8
+ module CrateAPI
9
+ def self.new(username, password)
10
+ CrateAPI::Base.new(username, password)
11
+ end
12
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'crate_api'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestCrateApi < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crate_api
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Brian Michel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-23 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.5.2
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rcov
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: httmultiparty
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 0.3.0
67
+ type: :runtime
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: json
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 1.5.1
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ description: This gem will allow for easy interaction with the Crate API to share your files!
82
+ email: brian.michel@gmail.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.md
90
+ files:
91
+ - .document
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - VERSION
98
+ - crate_api.gemspec
99
+ - lib/crate_api.rb
100
+ - lib/crate_api/base.rb
101
+ - lib/crate_api/crate.rb
102
+ - lib/crate_api/crateobject.rb
103
+ - lib/crate_api/crates.rb
104
+ - lib/crate_api/item.rb
105
+ - lib/crate_api/items.rb
106
+ - test/helper.rb
107
+ - test/test_crate_api.rb
108
+ homepage: http://github.com/brianmichel/crate_api
109
+ licenses:
110
+ - MIT
111
+ post_install_message:
112
+ rdoc_options: []
113
+
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 1029907823158595725
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: "0"
131
+ requirements: []
132
+
133
+ rubyforge_project:
134
+ rubygems_version: 1.7.2
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Interface with the Crate API
138
+ test_files:
139
+ - test/helper.rb
140
+ - test/test_crate_api.rb