putio-ruby 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +17 -11
- data/bin/console +3 -7
- data/lib/putio-ruby.rb +19 -9
- data/lib/putio-ruby/api/client.rb +7 -3
- data/lib/putio-ruby/api/object.rb +1 -34
- data/lib/putio-ruby/configuration.rb +5 -0
- data/lib/putio-ruby/error.rb +62 -0
- data/lib/putio-ruby/file.rb +18 -6
- data/lib/putio-ruby/middleware/error_handler.rb +9 -0
- data/lib/putio-ruby/middleware/oauth_token.rb +2 -2
- data/lib/putio-ruby/middleware/parse_root.rb +15 -0
- data/lib/putio-ruby/transfer.rb +34 -8
- data/lib/putio-ruby/version.rb +1 -1
- data/putio-ruby.gemspec +3 -1
- metadata +39 -15
- data/lib/putio-ruby/errors.rb +0 -6
- data/lib/putio-ruby/middleware/parse.rb +0 -17
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MDNmOGQ5YjQwNzAwNTAxOTg0OWY0OGE3NTE2NWViNGNlNTcyYzg5MA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
M2ZmM2Q2ZmU5MzVmMGQ1MDE5ODMzMGM1ZmQxMGM4ZGQ4MDQ1NTdmNA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YjdhZTZjZThkYmNhNmMxMmNhZjI2MWYyMzc2NzI2ZmY4ZGU4Y2FjYTc0MDM2
|
10
|
+
MWFjYTg2MTJiMzkwNWUxOTlmOWYwMGM5MGZmNjRmNTVkNDIxZTJiZTcyMjRk
|
11
|
+
MWEzMzk2ODI2MGViOGQ0NzZjYTg2MzZmNzFiMzc2OGZjZjMyM2M=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZWQxYmFjNDNkNjcyN2U2ZmU5Mjg4YWM0NDBmNThlYjEyMjU1ZDljM2NkYzQy
|
14
|
+
NzdjNjdlY2NiNWM5YjY3MjgzYjdjOGVmMTU3ODc1ZGQwNzZiN2JlMTkyZWJl
|
15
|
+
MWYyZmYxMDVkZjk4M2EzMmRjMmRiNGNmMjlmMzdhYzljY2Y3M2M=
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@ putio-ruby is a simple gem for communicating with the Put.io REST API. Currently
|
|
12
12
|
|
13
13
|
Add the gem to your Gemfile:
|
14
14
|
|
15
|
-
```
|
15
|
+
```ruby
|
16
16
|
gem 'putio-ruby'
|
17
17
|
```
|
18
18
|
|
@@ -28,15 +28,21 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
First configure the gem to use your oauth token (See [instructions](https://put.io/v2/docs/gettingstarted.html#sign-up) for generating a token)
|
30
30
|
|
31
|
-
|
32
|
-
|
31
|
+
```ruby
|
32
|
+
Putio.configure do |config|
|
33
|
+
oauth_token = "YOUR_OAUTH_TOKEN"
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
33
37
|
Then use it to query Put.io
|
34
38
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
```ruby
|
40
|
+
Putio::File.list
|
41
|
+
file = Putio::File.get(1234)
|
42
|
+
file.download_url
|
43
|
+
file.delete
|
44
|
+
|
45
|
+
Putio::Transfer.list
|
46
|
+
Putio::Transfer.get(1234)
|
47
|
+
Putio::Transfer.add("http://some_url.com")
|
48
|
+
```
|
data/bin/console
CHANGED
@@ -1,14 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require "bundler/setup"
|
4
|
-
require "putio
|
4
|
+
require "putio-ruby"
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start
|
9
|
+
require "pry"
|
10
|
+
Pry.start
|
data/lib/putio-ruby.rb
CHANGED
@@ -2,12 +2,14 @@ require "putio-ruby/version"
|
|
2
2
|
|
3
3
|
require "multi_json"
|
4
4
|
require "faraday"
|
5
|
-
require "
|
6
|
-
require "
|
7
|
-
require "active_support/core_ext/hash"
|
5
|
+
require "faraday_middleware"
|
6
|
+
require "virtus"
|
8
7
|
|
9
|
-
require "putio-ruby/
|
8
|
+
require "putio-ruby/configuration"
|
9
|
+
|
10
|
+
require "putio-ruby/middleware/parse_root"
|
10
11
|
require "putio-ruby/middleware/oauth_token"
|
12
|
+
require "putio-ruby/middleware/error_handler"
|
11
13
|
|
12
14
|
require "putio-ruby/api/client"
|
13
15
|
require "putio-ruby/api/object"
|
@@ -15,15 +17,23 @@ require "putio-ruby/api/object"
|
|
15
17
|
require "putio-ruby/file"
|
16
18
|
require "putio-ruby/transfer"
|
17
19
|
|
18
|
-
require "putio-ruby/
|
20
|
+
require "putio-ruby/error"
|
19
21
|
|
20
22
|
module Putio
|
21
|
-
|
22
|
-
|
23
|
+
class << self
|
24
|
+
attr_writer :configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.configuration
|
28
|
+
@configuration ||= Configuration.new
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.reset
|
32
|
+
@configuration = Configuration.new
|
23
33
|
end
|
24
34
|
|
25
|
-
def self.
|
26
|
-
|
35
|
+
def self.configure
|
36
|
+
yield(configuration)
|
27
37
|
end
|
28
38
|
end
|
29
39
|
|
@@ -2,13 +2,17 @@ module Putio
|
|
2
2
|
module Api
|
3
3
|
module Client
|
4
4
|
def client
|
5
|
-
|
5
|
+
Faraday.new :url => "https://api.put.io/v2/" do |connection|
|
6
6
|
# Request
|
7
|
-
connection.use Putio::Middleware::OauthToken
|
8
7
|
connection.request :url_encoded
|
8
|
+
connection.use Putio::Middleware::OauthToken
|
9
9
|
|
10
10
|
# Response
|
11
|
-
connection.use Putio::Middleware::
|
11
|
+
connection.use Putio::Middleware::ParseRoot
|
12
|
+
connection.use Putio::Middleware::ErrorHandler
|
13
|
+
connection.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
|
14
|
+
|
15
|
+
# Adapter
|
12
16
|
connection.adapter :net_http
|
13
17
|
end
|
14
18
|
end
|
@@ -1,45 +1,12 @@
|
|
1
1
|
module Putio
|
2
2
|
module Api
|
3
3
|
class Object
|
4
|
+
include Virtus.model
|
4
5
|
extend Putio::Api::Client
|
5
6
|
|
6
|
-
def self.plural_name
|
7
|
-
singular_name.pluralize
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.singular_name
|
11
|
-
self.name.demodulize.underscore.downcase
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.parse_collection(hash)
|
15
|
-
objects = hash[plural_name.to_sym]
|
16
|
-
objects.map do |obj|
|
17
|
-
instantiate_object obj
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.parse_object(hash)
|
22
|
-
object = hash[singular_name.to_sym]
|
23
|
-
instantiate_object object
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.instantiate_object(hash)
|
27
|
-
obj = self.new
|
28
|
-
hash.each do |k,v|
|
29
|
-
obj.set_attribute(k.to_s, v)
|
30
|
-
end
|
31
|
-
obj
|
32
|
-
end
|
33
|
-
|
34
7
|
def client
|
35
8
|
self.class.client
|
36
9
|
end
|
37
|
-
|
38
|
-
def set_attribute(attr, value)
|
39
|
-
return unless attr.present?
|
40
|
-
self.singleton_class.instance_eval { attr_reader "#{attr}".to_sym }
|
41
|
-
self.instance_variable_set("@#{attr}", value)
|
42
|
-
end
|
43
10
|
end
|
44
11
|
end
|
45
12
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Putio
|
2
|
+
class Error < StandardError
|
3
|
+
def self.from_env(env)
|
4
|
+
status = env.status
|
5
|
+
|
6
|
+
if klass = case status
|
7
|
+
when 400..499 then Putio::BadRequest
|
8
|
+
when 500..599 then Putio::ServerError
|
9
|
+
end
|
10
|
+
klass.new(env)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(env=nil)
|
15
|
+
@env = env
|
16
|
+
super(build_error_message)
|
17
|
+
end
|
18
|
+
|
19
|
+
def response_error
|
20
|
+
if response_body && error_type && error_message
|
21
|
+
"#{error_type}: #{error_message}"
|
22
|
+
elsif response_body && error_message
|
23
|
+
error_message
|
24
|
+
else
|
25
|
+
response_body
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def build_error_message
|
30
|
+
return nil if @env.nil?
|
31
|
+
|
32
|
+
message = "#{@env.method.to_s.upcase} "
|
33
|
+
message << "#{@env.status} - #{@env.url}\n"
|
34
|
+
message << "#{response_error}" unless response_error.nil?
|
35
|
+
message
|
36
|
+
end
|
37
|
+
|
38
|
+
def response_headers
|
39
|
+
@env.response_headers
|
40
|
+
end
|
41
|
+
|
42
|
+
def response_status
|
43
|
+
@env.status
|
44
|
+
end
|
45
|
+
|
46
|
+
def response_body
|
47
|
+
@env.body
|
48
|
+
end
|
49
|
+
|
50
|
+
def error_type
|
51
|
+
@env.body["error_type"]
|
52
|
+
end
|
53
|
+
|
54
|
+
def error_message
|
55
|
+
@env.body["error_message"]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class BadRequest < Error; end
|
60
|
+
class ServerError < Error; end
|
61
|
+
class ApplicationCredentialsRequired < StandardError; end
|
62
|
+
end
|
data/lib/putio-ruby/file.rb
CHANGED
@@ -1,18 +1,30 @@
|
|
1
1
|
module Putio
|
2
2
|
class File < Putio::Api::Object
|
3
|
+
attribute :content_type, String
|
4
|
+
attribute :crc32, String
|
5
|
+
attribute :created_at, DateTime
|
6
|
+
attribute :first_accessed_at, DateTime
|
7
|
+
attribute :icon, String
|
8
|
+
attribute :id, Integer
|
9
|
+
attribute :is_mp4_available, Boolean
|
10
|
+
attribute :is_shared, Boolean
|
11
|
+
attribute :name, String
|
12
|
+
attribute :opensubtitles_hash, String
|
13
|
+
attribute :parent_id, Integer
|
14
|
+
attribute :screenshot, String
|
15
|
+
attribute :size, Integer
|
16
|
+
|
3
17
|
def self.list(parent_id=nil)
|
4
18
|
res = client.get "files/list", ( parent_id ? {:parent_id => parent_id} : nil )
|
5
|
-
|
19
|
+
res.body.map { |f| self.new f }
|
6
20
|
end
|
7
21
|
|
8
|
-
def self.
|
9
|
-
|
10
|
-
parse_object res.body if res.success?
|
22
|
+
def self.file(id)
|
23
|
+
self.new client.get("files/#{id}").body
|
11
24
|
end
|
12
25
|
|
13
26
|
def download_url
|
14
|
-
|
15
|
-
res.headers[:location] if res.success?
|
27
|
+
client.get("files/#{id}/download").env.response_headers["location"]
|
16
28
|
end
|
17
29
|
|
18
30
|
def delete
|
@@ -2,8 +2,8 @@ module Putio
|
|
2
2
|
module Middleware
|
3
3
|
class OauthToken < Faraday::Response::Middleware
|
4
4
|
def call(env)
|
5
|
-
raise Putio::
|
6
|
-
oauth_string = "oauth_token=#{Putio.oauth_token}"
|
5
|
+
raise Putio::ApplicationCredentialsRequired, "No OAuth token provided" unless Putio.configuration.oauth_token
|
6
|
+
oauth_string = "oauth_token=#{Putio.configuration.oauth_token}"
|
7
7
|
query = env[:url].query
|
8
8
|
env[:url].query = query ? "#{query}&#{oauth_string}" : oauth_string
|
9
9
|
@app.call(env)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Putio
|
2
|
+
module Middleware
|
3
|
+
class ParseRoot < Faraday::Response::Middleware
|
4
|
+
def on_complete(env)
|
5
|
+
plural = env.url.to_s.match(/api\.put\.io\/v2\/(\w+)\//)[1]
|
6
|
+
singular = plural[0..-2]
|
7
|
+
if env[:body][plural]
|
8
|
+
env[:body] = env[:body][plural]
|
9
|
+
elsif env[:body][singular]
|
10
|
+
env[:body] = env[:body][singular]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/putio-ruby/transfer.rb
CHANGED
@@ -1,22 +1,48 @@
|
|
1
1
|
module Putio
|
2
2
|
class Transfer < Putio::Api::Object
|
3
|
+
attribute :callback_url, String
|
4
|
+
attribute :created_at, DateTime
|
5
|
+
attribute :current_ratio, Float
|
6
|
+
attribute :down_speed, Integer
|
7
|
+
attribute :downloaded, Integer
|
8
|
+
attribute :error_message, String
|
9
|
+
attribute :estimated_time, Integer
|
10
|
+
attribute :extract, Boolean
|
11
|
+
attribute :file_id, Integer
|
12
|
+
attribute :finished_at, DateTime
|
13
|
+
attribute :id, Integer
|
14
|
+
attribute :is_private, Boolean
|
15
|
+
attribute :name, String
|
16
|
+
attribute :peers_connected, Integer
|
17
|
+
attribute :peers_sending_to_us, Integer
|
18
|
+
attribute :percent_done, Integer
|
19
|
+
attribute :save_parent_id, Integer
|
20
|
+
attribute :seconds_seeding, Integer
|
21
|
+
attribute :size, Integer
|
22
|
+
attribute :source, String
|
23
|
+
attribute :status, String
|
24
|
+
attribute :status_message, String
|
25
|
+
attribute :subscription_id, Integer
|
26
|
+
attribute :tracker_message, String
|
27
|
+
attribute :type, String
|
28
|
+
attribute :up_speed, Integer
|
29
|
+
attribute :uploaded, Integer
|
30
|
+
|
3
31
|
def self.list
|
4
|
-
res = client.get
|
5
|
-
|
32
|
+
res = client.get("transfers/list")
|
33
|
+
res.body.map { |t| self.new t }
|
6
34
|
end
|
7
35
|
|
8
|
-
def self.
|
9
|
-
|
10
|
-
parse_object res.body if res.success?
|
36
|
+
def self.transfer(id)
|
37
|
+
self.new client.get("transfers/#{id}").body
|
11
38
|
end
|
12
39
|
|
13
40
|
def self.add(url)
|
14
|
-
|
15
|
-
parse_object res.body
|
41
|
+
self.new client.post("transfers/add", :url => url).body
|
16
42
|
end
|
17
43
|
|
18
44
|
def fetch
|
19
|
-
self.class.
|
45
|
+
self.class.transfer(id)
|
20
46
|
end
|
21
47
|
end
|
22
48
|
end
|
data/lib/putio-ruby/version.rb
CHANGED
data/putio-ruby.gemspec
CHANGED
@@ -22,9 +22,11 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
23
|
spec.add_development_dependency "pry", "~> 0.10"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "webmock", "~> 1.0"
|
25
26
|
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4"
|
26
27
|
|
27
28
|
spec.add_runtime_dependency "faraday", "~> 0.9"
|
28
|
-
spec.add_runtime_dependency "
|
29
|
+
spec.add_runtime_dependency "faraday_middleware", "~> 0.10"
|
29
30
|
spec.add_runtime_dependency "multi_json", "~> 1.7"
|
31
|
+
spec.add_runtime_dependency "virtus", "~> 1.0"
|
30
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: putio-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Bloxsom
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08
|
11
|
+
date: 2015-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: codeclimate-test-reporter
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,25 +109,19 @@ dependencies:
|
|
95
109
|
- !ruby/object:Gem::Version
|
96
110
|
version: '0.9'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
112
|
+
name: faraday_middleware
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
|
-
- -
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: 3.0.0
|
104
|
-
- - <=
|
115
|
+
- - ~>
|
105
116
|
- !ruby/object:Gem::Version
|
106
|
-
version:
|
117
|
+
version: '0.10'
|
107
118
|
type: :runtime
|
108
119
|
prerelease: false
|
109
120
|
version_requirements: !ruby/object:Gem::Requirement
|
110
121
|
requirements:
|
111
|
-
- -
|
112
|
-
- !ruby/object:Gem::Version
|
113
|
-
version: 3.0.0
|
114
|
-
- - <=
|
122
|
+
- - ~>
|
115
123
|
- !ruby/object:Gem::Version
|
116
|
-
version:
|
124
|
+
version: '0.10'
|
117
125
|
- !ruby/object:Gem::Dependency
|
118
126
|
name: multi_json
|
119
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,6 +136,20 @@ dependencies:
|
|
128
136
|
- - ~>
|
129
137
|
- !ruby/object:Gem::Version
|
130
138
|
version: '1.7'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: virtus
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ~>
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '1.0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ~>
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '1.0'
|
131
153
|
description: A simple library for communicating with the Put.io REST API
|
132
154
|
email:
|
133
155
|
- bloxsom@umich.edu
|
@@ -146,10 +168,12 @@ files:
|
|
146
168
|
- lib/putio-ruby.rb
|
147
169
|
- lib/putio-ruby/api/client.rb
|
148
170
|
- lib/putio-ruby/api/object.rb
|
149
|
-
- lib/putio-ruby/
|
171
|
+
- lib/putio-ruby/configuration.rb
|
172
|
+
- lib/putio-ruby/error.rb
|
150
173
|
- lib/putio-ruby/file.rb
|
174
|
+
- lib/putio-ruby/middleware/error_handler.rb
|
151
175
|
- lib/putio-ruby/middleware/oauth_token.rb
|
152
|
-
- lib/putio-ruby/middleware/
|
176
|
+
- lib/putio-ruby/middleware/parse_root.rb
|
153
177
|
- lib/putio-ruby/transfer.rb
|
154
178
|
- lib/putio-ruby/version.rb
|
155
179
|
- putio-ruby.gemspec
|
data/lib/putio-ruby/errors.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
module Putio
|
2
|
-
module Middleware
|
3
|
-
class Parse < Faraday::Response::Middleware
|
4
|
-
def parse(body)
|
5
|
-
begin
|
6
|
-
return MultiJson.load(body, :symbolize_keys => true)
|
7
|
-
rescue MultiJson::ParseError
|
8
|
-
return body
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def on_complete(env)
|
13
|
-
env[:body] = parse(env[:body])
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|