rmega 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,5 +15,12 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+
19
+ # OSX specific file
18
20
  .DS_Store
19
- .rvmrc
21
+
22
+ # RVM
23
+ .rvmrc
24
+
25
+ # Required to run integration specs
26
+ spec/integration/rmega_account.yml
@@ -0,0 +1,38 @@
1
+ module Rmega
2
+ class ApiRequestError < StandardError
3
+ attr_reader :code
4
+
5
+ def initialize error_code
6
+ @code = error_code
7
+ error_description = self.class.all_messages[@code]
8
+ super "Received error code #{@code}. #{error_description}".strip
9
+ end
10
+
11
+ def self.is_error_code? number
12
+ !!Integer(number) rescue false
13
+ end
14
+
15
+ def self.all_messages
16
+ {
17
+ -1 => 'An internal error has occurred. Please submit a bug report, detailing the exact circumstances in which this error occurred.',
18
+ -2 => 'You have passed invalid arguments to this command.',
19
+ -3 => 'A temporary congestion or server malfunction prevented your request from being processed. No data was altered. Retry. Retries must be spaced with exponential backoff.',
20
+ -4 => 'You have exceeded your command weight per time quota. Please wait a few seconds, then try again (this should never happen in sane real-life applications).',
21
+ -5 => 'The upload failed. Please restart it from scratch.',
22
+ -6 => 'Too many concurrent IP addresses are accessing this upload target URL.',
23
+ -7 => 'The upload file packet is out of range or not starting and ending on a chunk boundary.',
24
+ -8 => 'The upload target URL you are trying to access has expired. Please request a fresh one.',
25
+ -9 => 'Object (typically, node or user) not found',
26
+ -10 => 'Circular linkage attempted',
27
+ -11 => 'Access violation (e.g., trying to write to a read-only share)',
28
+ -12 => 'Trying to create an object that already exists',
29
+ -13 => 'Trying to access an incomplete resource',
30
+ -14 => 'A decryption operation failed (never returned by the API)',
31
+ -15 => 'Invalid or expired user session, please relogin',
32
+ -16 => 'User blocked',
33
+ -17 => 'Request over quota',
34
+ -18 => 'Resource temporarily not available, please try again later'
35
+ }
36
+ end
37
+ end
38
+ end
data/lib/rmega/node.rb CHANGED
@@ -9,8 +9,8 @@ module Rmega
9
9
 
10
10
  def self.initialize_by_public_url session, public_url
11
11
  public_handle, key = public_url.split('!')[1, 2]
12
- data = session.request a: 'g', g: 1, p: public_handle
13
- node = new session, data
12
+
13
+ node = new session, public_data(session, public_handle)
14
14
  node.instance_variable_set '@public_url', public_url
15
15
  node
16
16
  end
@@ -37,6 +37,10 @@ module Rmega
37
37
 
38
38
  # Other methods
39
39
 
40
+ def self.public_data session, public_handle
41
+ session.request a: 'g', g: 1, p: public_handle
42
+ end
43
+
40
44
  def public_handle
41
45
  @public_handle ||= session.request(a: 'l', n: handle)
42
46
  end
data/lib/rmega/session.rb CHANGED
@@ -57,11 +57,6 @@ module Rmega
57
57
  rand(1E7..1E9).to_i
58
58
  end
59
59
 
60
- def error_response? response
61
- response = response.first if response.respond_to? :first
62
- !!Integer(response) rescue false
63
- end
64
-
65
60
  def request body
66
61
  self.request_id += 1
67
62
  url = "#{api_url}?id=#{request_id}"
@@ -71,7 +66,7 @@ module Rmega
71
66
  response = HTTPClient.new.post url, [body].to_json, timeout: api_request_timeout
72
67
  debug "#{response.code}\n#{response.body}"
73
68
  resp = JSON.parse(response.body).first
74
- raise "Error code received: #{resp}" if error_response?(resp)
69
+ raise ApiRequestError.new(resp) if ApiRequestError.is_error_code?(resp)
75
70
  resp
76
71
  end
77
72
  end
data/lib/rmega/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rmega
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/rmega.rb CHANGED
@@ -19,6 +19,7 @@ require "rmega/crypto/crypto"
19
19
  require "rmega/storage"
20
20
  require "rmega/node"
21
21
  require "rmega/session"
22
+ require "rmega/api_request_error"
22
23
 
23
24
  module Rmega
24
25
  def self.logger
data/rmega.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.email = ["dani.m.mobile@gmail.com"]
7
7
  gem.description = %q{mega.co.nz ruby api}
8
8
  gem.summary = %q{mega.co.nz ruby api}
9
- gem.homepage = ""
9
+ gem.homepage = "https://github.com/daniele-m/rmega"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ require 'integration_spec_helper'
3
+
4
+ describe 'Login process' do
5
+ if account_file_exists?
6
+ context 'when email and password are correct' do
7
+ it 'returns a valid object' do
8
+ object = Rmega.login valid_account['email'], valid_account['password']
9
+ object.should respond_to :nodes
10
+ end
11
+ end
12
+
13
+ context 'when email and password are invalid' do
14
+ it 'raises an error' do
15
+ lambda { Rmega.login 'a@apple.com', 'b' }.should raise_error(Rmega::ApiRequestError)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ email: your_email@email.com
2
+ password: your_password
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+ require "yaml"
3
+
4
+
5
+ def account_file_path
6
+ File.join File.dirname(__FILE__), 'integration/rmega_account.yml'
7
+ end
8
+
9
+ def account_file_exists?
10
+ File.exists? account_file_path
11
+ end
12
+
13
+ def valid_account
14
+ @valid_account ||= YAML.load_file account_file_path
15
+ end
@@ -2,6 +2,12 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Rmega::Utils do
5
+ def random_a32_array
6
+ a32_ary = []
7
+ rand(0..20).times { |len| a32_ary << rand(0..2**31)*(rand(2).zero? ? 1 : -1) }
8
+ a32_ary
9
+ end
10
+
5
11
  describe '#str_to_a32' do
6
12
  it 'returns the expected value' do
7
13
  string = 'johnsnow'
@@ -16,12 +22,30 @@ describe Rmega::Utils do
16
22
  end
17
23
  end
18
24
 
25
+ describe '#format_bytes' do
26
+ it 'converts to the correct unit' do
27
+ described_class.format_bytes(1024, 2).should == '1.0kb'
28
+ described_class.format_bytes(1024**2).should == '1.0MB'
29
+ end
30
+ end
31
+
19
32
  describe '#a32_to_str' do
20
33
  it 'returns the expected value' do
21
34
  a32 = [1953853537, 1660944384]
22
35
  string = "tupac" + "\x00\x00\x00"
23
36
  described_class.a32_to_str(a32).should == string
24
37
  end
38
+
39
+ it 'is the opposite of #str_to_a32' do
40
+ a32_ary = random_a32_array
41
+ str = described_class.a32_to_str a32_ary
42
+ described_class.str_to_a32(str).should == a32_ary
43
+ end
44
+
45
+ it 'has the same result if len is multiplied by 4' do
46
+ a32 = random_a32_array
47
+ described_class.a32_to_str(a32).should == described_class.a32_to_str(a32, a32.size*4)
48
+ end
25
49
  end
26
50
 
27
51
  describe '#base64urlencode' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmega
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-25 00:00:00.000000000 Z
12
+ date: 2013-03-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pry
@@ -104,6 +104,7 @@ files:
104
104
  - README.md
105
105
  - Rakefile
106
106
  - lib/rmega.rb
107
+ - lib/rmega/api_request_error.rb
107
108
  - lib/rmega/crypto/aes.rb
108
109
  - lib/rmega/crypto/aes_ctr.rb
109
110
  - lib/rmega/crypto/crypto.rb
@@ -115,11 +116,14 @@ files:
115
116
  - lib/rmega/utils.rb
116
117
  - lib/rmega/version.rb
117
118
  - rmega.gemspec
119
+ - spec/integration/login_spec.rb
120
+ - spec/integration/rmega_account.yml.example
121
+ - spec/integration_spec_helper.rb
118
122
  - spec/rmega/lib/crypto/aes_spec.rb
119
123
  - spec/rmega/lib/crypto/crypto_spec.rb
120
124
  - spec/rmega/lib/utils_spec.rb
121
125
  - spec/spec_helper.rb
122
- homepage: ''
126
+ homepage: https://github.com/daniele-m/rmega
123
127
  licenses: []
124
128
  post_install_message:
125
129
  rdoc_options: []
@@ -144,6 +148,9 @@ signing_key:
144
148
  specification_version: 3
145
149
  summary: mega.co.nz ruby api
146
150
  test_files:
151
+ - spec/integration/login_spec.rb
152
+ - spec/integration/rmega_account.yml.example
153
+ - spec/integration_spec_helper.rb
147
154
  - spec/rmega/lib/crypto/aes_spec.rb
148
155
  - spec/rmega/lib/crypto/crypto_spec.rb
149
156
  - spec/rmega/lib/utils_spec.rb