kinto_box 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a55dde94245bcbc28a2a18b78ce2cbfaedf0c048
4
+ data.tar.gz: f213d31d114f44e1fe0d7060af203d6898c60c85
5
+ SHA512:
6
+ metadata.gz: 3d65b397f340e00cfcd78ab3b7fdabaab1594092a9ac93a61b607ea9e75e5ed9569e5b062a3f77b9edb23c129ae271ca4ed73bd0d1cd6167efd5ec3d8e5d6218
7
+ data.tar.gz: dfcfae59220e9f8a82947cad3632949a19112c99e4905b24e2f56c4d554896ff1f4427289f6bce72e949113a7fcb650713a2b69f6833fc944a5d539867cd00a2
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kavya Sukumar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Kinto Box [![Gem Version](https://badge.fury.io/rb/kinto_box.svg)](https://badge.fury.io/rb/kinto_box)
2
+
3
+ Kinto Box is a ruby client for [Kinto](http://kinto.readthedocs.io)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'kinto_box'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install kinto_box
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rake test` to run the tests.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`.
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kavyasukumar/kinto_box.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kinto_box"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/kinto_box.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kinto_box/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kinto_box"
8
+ spec.version = KintoBox::VERSION
9
+ spec.authors = ["Kavya Sukumar"]
10
+ spec.email = ["Kavya.Sukumar@voxmedia.com"]
11
+
12
+ spec.summary = %q{Kinto http client in ruby}
13
+ spec.description = %q{Kinto http client in ruby}
14
+ spec.homepage = "http://github.com/kavyasukumar/kinto_box"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency 'httparty', '~> 0.13.7'
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.11"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency 'json', '~> 1.8', '>= 1.8.3'
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+ end
@@ -0,0 +1,72 @@
1
+ require "kinto_box/version"
2
+ require 'httparty'
3
+ require 'kinto_box/response_handler'
4
+ require 'kinto_box/kinto_bucket'
5
+ require 'base64'
6
+
7
+ module KintoBox
8
+ class KintoClient
9
+ include HTTParty
10
+ headers 'Accept' => 'application/json', 'Content-Type' => 'application/json'
11
+ format :json
12
+
13
+ def initialize(server, options = nil)
14
+ @server = server
15
+ self.class.base_uri URI.join(@server, '/v1/').to_s
16
+
17
+ unless options.nil? || options[:username].nil? || options[:password].nil?
18
+ @auth = Base64.encode64("#{options[:username]}:#{options[:password]}")
19
+ end
20
+
21
+ @auth = ENV['KINTO_API_TOKEN'] if @auth.nil?
22
+ self.class.headers('Authorization' => "Basic #{@auth}")
23
+ end
24
+
25
+ def bucket (bucket_id)
26
+ @bucket = KintoBucket.new(self, bucket_id)
27
+ @bucket
28
+ end
29
+
30
+ def server_info
31
+ get '/'
32
+ end
33
+
34
+ def current_user_id
35
+ server_info['user']['id']
36
+ end
37
+
38
+ # buckets
39
+ def list_buckets
40
+ get '/buckets'
41
+ end
42
+
43
+ def create_bucket(bucket_id)
44
+ put "/buckets/#{bucket_id}"
45
+ bucket(bucket_id)
46
+ end
47
+
48
+ def delete_buckets
49
+ delete '/buckets'
50
+ end
51
+
52
+ def put(path, data = {})
53
+ ResponseHandler.handle self.class.put(path, :body => data.to_json)
54
+ end
55
+
56
+ def post(path, data = {})
57
+ ResponseHandler.handle self.class.post(path, :body => data.to_json)
58
+ end
59
+
60
+ def patch(path, data)
61
+ ResponseHandler.handle self.class.patch(path, :body => data.to_json)
62
+ end
63
+
64
+ def delete(path)
65
+ ResponseHandler.handle self.class.delete(path)
66
+ end
67
+
68
+ def get(path)
69
+ ResponseHandler.handle self.class.get(path)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,28 @@
1
+ require 'kinto_box/kinto_collection'
2
+ require 'kinto_box/kinto_object'
3
+ module KintoBox
4
+ class KintoBucket
5
+ include KintoObject
6
+
7
+ attr_accessor :id
8
+ attr_reader :kinto_client
9
+
10
+ def initialize (client, bucket_id)
11
+ raise ArgumentError if bucket_id.nil? || client.nil?
12
+
13
+ @kinto_client = client
14
+ @id = bucket_id
15
+ @url_path = "/buckets/#{@id}"
16
+ end
17
+
18
+ def collection (collection_id)
19
+ @collection = KintoCollection.new(self, collection_id)
20
+ @collection
21
+ end
22
+
23
+ def create_collection(collection_id)
24
+ @kinto_client.post("#{@url_path}/collections", { 'data' => { 'id' => collection_id}})
25
+ collection(collection_id)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,35 @@
1
+ require 'kinto_box/kinto_record'
2
+ require 'kinto_box/kinto_object'
3
+ require 'kinto_box/kinto_object'
4
+ module KintoBox
5
+ class KintoCollection
6
+ include KintoObject
7
+
8
+ attr_accessor :id
9
+ attr_reader :bucket
10
+
11
+ def initialize (bucket, collection_id)
12
+ raise ArgumentError if bucket.nil? || collection_id.nil?
13
+ @kinto_client = bucket.kinto_client
14
+ @bucket = bucket
15
+ @id = collection_id
16
+ @url_path = "/buckets/#{bucket.id}/collections/#{@id}"
17
+ end
18
+
19
+ def record (record_id, return_ref = false)
20
+ record = KintoRecord.new(self, record_id)
21
+ return record if return_ref
22
+ record.info
23
+ end
24
+
25
+ def list_records
26
+ @kinto_client.get("#{@url_path}/records")
27
+ end
28
+
29
+ def create_record(data)
30
+ resp = @kinto_client.post("#{@url_path}/records", { 'data' => data})
31
+ record_id = resp['data']['id']
32
+ record(record_id, true)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,58 @@
1
+ module KintoBox
2
+ module KintoObject
3
+ def info
4
+ @kinto_client.get(@url_path)
5
+ end
6
+
7
+ def delete
8
+ @kinto_client.delete(@url_path)
9
+ end
10
+
11
+ def update(data)
12
+ @kinto_client.patch(@url_path, {'data' => data})
13
+ end
14
+
15
+ def add_permission(principal, permission)
16
+ @kinto_client.patch(@url_path, {'permissions' => { permission_name(permission) => [principal_name(principal)] }})
17
+ return self
18
+ end
19
+
20
+ def replace_permission(principal, permission)
21
+ @kinto_client.put(@url_path, {'permissions' => { permission_name(permission) => [principal_name(principal)] }})
22
+ return self
23
+ end
24
+
25
+ def permissions
26
+ info['permissions']
27
+ end
28
+
29
+ private
30
+
31
+ def permission_name(permission)
32
+ case permission.downcase
33
+ when 'read'
34
+ return 'read'
35
+ when 'write'
36
+ return 'write'
37
+ when 'create'
38
+ return 'collection:create' if self.kind_of? KintoBucket
39
+ return 'record:create' if self.kind_of? KintoCollection
40
+ else
41
+ raise Exception 'not a valid permission'
42
+ end
43
+ end
44
+
45
+ def principal_name(principal)
46
+ case principal.downcase
47
+ when 'authenticated'
48
+ return 'system.Authenticated'
49
+ when 'anonymous'
50
+ return 'system.Everyone'
51
+ when 'everyone'
52
+ return 'system.Everyone'
53
+ else
54
+ return principal
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,17 @@
1
+ require 'kinto_box/kinto_object'
2
+ module KintoBox
3
+ class KintoRecord
4
+ include KintoObject
5
+ attr_reader :id
6
+ def initialize (collection, record_id)
7
+ raise ArgumentError if collection.nil? || record_id.nil?
8
+ @kinto_client = collection.bucket.kinto_client
9
+ @id = record_id
10
+ @url_path = "/buckets/#{collection.bucket.id}/collections/#{collection.id}/records/#{@id}"
11
+ end
12
+
13
+ def replace(data)
14
+ @kinto_client.put(@url_path, {'data' => data})
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,55 @@
1
+ module KintoBox
2
+ # Base exception class for all API Client exceptions
3
+ class Error < StandardError
4
+ attr_reader :response, :data
5
+ def initialize(response)
6
+ @response = response
7
+ @data ||= begin
8
+ JSON.parse(response.body)
9
+ rescue
10
+ {}
11
+ end
12
+
13
+ super("#{response.code} #{response.request.uri} #{response.body}")
14
+ end
15
+ end
16
+
17
+ # Raised when there was something wrong with the request
18
+ class BadRequest < Error; end
19
+
20
+ # Raised when the authentication information is incorrect or incomplete
21
+ class NotAllowed < Error; end
22
+
23
+ # Raised when the requested thing is not found
24
+ class NotFound < Error; end
25
+
26
+ # Raised when the user is not authorized
27
+ class NotAuthorized < Error; end
28
+
29
+ # Raised when there is some sort of error on the server
30
+ class ServerError < Error; end
31
+
32
+ class ResponseHandler
33
+ def self.handle(resp)
34
+ if [200, 201].include? resp.code
35
+ JSON.parse resp.body
36
+ elsif [202, 204].include? resp.code
37
+ true
38
+ elsif resp.code == 400
39
+ raise BadRequest, resp
40
+ elsif resp.code == 404
41
+ raise NotFound, resp
42
+ elsif resp.code == 401
43
+ raise NotAllowed, resp
44
+ elsif resp.code == 403
45
+ raise NotAuthorized, resp
46
+ elsif resp.code >= 500
47
+ raise ServerError, resp
48
+ else
49
+ raise Error, resp
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,3 @@
1
+ module KintoBox
2
+ VERSION = "0.1.2"
3
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kinto_box
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Kavya Sukumar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.8.3
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '1.8'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 1.8.3
75
+ - !ruby/object:Gem::Dependency
76
+ name: minitest
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '5.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '5.0'
89
+ description: Kinto http client in ruby
90
+ email:
91
+ - Kavya.Sukumar@voxmedia.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".travis.yml"
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - kinto_box.gemspec
105
+ - lib/kinto_box/kinto_box.rb
106
+ - lib/kinto_box/kinto_bucket.rb
107
+ - lib/kinto_box/kinto_collection.rb
108
+ - lib/kinto_box/kinto_object.rb
109
+ - lib/kinto_box/kinto_record.rb
110
+ - lib/kinto_box/response_handler.rb
111
+ - lib/kinto_box/version.rb
112
+ homepage: http://github.com/kavyasukumar/kinto_box
113
+ licenses:
114
+ - MIT
115
+ metadata:
116
+ allowed_push_host: https://rubygems.org
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.4.5
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Kinto http client in ruby
137
+ test_files: []