babili 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8cadf59b9e89b1cd4a7798bacef2ce71e21560b8
4
+ data.tar.gz: 93fa127f24e9ab435a2925c95418b7726f4ebcc5
5
+ SHA512:
6
+ metadata.gz: 474fc60e2d6aa098bcbddb8c9384ecc9e32649931287b37fd0033565e2700a5fb4b33979efda14c3634e62cb218bf2ba6784414d0a19bf21cd93a798a4b7bd93
7
+ data.tar.gz: 774b384c6367be18e65f97ff389f6b2fccd3c3b74dcd9541bb9dc680255489bf9db49f837dc5ca23fcd5e7e8db7fa903ed009eed7df3aa3b8bff7738cf554dca
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .DS_Store
16
+ .ruby*
17
+ *.gem
data/.gitkeep ADDED
File without changes
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,8 @@
1
+ he MIT License (MIT)
2
+ Copyright (c) 2016 Spin42 SPRL
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # Babili Ruby Client
2
+
3
+ ## Usage
4
+
5
+ * Install
6
+
7
+ ```
8
+ gem install "babili"
9
+ ```
10
+
11
+ * Initialize
12
+
13
+ ```
14
+ Babili.configure do |config|
15
+ config.api_scheme = <api_scheme> (default to https)
16
+ config.api_host = <api_host> (default to babili.spin42.me)
17
+ config.api_port = <api_port> (default to 443)
18
+ config.platform_token = <platform_token>
19
+ end
20
+ ```
21
+
22
+ * Use
23
+
24
+ ```
25
+ user = Babili::Platform::User.create
26
+ id = user.id
27
+ ```
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :console do
4
+ exec "irb -r babili -I ./lib"
5
+ end
data/babili.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 'babili/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "babili"
8
+ spec.version = Babili::VERSION
9
+ spec.authors = ["Spin42"]
10
+ spec.email = ["info@spin42.com"]
11
+ spec.summary = "Babili Ruby Client"
12
+ spec.description = ""
13
+ spec.homepage = "https://babili.spin42.me"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_dependency "rest-client", ">= 1.8.0"
22
+ spec.add_development_dependency "rspec", "3.4.0"
23
+ spec.add_development_dependency "webmock", "1.24.2"
24
+ end
data/lib/babili.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "ostruct"
2
+
3
+ require_relative "babili/client"
4
+ require_relative "babili/configuration"
5
+ require_relative "babili/errors"
6
+ require_relative "babili/platform/user"
7
+ require_relative "babili/platform/room"
8
+ require_relative "babili/platform/message"
9
+ require_relative "babili/platform/membership"
10
+ require_relative "babili/platform/messages_digest"
11
+ require_relative "babili/platform/token"
12
+
13
+ module Babili
14
+ def self.configure(&block)
15
+ yield @configuration ||= Babili::Configuration.new
16
+ end
17
+
18
+ def self.config
19
+ @configuration
20
+ end
21
+
22
+ def self.logger
23
+ @logger
24
+ end
25
+
26
+ configure do |config|
27
+ config.api_scheme = "https"
28
+ config.api_host = "babili-api.spin42.me"
29
+ config.api_port = 443
30
+ end
31
+ end
@@ -0,0 +1,52 @@
1
+ require "uri"
2
+ require "rest_client"
3
+
4
+ module Babili
5
+ class Client
6
+ def self.get(path, query_params = {})
7
+ execute(:get, uri(path), headers, query_params)
8
+ end
9
+
10
+ def self.post(path, payload, query_params = {})
11
+ execute(:post, uri(path), headers, nil, payload)
12
+ end
13
+
14
+ def self.put(path, payload, query_params = {})
15
+ execute(:put, uri(path), headers, nil, payload)
16
+ end
17
+
18
+ def self.delete(path, query_params = {})
19
+ execute(:delete, uri(path), headers, query_params)
20
+ end
21
+
22
+ private
23
+
24
+ def self.execute(method, url, headers, query_params = {}, payload = nil)
25
+ query = {
26
+ method: method,
27
+ url: url,
28
+ headers: { params: query_params }.merge(headers),
29
+ payload: payload ? payload.to_json : nil
30
+ }
31
+
32
+ raw_response = RestClient::Request.execute(query)
33
+ JSON.parse(raw_response)
34
+ end
35
+
36
+ def self.uri(path)
37
+ base_url = "#{Babili.config.api_scheme}://#{Babili.config.api_host}:#{Babili.config.api_port}"
38
+ URI.join(base_url, path).to_s
39
+ end
40
+
41
+ def self.headers
42
+ if !Babili.config.platform_token
43
+ raise TokenMissingError.new("You must define an platform token before calling the API.")
44
+ end
45
+ {
46
+ authorization: "Bearer #{Babili.config.platform_token}",
47
+ content_type: :json,
48
+ accept: :json
49
+ }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ module Babili
2
+ class Configuration
3
+ attr_accessor :api_scheme, :api_host, :api_port, :platform_token
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module Babili
2
+ class TokenMissingError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,37 @@
1
+ module Babili
2
+ module Platform
3
+ class Membership < OpenStruct
4
+ attr_accessor :room_id
5
+ attr_accessor :user_id
6
+
7
+ def self.path
8
+ "platform/rooms/:room_id/users/:user_id/membership"
9
+ end
10
+
11
+ def delete
12
+ delete_path = self.class.path.gsub(":room_id", room_id).gsub(":user_id", user_id)
13
+ raw_membership = Babili::Client.delete(delete_path, params)["data"]
14
+ membership = self.class.new(raw_membership["attributes"])
15
+ membership.room_id = raw_membership["relationships"]["room"]["data"]["id"]
16
+ membership.user_id = raw_membership["relationships"]["user"]["data"]["id"]
17
+ membership
18
+ end
19
+
20
+ def self.create(params = {})
21
+ room_id = params.delete(:room_id)
22
+ user_id = params.delete(:user_id)
23
+ create_path = path.gsub(":room_id", room_id).gsub(":user_id", user_id)
24
+ params = {
25
+ attributes: {
26
+ open: params[:open] || params["open"]
27
+ }
28
+ }
29
+ raw_membership = Babili::Client.post(create_path, params)["data"]
30
+ membership = new(raw_membership["attributes"])
31
+ membership.room_id = raw_membership["relationships"]["room"]["data"]["id"]
32
+ membership.user_id = raw_membership["relationships"]["user"]["data"]["id"]
33
+ membership
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,45 @@
1
+ module Babili
2
+ module Platform
3
+ class Message < OpenStruct
4
+ def self.path
5
+ "platform/rooms/:room_id/messages"
6
+ end
7
+
8
+ def self.create(params = {})
9
+ room_id = params.delete(:room_id)
10
+ create_path = path.gsub(":room_id", room_id)
11
+ sender_id = params[:sender_id] || params("sender_id")
12
+ formatted_params = {
13
+ data: {
14
+ type: "mesage",
15
+ id: params[:id] || params["id"],
16
+ relationships: {},
17
+ attributes: {
18
+ content: params[:content] || params["content"],
19
+ content_type: params[:content_type] || params["content_type"],
20
+ read_by_user_ids: params[:read_by_user_ids] || params["read_by_user_ids"]
21
+ }
22
+ }
23
+ }
24
+ if sender_id
25
+ formatted_params[:data][:relationships][:user] = {
26
+ data: { id: sender_id }
27
+ }
28
+ end
29
+ raw_message = Babili::Client.post(create_path, formatted_params)["data"]
30
+ message = new(raw_message["attributes"])
31
+ message.id = raw_message["id"]
32
+ message.room_id = room_id
33
+ message
34
+ end
35
+
36
+ def delete
37
+ path = self.class.path.gsub(":room_id", room_id) + "/#{id}"
38
+ raw_message = Babili::Client.delete(path)["data"]
39
+ message = self.class.new(raw_message["attributes"])
40
+ message.id = raw_message["id"]
41
+ message
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module Babili
2
+ module Platform
3
+ class MessagesDigest < OpenStruct
4
+ @path = "platform/messages-digests"
5
+
6
+ def self.create
7
+ raw_digest = Babili::Client.post(@path, {})
8
+ digest = new(raw_digest["meta"])
9
+ digest
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,81 @@
1
+ module Babili
2
+ module Platform
3
+ class Room < OpenStruct
4
+ def self.path
5
+ "platform/rooms"
6
+ end
7
+
8
+ def self.all
9
+ rooms = []
10
+ previous_first_seen_room_id = false
11
+ first_seen_room_id = nil
12
+ while previous_first_seen_room_id != first_seen_room_id
13
+ previous_first_seen_room_id = first_seen_room_id
14
+ if first_seen_room_id
15
+ raw_rooms = Babili::Client.get(path + "?firstSeenRoomId=#{first_seen_room_id}")
16
+ else
17
+ raw_rooms = Babili::Client.get(path)
18
+ end
19
+
20
+ return rooms if raw_rooms["data"].empty?
21
+ rooms.concat(raw_rooms["data"].map do |raw_room|
22
+ room = new(raw_room["attributes"])
23
+ room.id = raw_room["id"]
24
+ room
25
+ end)
26
+ first_seen_room_id = raw_rooms["data"].last["id"]
27
+ end
28
+ end
29
+
30
+ def self.create(params = {})
31
+ user_ids = params[:user_ids] || params["user_ids"]
32
+ formatted_params = {
33
+ data: {
34
+ id: params[:id] || params["id"],
35
+ attributes: {
36
+ name: params[:name] || params["name"]
37
+ },
38
+ relationships: {
39
+ users: {}
40
+ }
41
+ }
42
+ }
43
+ if user_ids
44
+ formatted_params[:data][:relationships][:users][:data] = user_ids.map do |user_id|
45
+ { id: user_id }
46
+ end
47
+ end
48
+ raw_room = Babili::Client.post(path, formatted_params)["data"]
49
+ room = new(raw_room["attributes"])
50
+ room.id = raw_room["id"]
51
+ room
52
+ end
53
+
54
+ def self.retreive(room_id)
55
+ retreive_path = path + "/#{room_id}"
56
+ raw_room = Babili::Client.get(retreive_path)["data"]
57
+ room = new(raw_room["attributes"])
58
+ room.id = raw_room["id"]
59
+ room
60
+ end
61
+
62
+ def delete
63
+ path = self.class.path + "/#{id}"
64
+ raw_room = Babili::Client.delete(path)["data"]
65
+ room = self.class.new(raw_room["attributes"])
66
+ room.id = raw_room["id"]
67
+ room
68
+ end
69
+
70
+ def users
71
+ path = self.class.path + "/#{id}/users"
72
+ raw_users = Babili::Client.get(path)
73
+ raw_users["data"].map do |raw_user|
74
+ user = Babili::Platform::User.new(raw_user["attributes"])
75
+ user.id = raw_user["id"]
76
+ user
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,13 @@
1
+ module Babili
2
+ module Platform
3
+ class Token < OpenStruct
4
+ @path = "platform/users/:user_id/tokens"
5
+
6
+ def self.for_user_id(user_id)
7
+ path = @path.gsub(":user_id", user_id)
8
+ raw_token = Babili::Client.post(path, {})
9
+ raw_token["data"]["attributes"]["token"]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,46 @@
1
+ module Babili
2
+ module Platform
3
+ class User < OpenStruct
4
+ def self.path
5
+ "platform/users"
6
+ end
7
+
8
+ def self.all
9
+ raw_users = Babili::Client.get(path)
10
+ raw_users["data"].map do |raw_user|
11
+ user = new(raw_user["attributes"])
12
+ user.id = raw_user["id"]
13
+ user
14
+ end
15
+ end
16
+
17
+ def self.create(params = {})
18
+ params = {
19
+ data: {
20
+ id: params["id"] || params [:id],
21
+ attributes: {
22
+ alive_at: params["alive_at"] || params [:alive_at],
23
+ status: params["status"] || params [:status],
24
+ created_at: params["created_at"] || params [:created_at],
25
+ updated_at: params["updated_at"] || params [:updated_at],
26
+ last_messages_digest_sent_at: params["last_messages_digest_sent_at"] || params [:last_messages_digest_sent_at]
27
+ }
28
+ }
29
+ }
30
+
31
+ raw_user = Babili::Client.post(path, params)["data"]
32
+ user = new(raw_user["attributes"])
33
+ user.id = raw_user["id"]
34
+ user
35
+ end
36
+
37
+ def delete
38
+ path = self.class.path + "/#{id}"
39
+ raw_user = Babili::Client.delete(path)["data"]
40
+ user = self.class.new(raw_user["attributes"])
41
+ user.id = raw_user["id"]
42
+ user
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Babili
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ RSpec.describe Babili::Platform::Room do
2
+ describe "#all" do
3
+ context "when there are no rooms" do
4
+ before(:each) do
5
+ stub_request(:get, "https://babili-api.spin42.me/platform/rooms")
6
+ .to_return(body: '{"data": [], "meta": { "count": 0 } }')
7
+ end
8
+
9
+ it "should return an empty array" do
10
+ expect(Babili::Platform::Room.all.size).to eq(0)
11
+ end
12
+ end
13
+
14
+ context "when there is a Room" do
15
+ before(:each) do
16
+ stub_request(:get, "https://babili-api.spin42.me/platform/rooms")
17
+ .to_return(body: '{"data": [{"type": "room", "data": {"id": 1234}}], "meta": { "count": 1 } }')
18
+ end
19
+
20
+ it "should not return an empty array" do
21
+ expect(Babili::Platform::Room.all.size).to eq(1)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ RSpec.describe Babili::Platform::User, ".all" do
2
+ context "when there are no users" do
3
+ before(:each) do
4
+ stub_request(:get, "https://babili-api.spin42.me/platform/users")
5
+ .to_return(body: '{"data": [], "meta": { "count": 0 } }')
6
+ end
7
+
8
+ it "should return an empty array" do
9
+ expect(Babili::Platform::User.all.size).to eq(0)
10
+ end
11
+ end
12
+
13
+ context "when is a user" do
14
+ before(:each) do
15
+ stub_request(:get, "https://babili-api.spin42.me/platform/users")
16
+ .to_return(body: '{"data": [{"type": "user", "data": {"id": 1234}}], "meta": { "count": 1 } }')
17
+ end
18
+
19
+ it "should not return an empty array" do
20
+ expect(Babili::Platform::User.all.size).to eq(1)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ require "webmock/rspec"
2
+ require_relative "../lib/babili"
3
+
4
+ RSpec.configure do |config|
5
+ config.expect_with :rspec do |expectations|
6
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
7
+ end
8
+
9
+ config.mock_with :rspec do |mocks|
10
+ mocks.verify_partial_doubles = true
11
+ end
12
+
13
+ config.before(:example) do
14
+ Babili.configure do |config|
15
+ config.token = "TOKEN"
16
+ end
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: babili
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Spin42
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.4.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.4.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.24.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.24.2
55
+ description: ''
56
+ email:
57
+ - info@spin42.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".gitkeep"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - babili.gemspec
70
+ - lib/babili.rb
71
+ - lib/babili/client.rb
72
+ - lib/babili/configuration.rb
73
+ - lib/babili/errors.rb
74
+ - lib/babili/platform/membership.rb
75
+ - lib/babili/platform/message.rb
76
+ - lib/babili/platform/messages_digest.rb
77
+ - lib/babili/platform/room.rb
78
+ - lib/babili/platform/token.rb
79
+ - lib/babili/platform/user.rb
80
+ - lib/babili/version.rb
81
+ - spec/platform/room_spec.rb
82
+ - spec/platform/user_spec.rb
83
+ - spec/spec_helper.rb
84
+ homepage: https://babili.spin42.me
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.2.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Babili Ruby Client
108
+ test_files:
109
+ - spec/platform/room_spec.rb
110
+ - spec/platform/user_spec.rb
111
+ - spec/spec_helper.rb