btsync_api 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in btsync-api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Pascal Jungblut
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # btsync
2
+
3
+ A Ruby wrapper for the [BitTorrent Sync API](http://www.bittorrent.com/sync/developers).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'btsync_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install btsync_api
18
+
19
+ ## Usage
20
+
21
+ Once you started the BitTorrent Sync client as described in the [documentation](http://www.bittorrent.com/sync/developers/api), you can use the wrapper:
22
+
23
+ ```ruby
24
+ options = {host: 'localhost', port: 8888, login: '', password: ''} # default
25
+ api = BtsyncApi::Api.new(options)
26
+ api.get_os # => {"os"=>"mac"}
27
+
28
+ # pass arguments to the API
29
+ api.add_folder(dir: dirPath, secret: my_secret, selective_sync: 1)
30
+
31
+ api.remove_folder # raises BtsyncApi::ApiError('Specify all the required parameters for remove_folder')
32
+ ```
33
+ *Note:* The error codes from the BitTorrent Sync API are not yet completely consistent, so expect some `NoMethodError` where it should actually be `Btsync::ApiError`.
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/btsync.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'btsync/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "btsync_api"
8
+ spec.version = BtsyncApi::VERSION
9
+ spec.authors = ["Pascal Jungblut"]
10
+ spec.email = ["mail@pascal-jungblut.com"]
11
+ spec.description = %q{Wrapper for the BitTorrent Sync API}
12
+ spec.summary = %q{A thin wrapper around the BitTorrent Sync API. It abstracts the request into method calls and returns parsed JSON.}
13
+ spec.homepage = "http://github.com/pascalj/btsync"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
data/lib/btsync/api.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module BtsyncApi
5
+ class ApiError < Exception
6
+ attr_reader :error_code
7
+ def initialize(error_code)
8
+ @error_code = error_code
9
+ end
10
+ end
11
+
12
+ class Api
13
+ attr_accessor :settings
14
+
15
+ def initialize(options = {})
16
+ self.settings = default_settings.merge(options)
17
+ end
18
+
19
+ def execute(method, options = {})
20
+ params = {method: method.to_s}
21
+ params.merge!(options)
22
+ response = do_request(params)
23
+ if response['error'] && response['error'] != 0
24
+ raise BtsyncApi::ApiError.new(response['error']), response['message']
25
+ end
26
+ response
27
+ end
28
+
29
+ def method_missing(method, *args, &block)
30
+ begin
31
+ if args.count == 1
32
+ return execute(method, args[0])
33
+ else
34
+ return execute(method)
35
+ end
36
+ rescue BtsyncApi::ApiError => e
37
+ if e.error_code != 1 # not found
38
+ raise e
39
+ end
40
+ end
41
+ super
42
+ end
43
+
44
+ private
45
+
46
+ def do_request(params)
47
+ url = base_url
48
+ url.query = URI.encode_www_form(params)
49
+ http = Net::HTTP.new(url.host, url.port)
50
+ request = Net::HTTP::Get.new(url.request_uri)
51
+ request.basic_auth(settings[:login], settings[:password])
52
+ response = http.request(request)
53
+ JSON.parse(response.body)
54
+ end
55
+
56
+ def base_url
57
+ URI::HTTP.build(path: '/api', port: settings[:port], host: settings[:host])
58
+ end
59
+
60
+ def default_settings
61
+ {
62
+ host: 'localhost',
63
+ port: 8888,
64
+ login: '',
65
+ password: ''
66
+ }
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module BtsyncApi
2
+ VERSION = "0.8.1"
3
+ end
data/lib/btsync_api.rb ADDED
@@ -0,0 +1,3 @@
1
+ module BtsyncApi
2
+ autoload :Api, 'btsync/api'
3
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/btsync/api.rb'
3
+
4
+ describe BtsyncApi::Api do
5
+
6
+ let(:api) { BtsyncApi::Api.new }
7
+
8
+ before(:each) do
9
+ api.stub(:do_request).and_return({"error" => 0})
10
+ end
11
+
12
+ describe "#initialize" do
13
+ it "sets a default port and host if none are given" do
14
+ api.settings[:host].should == 'localhost'
15
+ api.settings[:port].should == 8888
16
+ end
17
+ end
18
+
19
+ describe "#execute" do
20
+
21
+ it "calls the API" do
22
+ api.should_receive(:do_request).and_return({})
23
+ api.execute('my_method')
24
+ end
25
+
26
+ it "merges the options as additional parameters" do
27
+ api.should_receive(:do_request).with(method: 'my_method', foo: 'bar', baz: 'foo')
28
+ api.execute('my_method', foo: 'bar', baz: 'foo')
29
+ end
30
+ end
31
+
32
+ describe "#method_missing" do
33
+
34
+ it "tries to send a missing method to the API" do
35
+ api.should_receive(:execute).with(:foobar)
36
+ api.foobar
37
+ end
38
+
39
+ it "hands the parameters over to #execute" do
40
+ api.should_receive(:execute).with(:foobar, {foo: 'bar', other: 'argument'})
41
+ api.foobar(foo: 'bar', other: 'argument')
42
+ end
43
+
44
+ it "allows empty options" do
45
+ expect{
46
+ api.valid_method
47
+ }.to_not raise_error
48
+ end
49
+
50
+ it "calls super if the method is not found" do
51
+ api.should_receive(:do_request).and_return({ "error" => 1, "message" => "Invalid API method name or format." })
52
+ expect{
53
+ api.unkown_method
54
+ }.to raise_error(NoMethodError)
55
+ end
56
+
57
+ it "passes exceptions other than method not found through" do
58
+ api.should_receive(:do_request).and_return({ "error" => 2, "message" => "Specify all the required parameters for get_files." })
59
+ expect{
60
+ api.get_files
61
+ }.to raise_error(BtsyncApi::ApiError)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,13 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ # Run specs in random order to surface order dependencies. If you find an
9
+ # order dependency and want to debug it, you can fix the order by providing
10
+ # the seed, which is printed after each run.
11
+ # --seed 1234
12
+ config.order = 'random'
13
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: btsync_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pascal Jungblut
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Wrapper for the BitTorrent Sync API
63
+ email:
64
+ - mail@pascal-jungblut.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - btsync.gemspec
76
+ - lib/btsync/api.rb
77
+ - lib/btsync/version.rb
78
+ - lib/btsync_api.rb
79
+ - spec/btsync/api_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: http://github.com/pascalj/btsync
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.23
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: A thin wrapper around the BitTorrent Sync API. It abstracts the request into
106
+ method calls and returns parsed JSON.
107
+ test_files:
108
+ - spec/btsync/api_spec.rb
109
+ - spec/spec_helper.rb
110
+ has_rdoc: