ribose 0.3.1 → 0.5.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.
@@ -7,42 +7,41 @@ RSpec.describe Ribose::Client do
7
7
  client = Ribose::Client.new
8
8
 
9
9
  expect(client.api_token).to eq(Ribose.configuration.api_token)
10
- expect(client.user_email).to eq(Ribose.configuration.user_email)
10
+ expect(client.api_email).to eq(Ribose.configuration.api_email)
11
11
  end
12
12
  end
13
13
 
14
14
  context "custom attribtues provided" do
15
15
  it "initialize with the supplied attribtues" do
16
- client = Ribose::Client.new(token: 123, email: "john@ex.com")
16
+ email = "john@ex.com"
17
+ token = "SECRET_API_TOKEN"
18
+ client = Ribose::Client.new(api_token: token, api_email: email)
17
19
 
18
- expect(client.api_token).to eq("123")
19
- expect(client.user_email).to eq("john@ex.com")
20
+ expect(client.api_token).to eq(token)
21
+ expect(client.api_email).to eq(email)
20
22
  end
21
23
  end
22
24
  end
23
25
 
24
26
  describe ".from_login" do
25
27
  it "authenticate the user and build a client" do
26
- email = "useremail@example..com"
27
- password = "supersecretpassword"
28
+ email = "user.email@gmail.com"
29
+ password = "supser.secrect.password"
28
30
 
29
- allow(Ribose::Session).to receive(:create).and_return(session_hash)
31
+ allow(Ribose::Session).to receive(:create).and_return(session)
30
32
  client = Ribose::Client.from_login(email: email, password: password)
31
33
 
32
- expect(client.user_email).to eq(email)
33
- expect(client.api_token).to eq(session_hash["authentication_token"])
34
+ expect(client.api_email).to eq(email)
35
+ expect(client.uid).to eq(session.uid)
36
+ expect(client.client_id).to eq(session.client)
34
37
  end
35
38
  end
36
39
 
37
- def session_hash
38
- {
39
- "authentication_token" => "SecretToken",
40
- "last_activity" => {
41
- "id" => 122072207,
42
- "session_id" => "SessionId",
43
- "browser" => "Ribose Ruby Client",
44
- "user_id" => "user-uuid-123-4567"
45
- },
46
- }
40
+ def session
41
+ @session ||= Ribose::SessionData.new(
42
+ uid: "hello",
43
+ expiry: Time.now + 3600,
44
+ client: "RIBOSE_RUBY_CLIENT",
45
+ )
47
46
  end
48
47
  end
@@ -20,7 +20,6 @@ RSpec.describe Ribose::Config do
20
20
  expect(Ribose.configuration.debug_mode?).to be_falsey
21
21
  expect(Ribose.configuration.api_token).to eq(api_token)
22
22
  expect(Ribose.configuration.user_email).to eq(user_email)
23
- expect(Ribose.configuration.web_url).to eq ["https", api_host].join("://")
24
23
  end
25
24
  end
26
25
 
@@ -23,4 +23,15 @@ RSpec.describe Ribose::Connection do
23
23
  expect(suggestions.first.name).to eq("Jennie Doe")
24
24
  end
25
25
  end
26
+
27
+ describe ".disconnect" do
28
+ it "disconnect with provided connection" do
29
+ connection_id = 123_456
30
+ stub_ribose_connection_delete_api(connection_id)
31
+
32
+ expect do
33
+ Ribose::Connection.disconnect(connection_id)
34
+ end.not_to raise_error
35
+ end
36
+ end
26
37
  end
@@ -7,18 +7,31 @@ RSpec.describe Ribose::FileUploader do
7
7
  space_id = 123_456_789
8
8
 
9
9
  stub_ribose_space_file_upload_api(space_id, file_attributes)
10
- file_upload = Ribose::FileUploader.upload(space_id, file_attributes)
10
+ file_upload = Ribose::FileUploader.upload(space_id, **file_attributes)
11
11
 
12
12
  expect(file_upload.attachment.id).not_to be_nil
13
13
  expect(file_upload.attachment.author).to eq("John Doe")
14
14
  expect(file_upload.attachment.content_type).to eq("image/png")
15
15
  end
16
16
  end
