mite-backup 0.0.2

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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3@mite-backup --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mite-backup.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Yolk Sebastian Munz & Julia Soergel GbR
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,26 @@
1
+ # mite-backup
2
+
3
+ Simple command-line tool for downloading a backup in XML of your [mite](http://mite.yo.lk/en).account.
4
+
5
+ ## Installation
6
+
7
+ gem install mite-backup
8
+
9
+ ## Usage
10
+
11
+ mite-backup -a [ACCOUNT] -e [EMAIL] -p [PASSWORD]
12
+
13
+ This will output the backup file in XML to the prompt. You propably want to pipe it into an file:
14
+
15
+ mite-backup -a [ACCOUNT] -e [EMAIL] -p [PASSWORD] > my_backup_file.xml
16
+
17
+ For further instructions run
18
+
19
+ mite-backup -h
20
+
21
+
22
+ ## BlaBla
23
+
24
+ Copyright (c) 2011 [Yolk](http://yo.lk/) Sebastian Munz & Julia Soergel GbR
25
+
26
+ Beyond that, the implementation is licensed under the MIT License.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/mite-backup ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+
5
+ require 'mite-backup'
6
+ require 'optparse'
7
+
8
+ @options = {}
9
+
10
+ parser = OptionParser.new do |opts|
11
+ opts.banner = "Usage: mite-backup COMMAND [options]"
12
+
13
+ opts.separator ""
14
+ opts.separator "Options:"
15
+
16
+ opts.on("-a", "--account [ACCOUNT]", "your mite account-name (subdomain without .mite.yo.lk)") do |account|
17
+ @options["account"] = account
18
+ end
19
+
20
+ opts.on("-e", "--email [EMAIL]", "mite.user email") do |email|
21
+ @options["email"] = email
22
+ end
23
+
24
+ opts.on("-p", "--password [PASSWORD]", "mite.user password") do |password|
25
+ @options["password"] = password
26
+ end
27
+
28
+ opts.on("-c", "--clear", "Removes all config values from config file.") do
29
+ @options["clear_config"] = true
30
+ end
31
+
32
+ opts.on("-h", "--help", "Show this message") do
33
+ puts opts
34
+ exit
35
+ end
36
+
37
+ opts.separator ""
38
+ opts.separator "Commands:"
39
+ opts.separator " get Download backupfile and ouput xml to STDOUT (Default command)"
40
+ opts.separator " setup Write given options to config file ~/.mite-backup.yml, so you don't need to repeat the on ever get command."
41
+ opts.separator ""
42
+ end
43
+
44
+ parser.parse!
45
+
46
+ case ARGV[0] || "get"
47
+ when 'get'
48
+ MiteBackup.new(@options).run
49
+ when 'setup'
50
+ MiteBackup.new(@options).setup
51
+ else
52
+ $stderr.puts "Unknown command #{ARGV[0].inspect}"
53
+ puts ""
54
+ puts parser.help
55
+ exit(1)
56
+ end
@@ -0,0 +1,3 @@
1
+ class MiteBackup
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,105 @@
1
+ require "mite-backup/version"
2
+ require "multi_json"
3
+ require "net/http"
4
+ require "uri"
5
+ require 'zlib'
6
+ require 'yaml'
7
+
8
+ class MiteBackup
9
+ MAX_CHECKS = 30
10
+ SLEEP_BEFORE_EACH_CHECK = 2 # seconds
11
+ CONFIG_FILE = File.expand_path('~/.mite-backup.yml')
12
+ USER_AGENT = "mite-backup/#{MiteBackup::VERSION}"
13
+
14
+ def initialize(options={})
15
+ @options = options
16
+ self.class.clear_config if options["clear_config"]
17
+ @account = options["account"] || config["account"]
18
+ @email = options["email"] || config["email"]
19
+ @password = options["password"] || config["password"]
20
+ end
21
+
22
+ def run
23
+ runnable?
24
+ create
25
+ check
26
+ download
27
+ end
28
+
29
+ def setup
30
+ (config["account"] = @account) || config.delete("account")
31
+ (config["email"] = @email) || config.delete("email")
32
+ (config["password"] = @password) || config.delete("password")
33
+
34
+ if config.size == 0
35
+ self.class.clear_config
36
+ else
37
+ File.open(CONFIG_FILE, "w") do |f|
38
+ f.write(YAML::dump(config))
39
+ end
40
+ end
41
+ end
42
+
43
+ def self.clear_config
44
+ File.exist?(CONFIG_FILE) && File.delete(CONFIG_FILE)
45
+ end
46
+
47
+ private
48
+
49
+ def runnable?
50
+ failed "Please provide your account name with --account [ACCOUNT]." unless @account
51
+ failed "Please provide your mite.users email with --email [EMAIL]." unless @email
52
+ failed "Please provide your mite.users password with --password [PASSWORD]." unless @password
53
+ end
54
+
55
+ def create
56
+ @id = parse_json(perform_request(Net::HTTP::Post.new("/account/backup.json")))["id"]
57
+ end
58
+
59
+ def check
60
+ MAX_CHECKS.times do |i|
61
+ sleep(SLEEP_BEFORE_EACH_CHECK)
62
+ @ready = parse_json(perform_request(Net::HTTP::Get.new("/account/backup/#{@id}.json")))["ready"]
63
+ break if @ready
64
+ end
65
+ end
66
+
67
+ def download
68
+ if @ready
69
+ content = perform_request(Net::HTTP::Get.new("/account/backup/#{@id}/download.json"))
70
+ gz = Zlib::GzipReader.new(StringIO.new(content), :external_encoding => content.encoding)
71
+ puts gz.read
72
+ else
73
+ failed "Backup was not ready for download after #{MAX_CHECKS*SLEEP_BEFORE_EACH_CHECK} seconds. Contact the mite support."
74
+ end
75
+ end
76
+
77
+ def perform_request(request)
78
+ request.basic_auth(@email, @password)
79
+ request['User-Agent'] = USER_AGENT
80
+ response = mite.request(request)
81
+ if response.code == "401"
82
+ failed "Could not authenticate with email #{@email.inspect} and provided password. The user needs to be an admin or the owner of the mite.account!"
83
+ elsif !["200", "201"].include?(response.code)
84
+ failed "mite responded with irregular code #{response.code}"
85
+ end
86
+ response.body
87
+ end
88
+
89
+ def parse_json(string)
90
+ MultiJson.decode(string)["backup"]
91
+ end
92
+
93
+ def mite
94
+ @mite ||= Net::HTTP.new(URI.parse("http://#{@account}.mite.yo.lk/").host)
95
+ end
96
+
97
+ def failed(reason)
98
+ $stderr.puts "Failed: #{reason}"
99
+ exit(1)
100
+ end
101
+
102
+ def config
103
+ @config ||= File.exist?(CONFIG_FILE) && YAML::load( File.open( CONFIG_FILE ) ) || {}
104
+ end
105
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mite-backup/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mite-backup"
7
+ s.version = MiteBackup::VERSION
8
+ s.authors = ["Sebastian"]
9
+ s.email = ["sebastian@yo.lk"]
10
+ s.homepage = "http://mite.yo.lk/en"
11
+ s.summary = %q{Download a backup of your mite.account from the command-line.}
12
+ s.description = %q{Download a backup of your mite.account from the command-line.}
13
+
14
+ s.rubyforge_project = "mite-backup"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency "multi_json"
22
+ s.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mite-backup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sebastian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: multi_json
16
+ requirement: &2167794720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2167794720
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2167794300 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2167794300
36
+ description: Download a backup of your mite.account from the command-line.
37
+ email:
38
+ - sebastian@yo.lk
39
+ executables:
40
+ - mite-backup
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - .rvmrc
46
+ - Gemfile
47
+ - MIT-LICENSE
48
+ - README.md
49
+ - Rakefile
50
+ - bin/mite-backup
51
+ - lib/mite-backup.rb
52
+ - lib/mite-backup/version.rb
53
+ - mite-backup.gemspec
54
+ homepage: http://mite.yo.lk/en
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
67
+ - 0
68
+ hash: -1264802230694611080
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: -1264802230694611080
78
+ requirements: []
79
+ rubyforge_project: mite-backup
80
+ rubygems_version: 1.8.10
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Download a backup of your mite.account from the command-line.
84
+ test_files: []