quickblox-rb 0.1.0

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: 694963a669f27b2ee03e195281aa3468cda2ba2b
4
+ data.tar.gz: 50cc70ee95e0b84634014de900634cfac75fe840
5
+ SHA512:
6
+ metadata.gz: dad34d550c462ac9007a1ec770f8c493b780c55fbf489b69e6cb5f1f9600bce4f129d39b4f5af01778592e2fc9cc6b8c1f3ecedfc023a404183f8e2231c38d15
7
+ data.tar.gz: a8ef607b86e59faef52641a1af729386fb7ad492c5b3140819404d068bfcfbdec94bbb21472ec0886ef027f797b0cb319f0db4d10e34f36558114c9f467c689d
data/.gems ADDED
@@ -0,0 +1,3 @@
1
+ cutest -v 1.2.3
2
+ mocoso -v 1.2.3
3
+ requests -v 1.0.0
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .gs/
data/Makefile ADDED
@@ -0,0 +1,4 @@
1
+ build:
2
+ cutest test/**/*_test.rb
3
+
4
+ .PHONY: build
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # Quickblox API for Ruby
2
+
3
+ This is a gem to work with [Quickblox API](http://quickblox.com/developers/Overview) in Ruby.
4
+
data/lib/quickblox.rb ADDED
@@ -0,0 +1,85 @@
1
+ require "requests/sugar"
2
+ require "securerandom"
3
+ require "json"
4
+
5
+ class Quickblox
6
+ QB_ENDPOINT = "https://api.quickblox.com".freeze
7
+ QB_HEADER_API_VERSION = "QuickBlox-REST-API-Version".freeze
8
+ QB_HEADER_TOKEN = "QB-Token".freeze
9
+
10
+ attr :auth_key,
11
+ :auth_secret,
12
+ :application_id,
13
+ :email,
14
+ :password,
15
+ :session
16
+
17
+ def initialize(**args)
18
+ @auth_key = args.fetch(:auth_key)
19
+ @auth_secret = args.fetch(:auth_secret)
20
+ @application_id = args.fetch(:application_id)
21
+ @email = args[:email]
22
+ @password = args[:password]
23
+ end
24
+
25
+ def create_session
26
+ data = {
27
+ "application_id" => application_id,
28
+ "auth_key" => auth_key,
29
+ "nonce" => SecureRandom.random_number(100000),
30
+ "timestamp" => Time.now.to_i,
31
+ }
32
+
33
+ data["user[email]"] = email if email
34
+ data["user[password]"] = password if password
35
+
36
+ signature = sign(data)
37
+
38
+ data["signature"] = signature
39
+
40
+ response = Requests.post(
41
+ QB_ENDPOINT + "/session.json",
42
+ headers: { QB_HEADER_API_VERSION => "0.1.1" },
43
+ data: data
44
+ )
45
+
46
+ if response.status == 201
47
+ @session = response.json.fetch("session")
48
+ end
49
+ end
50
+
51
+ def transcript(dialog_id:)
52
+ response = Requests.get(
53
+ QB_ENDPOINT + "/chat/Message.json",
54
+ headers: {
55
+ QB_HEADER_API_VERSION => "0.1.1",
56
+ QB_HEADER_TOKEN => session_token
57
+ },
58
+ params: {
59
+ chat_dialog_id: dialog_id,
60
+ mark_as_read: 0
61
+ }
62
+ )
63
+
64
+ if response.status == 200
65
+ response.json
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def sign(data)
72
+ normalized_string = data.each_key
73
+ .sort
74
+ .map { |key| "#{key}=#{data[key]}" }
75
+ .join("&")
76
+
77
+ sha1 = OpenSSL::Digest::SHA1.new
78
+ OpenSSL::HMAC.hexdigest(sha1, auth_secret, normalized_string)
79
+ end
80
+
81
+ def session_token
82
+ (@session || create_session).fetch("token")
83
+ end
84
+ end
85
+
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "quickblox-rb"
3
+ s.version = "0.1.0"
4
+ s.summary = "Ruby gem to work with Quickblox API"
5
+ s.description = s.summary
6
+ s.authors = ["Lucas Tolchinsky"]
7
+ s.email = ["lucas.tolchinsky@gmail.com"]
8
+ s.homepage = "https://github.com/properati/quickblox-rb"
9
+ s.license = "MIT"
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_runtime_dependency "requests"
14
+
15
+ s.add_development_dependency "cutest"
16
+ s.add_development_dependency "mocoso"
17
+ end
18
+
@@ -0,0 +1,79 @@
1
+ require "cutest"
2
+ require "mocoso"
3
+ require_relative "../lib/quickblox"
4
+
5
+ include Mocoso
6
+
7
+ scope "initialization" do
8
+ test "with application credentials" do
9
+ qb = Quickblox.new(
10
+ auth_key: "AUTH_KEY",
11
+ auth_secret: "AUTH_SECRET",
12
+ application_id: 1234,
13
+ )
14
+
15
+ assert_equal "AUTH_KEY", qb.auth_key
16
+ assert_equal "AUTH_SECRET", qb.auth_secret
17
+ assert_equal 1234, qb.application_id
18
+ assert qb.email.nil?
19
+ assert qb.password.nil?
20
+ end
21
+
22
+ test "with user credentials" do
23
+ qb = Quickblox.new(
24
+ auth_key: "AUTH_KEY",
25
+ auth_secret: "AUTH_SECRET",
26
+ application_id: 1234,
27
+ email: "account@owner.com",
28
+ password: "foobarbaz"
29
+ )
30
+
31
+ assert_equal "AUTH_KEY", qb.auth_key
32
+ assert_equal "AUTH_SECRET", qb.auth_secret
33
+ assert_equal 1234, qb.application_id
34
+ assert_equal "account@owner.com", qb.email
35
+ assert_equal "foobarbaz", qb.password
36
+ end
37
+ end
38
+
39
+ test "create session" do
40
+ qb = Quickblox.new(
41
+ auth_key: "AUTH_KEY",
42
+ auth_secret: "AUTH_SECRET",
43
+ application_id: 1234,
44
+ email: "account@owner.com",
45
+ password: "foobarbaz"
46
+ )
47
+
48
+ # This is a real life example of a response to POST /session.json
49
+ mock_response = Requests::Response.new(
50
+ 201,
51
+ {
52
+ "access-control-allow-origin"=>["*"],
53
+ "cache-control"=>["max-age=0, private, must-revalidate"],
54
+ "content-type"=>["application/json; charset=utf-8"],
55
+ "date"=>["Mon, 02 May 2016 17:52:01 GMT"],
56
+ "etag"=>["\"442ceb087b8b83fee1ba7f28e082caca\""],
57
+ "qb-token-expirationdate"=>["2016-05-02 19:52:00 UTC"],
58
+ "quickblox-rest-api-version"=>["0.1.1"],
59
+ "server"=>["nginx/1.8.1"],
60
+ "status"=>["201 Created"],
61
+ "strict-transport-security"=>["max-age=15768000;"],
62
+ "x-rack-cache"=>["invalidate, pass"],
63
+ "x-request-id"=>["2d7b3f554389b95e8561d89fc77104bd"],
64
+ "x-runtime"=>["0.014038"],
65
+ "x-ua-compatible"=>["IE=Edge,chrome=1"],
66
+ "content-length"=>["259"],
67
+ "connection"=>["keep-alive"]
68
+ },
69
+ "{\"session\":{\"_id\":\"572793c0a28f9a658800002a\",\"application_id\":35265,\"created_at\":\"2016-05-02T17:52:00Z\",\"device_id\":0,\"nonce\":29601,\"token\":\"le-token-stuff\",\"ts\":1462211496,\"updated_at\":\"2016-05-02T17:52:00Z\",\"user_id\":0,\"id\":18862}}"
70
+ )
71
+
72
+ assert qb.session.nil?
73
+
74
+ stub(Requests, :request, mock_response) { qb.create_session }
75
+
76
+ assert qb.session
77
+ assert_equal "le-token-stuff", qb.session.fetch("token")
78
+ end
79
+
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quickblox-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Tolchinsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: requests
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cutest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mocoso
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Ruby gem to work with Quickblox API
56
+ email:
57
+ - lucas.tolchinsky@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gems"
63
+ - ".gitignore"
64
+ - Makefile
65
+ - README.md
66
+ - lib/quickblox.rb
67
+ - quickblox-rb.gemspec
68
+ - test/quickblox_test.rb
69
+ homepage: https://github.com/properati/quickblox-rb
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.2.2
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Ruby gem to work with Quickblox API
93
+ test_files: []