17
+
18
+ context "with unknown file type" do
19
+ it "creates a new upload as octet-stream" do
20
+ space_id = 123_456_789
21
+ attributes = file_attributes(File.join(Ribose.root, "Rakefile"))
22
+
23
+ stub_ribose_space_file_upload_api(space_id, attributes)
24
+ file_upload = Ribose::FileUploader.upload(space_id, **attributes)
25
+
26
+ expect(file_upload.attachment.id).not_to be_nil
27
+ expect(file_upload.attachment.author).to eq("John Doe")
28
+ end
29
+ end
17
30
  end
18
31
 
19
- def file_attributes
32
+ def file_attributes(file = nil)
20
33
  {
21
- file: sample_fixture_file,
34
+ file: file || sample_fixture_file,
22
35
  tag_list: "sample, file, samplefile",
23
36
  description: "This is a sample file",
24
37
  }
@@ -17,4 +17,60 @@ RSpec.describe Ribose::FileVersion do
17
17
  expect(file_version.current_version_id).to eq(789012)
18
18
  end
19
19
  end
20
+
21
+ describe ".download" do
22
+ context "with version id specified" do
23
+ it "downloads the specific file version" do
24
+ file_id = 123_456
25
+ space_id = 456_789
26
+ version_id = 789_012
27
+
28
+ output_file = "./tmp/download"
29
+ content = "This is the content in the file"
30
+
31
+ stub_aws_file_version_download_api(content)
32
+ buffer = stub_file_write_to_io(output_file)
33
+ stub_ribose_file_version_download_api(space_id, file_id, version_id)
34
+
35
+ Ribose::FileVersion.download(
36
+ space_id, file_id, version_id: version_id, output: output_file
37
+ )
38
+
39
+ expect(buffer).to eq(content)
40
+ end
41
+ end
42
+ end
43
+
44
+ describe ".create" do
45
+ it "create a new file version" do
46
+ file_id = 123_456
47
+ space_id = 456_789
48
+
49
+ stub_ribose_space_file_upload_api(space_id, file_attributes, file_id)
50
+ file = Ribose::FileVersion.create(space_id, file_id, **file_attributes)
51
+
52
+ expect(file.id).not_to be_nil
53
+ expect(file.author).to eq("John Doe")
54
+ expect(file.content_type).to eq("image/png")
55
+ end
56
+ end
57
+
58
+ def file_attributes
59
+ {
60
+ file: sample_fixture_file,
61
+ description: "Version 2.0",
62
+ tag_list: "tags for new version",
63
+ }
64
+ end
65
+
66
+ def sample_fixture_file
67
+ @sample_fixture_file ||= File.join(Ribose.root, "spec/fixtures/sample.png")
68
+ end
69
+
70
+ def stub_file_write_to_io(output_file)
71
+ buffer = StringIO.new
72
+ allow(File).to receive(:open).with(output_file, "w").and_yield(buffer)
73
+
74
+ buffer.string
75
+ end
20
76
  end
@@ -20,9 +20,10 @@ RSpec.describe Ribose::Message do
20
20
  describe ".create" do
21
21
  it "creates a new message into a conversation" do
22
22
  space_id = 123_456
23
+ message_attributes = message_attrs.merge(space_id: space_id)
23
24
 
24
25
  stub_ribose_message_create(space_id, message: message_attrs)
25
- message = Ribose::Message.create(message_attrs.merge(space_id: space_id))
26
+ message = Ribose::Message.create(**message_attributes)
26
27
 
27
28
  expect(message.id).not_to be_nil
28
29
  expect(message.user.name).to eq("John Doe")
@@ -37,7 +38,7 @@ RSpec.describe Ribose::Message do
37
38
 
38
39
  stub_ribose_message_update(space_id, message_id, message: message_attrs)
39
40
  message = Ribose::Message.update(
40
- message_attrs.merge(space_id: space_id, message_id: message_id),
41
+ **message_attrs.merge(space_id: space_id, message_id: message_id),
41
42
  )
42
43
 
43
44
  expect(message.user.name).to eq("John Doe")
@@ -6,41 +6,35 @@ RSpec.describe Ribose::Session do
6
6
  username = "super.user"
7
7
  password = "supper.secreet.password"
8
8
 
9
- stub_session_creation_request_via_web
9
+ stub_post_signin_request
10
10
  session = Ribose::Session.create(username: username, password: password)
11
11
 
12
- expect(session["created_at"]).not_to be_nil
13
- expect(session["authentication_token"]).to eq("user-super-secret-token")
12
+ expect(session.uid).to eq(session_headers["uid"])
13
+ expect(session.client).to eq(session_headers["client"])
14
+ expect(session["access-token"]).to eq(session_headers["access-token"])
14
15
  end
15
16
  end
16
17
 
17
- def login_url
18
- ribose_url_for("login")
19
- end
20
-
21
- def ribose_url_for(*endpoints)
22
- [Ribose.configuration.web_url, *endpoints].join("/")
18
+ def session_headers
19
+ @session_headers ||= {
20
+ "uid" => "user@example.com",
21
+ "expiry" => Time.now,
22
+ "client" => "sample-user-client",
23
+ "access-token" => "super.secret.access.token",
24
+ }
23
25
  end
24
26
 
25
- def stub_session_creation_request_via_web
26
- stub_get_request_with_login_page
27
- stub_post_request_with_empty_body
28
- stub_general_inforomation_request
29
- end
30
-
31
- def stub_get_request_with_login_page
32
- stub_request(:get, login_url).and_return(
33
- body: ribose_fixture("login", "html"),
34
- headers: { content_type: "text/html" },
27
+ def stub_post_signin_request
28
+ stub_request(:post, login_url).and_return(
29
+ body: ribose_fixture("empty"), headers: session_headers,
35
30
  )
36
31
  end
37
32
 
38
- def stub_general_inforomation_request
39
- stub_request(:get, ribose_url_for(["settings", "general", "info"])).
40
- and_return(body: ribose_fixture("general_information"), status: 200)
33
+ def login_url
34
+ [api_host, "api/v2/auth/sign_in"].join("/")
41
35
  end
42
36
 
43
- def stub_post_request_with_empty_body
44
- stub_request(:post, login_url).and_return(body: ribose_fixture("empty"))
37
+ def api_host
38
+ "https://#{Ribose.configuration.api_host}"
45
39
  end
46
40
  end
@@ -28,12 +28,43 @@ RSpec.describe Ribose::SpaceFile do
28
28
  end
29
29
  end
30
30
 
31
+ describe ".download" do
32
+ context "without specific version id" do
33
+ it "fetch version id and then downloads the file" do
34
+ file_id = 123_456_789
35
+ space_id = 456_789_012
36
+
37
+ allow(Ribose::FileVersion).to receive(:download)
38
+ stub_ribose_space_file_fetch_api(space_id, file_id)
39
+
40
+ Ribose::SpaceFile.download(space_id, file_id)
41
+
42
+ expect(Ribose::FileVersion).to have_received(:download).
43
+ with(space_id, file_id, version_id: 11559)
44
+ end
45
+ end
46
+
47
+ context "with specific version id" do
48
+ it "sends downlod message to the downloader" do
49
+ file_id = 123_456_789
50
+ space_id = 456_789_012
51
+ version_id = 123_456_789
52
+
53
+ allow(Ribose::FileVersion).to receive(:download)
54
+ Ribose::SpaceFile.download(space_id, file_id, version_id: version_id)
55
+
56
+ expect(Ribose::FileVersion).to have_received(:download).
57
+ with(space_id, file_id, version_id: version_id)
58
+ end
59
+ end
60
+ end
61
+
31
62
  describe ".create" do
32
63
  it "creates a new file with provided details" do
33
64
  space_id = 123_456_789
34
65
 
35
66
  stub_ribose_space_file_upload_api(space_id, file_attributes)
36
- file = Ribose::SpaceFile.create(space_id, file_attributes)
67
+ file = Ribose::SpaceFile.create(space_id, **file_attributes)
37
68
 
38
69
  expect(file.id).not_to be_nil
39
70
  expect(file.author).to eq("John Doe")
@@ -28,7 +28,7 @@ RSpec.describe Ribose::Space do
28
28
  describe ".create" do
29
29
  it "creates a new space with provided details" do
30
30
  stub_ribose_space_create_api(space_attributes)
31
- space = Ribose::Space.create(space_attributes)
31
+ space = Ribose::Space.create(**space_attributes)
32
32
 
33
33
  expect(space.id).not_to be_nil
34
34
  expect(space.visibility).to eq("invisible")
@@ -59,6 +59,17 @@ RSpec.describe Ribose::Space do
59
59
  end
60
60
  end
61
61
 
62
+ describe ".delete" do
63
+ it "deletes an existing space" do
64
+ space_id = 123_456_789
65
+ stub_ribose_space_remove_api(space_id, password_confirmation: 1234)
66
+
67
+ expect do
68
+ Ribose::Space.delete(space_id, confirmation: 1234)
69
+ end.not_to raise_error
70
+ end
71
+ end
72
+
62
73
  def space_attributes
63
74
  {
64
75
  access: "private",
@@ -18,7 +18,7 @@ RSpec.describe Ribose::User do
18
18
  describe ".activate" do
19
19
  it "complete the user signup process" do
20
20
  stub_ribose_app_user_activate_api(user_attributes)
21
- user = Ribose::User.activate(user_attributes)
21
+ user = Ribose::User.activate(**user_attributes)
22
22
 
23
23
  expect(user.id).not_to be_nil
24
24
  expect(user.name).to eq("John Doe")
@@ -30,7 +30,7 @@ RSpec.describe Ribose::User do
30
30
  @user_attributes ||= {
31
31
  email: "john.doe@example.com",
32
32
  password: "SecurePassword",
33
- otp: "OTP_RECEIVED_VIA_EMAIL",
33
+ edata: "OTP_RECEIVED_VIA_EMAIL",
34
34
  }
35
35
  end
36
36
 
data/spec/spec_helper.rb CHANGED
@@ -15,8 +15,9 @@ RSpec.configure do |config|
15
15
 
16
16
  config.before :all do
17
17
  Ribose.configure do |ribose_config|
18
- ribose_config.api_token = ENV["RIBOSE_API_TOKEN"] || "RIBOSE_API_TOKEN"
19
18
  ribose_config.user_email = ENV["RIBOSE_USER_EMAIL"] || "RIBOSE_USER_EMAIL"
19
+ ribose_config.user_password = ENV["RIBOSE_USER_PASSWORD"] || "SECRET_PASS"
20
+ ribose_config.client = Ribose::Client.new
20
21
  end
21
22
  end
22
23
  end
@@ -30,6 +30,12 @@ module Ribose
30
30
  )
31
31
  end
32
32
 
33
+ def stub_ribose_space_delete_api(space_id, options = {})
34
+ stub_api_response(
35
+ :delete, "spaces/#{space_id}", data: options, filename: "empty"
36
+ )
37
+ end
38
+
33
39
  def stub_ribose_feed_api
34
40
  stub_api_response(:get, "feeds", filename: "feeds")
35
41
  end
@@ -173,7 +179,7 @@ module Ribose
173
179
  def stub_ribose_app_user_activate_api(attributes)
174
180
  stub_api_response(
175
181
  :post,
176
- "signup.user",
182
+ "api/v2/auth",
177
183
  data: { user: attributes },
178
184
  filename: "user_activated",
179
185
  )
@@ -220,6 +226,17 @@ module Ribose
220
226
  )
221
227
  end
222
228
 
229
+ def stub_ribose_file_version_download_api(sid, fid, vid)
230
+ version = ["spaces", sid, "file/files", fid, "versions", vid].join("/")
231
+ stub_request(:get, ribose_endpoint(version)).to_return(
232
+ headers: { location: "https://ribose-data.aws.com", status: 302 },
233
+ )
234
+ end
235
+
236
+ def stub_aws_file_version_download_api(content)
237
+ stub_request(:get, "https://ribose-data.aws.com").to_return(body: content)
238
+ end
239
+
223
240
  def stub_ribose_space_conversation_list(space_id)
224
241
  stub_api_response(
225
242
  :get, conversations_path(space_id), filename: "conversations"
@@ -310,6 +327,12 @@ module Ribose
310
327
  stub_api_response(:get, "people/connections?s=", filename: "connections")
311
328
  end
312
329
 
330
+ def stub_ribose_connection_delete_api(id)
331
+ stub_api_response(
332
+ :delete, ["people", "connections", id].join("/"), filename: "empty"
333
+ )
334
+ end
335
+
313
336
  def stub_ribose_suggestion_list_api
314
337
  stub_api_response(
315
338
  :get, "people_finding", filename: "connection_suggestion"
@@ -526,16 +549,16 @@ module Ribose
526
549
 
527
550
  if Ribose.configuration.api_token
528
551
  headers["X-Indigo-Token"] = client.api_token
529
- headers["X-Indigo-Email"] = client.user_email
552
+ headers["X-Indigo-Email"] = client.api_email
530
553
  end
531
554
  end
532
555
  end
533
556
 
534
- def response_with(filename:, status:)
557
+ def response_with(filename:, status:, content_type: "application/json")
535
558
  {
536
559
  status: status,
537
560
  body: ribose_fixture(filename),
538
- headers: { content_type: "application/json" },
561
+ headers: { content_type: content_type },
539
562
  }
540
563
  end
541
564
 
@@ -546,11 +569,15 @@ module Ribose
546
569
  File.read(File.expand_path(file_path, __FILE__))
547
570
  end
548
571
 
549
- def stub_api_response(method, endpoint, filename:,
550
- status: 200, data: nil, client: nil)
572
+ def stub_api_response(method, endpoint, filename:, status: 200, data: nil,
573
+ client: nil, content_type: "application/json")
551
574
  stub_request(method, ribose_endpoint(endpoint)).
552
575
  with(ribose_headers(data: data, client: client)).
553
- to_return(response_with(filename: filename, status: status))
576
+ to_return(
577
+ response_with(
578
+ filename: filename, status: status, content_type: content_type,
579
+ ),
580
+ )
554
581
  end
555
582
  end
556
583
  end
@@ -2,16 +2,16 @@ require "faraday"
2
2
 
3
3
  module Ribose
4
4
  module FileUploadStub
5
- def stub_ribose_space_file_upload_api(space_id, attributes)
5
+ def stub_ribose_space_file_upload_api(space_id, attributes, file_id = nil)
6
6
  stub_ribose_aws_s3_file_upload_api
7
- stub_ribose_file_prepare_api(space_id, attributes)
8
- stub_ribose_file_upload_notify_api(space_id, attributes)
7
+ stub_ribose_file_prepare_api(space_id, attributes, file_id)
8
+ stub_ribose_file_upload_notify_api(space_id, attributes, file_id)
9
9
  end
10
10
 
11
- def stub_ribose_file_prepare_api(space_id, attributes)
11
+ def stub_ribose_file_prepare_api(space_id, attributes, file_id = nil)
12
12
  stub_api_response(
13
13
  :get,
14
- ribose_prepare_endpoint(space_id, attributes),
14
+ ribose_prepare_endpoint(space_id, attributes, file_id),
15
15
  filename: "file_upload_prepared",
16
16
  )
17
17
  end
@@ -22,23 +22,26 @@ module Ribose
22
22
  to_return(response_with(filename: "empty", status: 200))
23
23
  end
24
24
 
25
- def stub_ribose_file_upload_notify_api(space_id, attributes)
25
+ def stub_ribose_file_upload_notify_api(space_id, attributes, file_id = nil)
26
26
  stub_api_response(
27
27
  :post,
28
- ribose_file_endpoint(space_id),
29
- data: build_notify_request_body(attributes),
28
+ ribose_file_endpoint(space_id, file_id),
29
+ data: build_notify_request_body(attributes, file_id),
30
30
  filename: "file_uploaded",
31
+ content_type: "text/html",
31
32
  )
32
33
  end
33
34
 
34
35
  private
35
36
 
36
- def ribose_file_endpoint(space_id)
37
- ["spaces", space_id, "file", "files"].join("/")
37
+ def ribose_file_endpoint(space_id, file_id = nil)
38
+ end_path = file_id ? "#{file_id}/versions" : nil
39
+ ["spaces", space_id, "file", "files", end_path].compact.join("/")
38
40
  end
39
41
 
40
- def ribose_prepare_endpoint(sid, attrs)
41
- [ribose_file_endpoint(sid), "prepare?#{prepare_params(attrs)}"].join("/")
42
+ def ribose_prepare_endpoint(sid, attrs, file_id = nil)
43
+ [ribose_file_endpoint(sid, file_id), "prepare?#{prepare_params(attrs)}"].
44
+ join("/")
42
45
  end
43
46
 
44
47
  def prepare_params(attributes)
@@ -49,16 +52,19 @@ module Ribose
49
52
  )
50
53
  end
51
54
 
52
- def build_notify_request_body(attributes)
55
+ def build_notify_request_body(attributes, file_id = nil)
56
+ file_info_key = file_id ? "file_info_version" : "file_info"
57
+
53
58
  extract_file_details(attributes).merge(
54
- file_info: extract_file_info(attributes),
59
+ file_info_key.to_sym => extract_file_info(attributes),
55
60
  key: "uploads/123456789/${filename}",
56
61
  )
57
62
  end
58
63
 
59
64
  def content_type_form_file(file)
60
65
  require "mime/types"
61
- MIME::Types.type_for(file).first.content_type
66
+ mime = MIME::Types.type_for(file).first
67
+ mime ? mime.content_type : "application/octet-stream"
62
68
  end
63
69
 
64
70
  def extract_file_details(attributes)