ribose 0.1.0 → 0.2.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.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +539 -970
  4. data/.sample.pryrc +4 -0
  5. data/CHANGELOG.md +5 -0
  6. data/README.md +539 -3
  7. data/bin/console +2 -5
  8. data/lib/ribose.rb +31 -1
  9. data/lib/ribose/actions.rb +10 -0
  10. data/lib/ribose/actions/all.rb +43 -0
  11. data/lib/ribose/actions/base.rb +11 -0
  12. data/lib/ribose/actions/create.rb +44 -0
  13. data/lib/ribose/actions/delete.rb +32 -0
  14. data/lib/ribose/actions/fetch.rb +44 -0
  15. data/lib/ribose/actions/update.rb +35 -0
  16. data/lib/ribose/app_data.rb +15 -0
  17. data/lib/ribose/app_relation.rb +12 -0
  18. data/lib/ribose/base.rb +48 -0
  19. data/lib/ribose/calendar.rb +22 -0
  20. data/lib/ribose/client.rb +31 -0
  21. data/lib/ribose/config.rb +21 -0
  22. data/lib/ribose/configuration.rb +25 -0
  23. data/lib/ribose/connection.rb +39 -0
  24. data/lib/ribose/connection_invitation.rb +51 -0
  25. data/lib/ribose/conversation.rb +87 -0
  26. data/lib/ribose/error.rb +30 -0
  27. data/lib/ribose/feed.rb +11 -0
  28. data/lib/ribose/file_uploader.rb +93 -0
  29. data/lib/ribose/join_space_request.rb +41 -0
  30. data/lib/ribose/leaderboard.rb +19 -0
  31. data/lib/ribose/member.rb +34 -0
  32. data/lib/ribose/message.rb +94 -0
  33. data/lib/ribose/profile.rb +56 -0
  34. data/lib/ribose/request.rb +122 -0
  35. data/lib/ribose/resource_helper.rb +81 -0
  36. data/lib/ribose/response/raise_error.rb +15 -0
  37. data/lib/ribose/rspec.rb +19 -0
  38. data/lib/ribose/session.rb +48 -0
  39. data/lib/ribose/setting.rb +16 -0
  40. data/lib/ribose/space.rb +30 -0
  41. data/lib/ribose/space_file.rb +49 -0
  42. data/lib/ribose/space_invitation.rb +70 -0
  43. data/lib/ribose/stream.rb +15 -0
  44. data/lib/ribose/user.rb +42 -0
  45. data/lib/ribose/version.rb +1 -1
  46. data/lib/ribose/widget.rb +11 -0
  47. data/lib/ribose/wiki.rb +80 -0
  48. data/ribose.gemspec +6 -0
  49. data/spec/fixtures/app_data.json +188 -0
  50. data/spec/fixtures/app_relation.json +19 -0
  51. data/spec/fixtures/app_relations.json +89 -0
  52. data/spec/fixtures/calendar.json +12 -0
  53. data/spec/fixtures/calendars.json +34 -0
  54. data/spec/fixtures/connection_invitation.json +26 -0
  55. data/spec/fixtures/connection_invitation_accepted.json +26 -0
  56. data/spec/fixtures/connection_invitations.json +28 -0
  57. data/spec/fixtures/connection_invitations_created.json +37 -0
  58. data/spec/fixtures/connection_suggestion.json +22 -0
  59. data/spec/fixtures/connections.json +28 -0
  60. data/spec/fixtures/conversation.json +26 -0
  61. data/spec/fixtures/conversation_created.json +31 -0
  62. data/spec/fixtures/conversations.json +34 -0
  63. data/spec/fixtures/empty.json +1 -0
  64. data/spec/fixtures/feeds.json +84 -0
  65. data/spec/fixtures/file_upload_prepared.json +17 -0
  66. data/spec/fixtures/file_uploaded.json +38 -0
  67. data/spec/fixtures/general_information.json +16 -0
  68. data/spec/fixtures/join_space_request_created.json +26 -0
  69. data/spec/fixtures/join_space_request_updated.json +25 -0
  70. data/spec/fixtures/join_space_requests.json +34 -0
  71. data/spec/fixtures/leaderboard.json +21 -0
  72. data/spec/fixtures/login.html +323 -0
  73. data/spec/fixtures/members.json +20 -0
  74. data/spec/fixtures/message.json +22 -0
  75. data/spec/fixtures/messages.json +24 -0
  76. data/spec/fixtures/ping.json +3 -0
  77. data/spec/fixtures/profile.json +10 -0
  78. data/spec/fixtures/sample.png +0 -0
  79. data/spec/fixtures/setting.json +16 -0
  80. data/spec/fixtures/settings.json +18 -0
  81. data/spec/fixtures/space.json +59 -0
  82. data/spec/fixtures/space_created.json +59 -0
  83. data/spec/fixtures/space_file.json +58 -0
  84. data/spec/fixtures/space_invitation.json +32 -0
  85. data/spec/fixtures/space_invitation_updated.json +35 -0
  86. data/spec/fixtures/space_invitations.json +34 -0
  87. data/spec/fixtures/space_mass_invitations.json +46 -0
  88. data/spec/fixtures/spaces.json +61 -0
  89. data/spec/fixtures/stream.json +176 -0
  90. data/spec/fixtures/user_activated.json +6 -0
  91. data/spec/fixtures/widgets.json +50 -0
  92. data/spec/fixtures/wiki.json +79 -0
  93. data/spec/fixtures/wikis.json +197 -0
  94. data/spec/ribose/actions/create_spec.rb +40 -0
  95. data/spec/ribose/actions/delete_spec.rb +24 -0
  96. data/spec/ribose/actions/fetch_spec.rb +32 -0
  97. data/spec/ribose/actions/update_spec.rb +36 -0
  98. data/spec/ribose/app_data_spec.rb +15 -0
  99. data/spec/ribose/app_relation_spec.rb +27 -0
  100. data/spec/ribose/calendar_spec.rb +50 -0
  101. data/spec/ribose/client_spec.rb +48 -0
  102. data/spec/ribose/config_spec.rb +41 -0
  103. data/spec/ribose/connection_invitation_spec.rb +79 -0
  104. data/spec/ribose/connection_spec.rb +26 -0
  105. data/spec/ribose/conversation_spec.rb +84 -0
  106. data/spec/ribose/error_spec.rb +67 -0
  107. data/spec/ribose/feed_spec.rb +14 -0
  108. data/spec/ribose/file_uploader_spec.rb +30 -0
  109. data/spec/ribose/join_space_request_spec.rb +70 -0
  110. data/spec/ribose/leaderboard_spec.rb +13 -0
  111. data/spec/ribose/member_spec.rb +16 -0
  112. data/spec/ribose/message_spec.rb +69 -0
  113. data/spec/ribose/profile_spec.rb +40 -0
  114. data/spec/ribose/request_spec.rb +66 -0
  115. data/spec/ribose/resource_helper_spec.rb +58 -0
  116. data/spec/ribose/session_spec.rb +46 -0
  117. data/spec/ribose/setting_spec.rb +40 -0
  118. data/spec/ribose/space_file_spec.rb +41 -0
  119. data/spec/ribose/space_invitation_spec.rb +117 -0
  120. data/spec/ribose/space_spec.rb +70 -0
  121. data/spec/ribose/stream_spec.rb +14 -0
  122. data/spec/ribose/user_spec.rb +48 -0
  123. data/spec/ribose/widget_spec.rb +14 -0
  124. data/spec/ribose/wiki_spec.rb +67 -0
  125. data/spec/spec_helper.rb +11 -0
  126. data/spec/support/fake_ribose_api.rb +431 -0
  127. data/spec/support/file_upload_stub.rb +76 -0
  128. metadata +190 -3
  129. data/spec/ribose_spec.rb +0 -4
@@ -0,0 +1,56 @@
1
+ module Ribose
2
+ class Profile < Ribose::Base
3
+ include Ribose::Actions::Fetch
4
+ include Ribose::Actions::Update
5
+
6
+ def set_login
7
+ update_login_name[resource_key]
8
+ end
9
+
10
+ # Fetch user profile
11
+ #
12
+ # @param options [Hash] The query parameters
13
+ # @return [Sawyer::Resource] The user profile
14
+ #
15
+ def self.fetch(options = {})
16
+ new(resource_id: nil, **options).fetch
17
+ end
18
+
19
+ # Update user profile
20
+ #
21
+ # @param attributes [Hash] The new attributes
22
+ # @return [Sawyer::Resource] The user profile
23
+ #
24
+ def self.update(attributes)
25
+ new(resource_id: nil, **attributes).update
26
+ end
27
+
28
+ # Set login name
29
+ #
30
+ # @param login [String] The user login name
31
+ # @return [Sawyer::Resource] The user profile
32
+ #
33
+ def self.set_login(name, options = {})
34
+ new(login: name, **options).set_login
35
+ end
36
+
37
+ private
38
+
39
+ def resource
40
+ "user"
41
+ end
42
+
43
+ def resources_path
44
+ "people/profile"
45
+ end
46
+
47
+ def update_login_name
48
+ Ribose::Request.put(
49
+ "people/users/#{fetch.user_id}",
50
+ custom_option.merge(
51
+ resource_key.to_sym => { login: attributes[:login] }
52
+ ),
53
+ )
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,122 @@
1
+ module Ribose
2
+ class Request
3
+ # Initialize a Request
4
+ #
5
+ # @param http_method [Symbol] HTTP verb as sysmbol
6
+ # @param endpoint [String] The relative API endpoint
7
+ # @param data [Hash] Attributes / Options as a Hash
8
+ # @return [Ribose::Request]
9
+ #
10
+ def initialize(http_method, endpoint, **data)
11
+ @data = data
12
+ @endpoint = endpoint
13
+ @http_method = http_method
14
+ @client = find_suitable_client
15
+ end
16
+
17
+ # Make a HTTP Request
18
+ #
19
+ # @param options [Hash] Additonal options hash
20
+ # @return [Sawyer::Resource]
21
+ #
22
+ def request(options = {})
23
+ options[:query] = extract_config_option(:query) || {}
24
+ agent.call(http_method, api_endpoint, data, options).data
25
+ end
26
+
27
+ # Make a HTTP GET Request
28
+ #
29
+ # @param endpoint [String] The relative API endpoint
30
+ # @param options [Hash] The additional query params
31
+ # @return [Sawyer::Resource]
32
+ #
33
+ def self.get(endpoint, options = {})
34
+ new(:get, endpoint, options).request
35
+ end
36
+
37
+ # Make a HTTP POST Request
38
+ #
39
+ # @param endpoint [String] The relative API endpoint
40
+ # @param data [Hash] The request data as a Hash
41
+ # @return [Sawyer::Resource]
42
+ #
43
+ def self.post(endpoint, data)
44
+ new(:post, endpoint, data).request
45
+ end
46
+
47
+ # Make a HTTP PUT Request
48
+ #
49
+ # @param endpoint [String] The relative API endpoint
50
+ # @param data [Hash] The request data as a Hash
51
+ # @return [Sawyer::Resource]
52
+ #
53
+ def self.put(endpoint, data)
54
+ new(:put, endpoint, data).request
55
+ end
56
+
57
+ # Make a HTTP DELETE Request
58
+ #
59
+ # @param endpoint [String] The relative API endpoint
60
+ # @return [Sawyer::Resource]
61
+ #
62
+ def self.delete(endpoint, options = {})
63
+ new(:delete, endpoint, options).request
64
+ end
65
+
66
+ private
67
+
68
+ attr_reader :client, :data, :http_method
69
+
70
+ def ribose_host
71
+ Ribose.configuration.api_host
72
+ end
73
+
74
+ def extract_config_option(key)
75
+ if data.is_a?(Hash)
76
+ data.delete(key.to_sym)
77
+ end
78
+ end
79
+
80
+ def find_suitable_client
81
+ client = extract_config_option(:client) || Ribose::Client.new
82
+ client.is_a?(Ribose::Client) ? client: raise(Ribose::Unauthorized)
83
+ end
84
+
85
+ def require_auth_headers?
86
+ auth_header = extract_config_option(:auth_header)
87
+ auth_header == false ? false : true
88
+ end
89
+
90
+ def api_endpoint
91
+ URI::HTTPS.build(
92
+ host: ribose_host,
93
+ path: ["", @endpoint].join("/").squeeze("/"),
94
+ )
95
+ end
96
+
97
+ def sawyer_options
98
+ {
99
+ links_parser: Sawyer::LinkParsers::Simple.new,
100
+ faraday: Faraday.new(builder: custom_rack_builder),
101
+ }
102
+ end
103
+
104
+ def custom_rack_builder
105
+ Faraday::RackBuilder.new do |builder|
106
+ Ribose.configuration.add_default_middleware(builder)
107
+ end
108
+ end
109
+
110
+ def agent
111
+ @agent ||= Sawyer::Agent.new(ribose_host, sawyer_options) do |http|
112
+ http.headers[:accept] = "application/json"
113
+ http.headers[:content_type] = "application/json"
114
+
115
+ if require_auth_headers?
116
+ http.headers["X-Indigo-Token"] = client.api_token
117
+ http.headers["X-Indigo-Email"] = client.user_email
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,81 @@
1
+ module Ribose
2
+ module ResourceHelper
3
+ # Resource
4
+ #
5
+ # This is the key interface for any resource and every single
6
+ # resource should implement this method.Ideally this should
7
+ # reflect resource name in singular form, eg: +space+.
8
+ #
9
+ # Once we've this method implemented then this module will try
10
+ # to auto-generate all of the related helper methods, but if
11
+ # we need something different that the standard format then we
12
+ # can always overirde that in those classes.
13
+ #
14
+ # @return resrouce [String] The singular form of the Resource
15
+ def resource
16
+ raise NotImplementedError
17
+ end
18
+
19
+ # Resource Id
20
+ #
21
+ # The id for the key component of any of the classes in
22
+ # Ribose module, ideally we will have that one as default
23
+ # attribtue to the base class, but we can override it if
24
+ # necessary.
25
+ #
26
+ # @return resource_id [String] The id/uuid for a Resource
27
+ def resource_id
28
+ raise NotImplementedError
29
+ end
30
+
31
+ # Resources
32
+ #
33
+ # The plural version of the resoruce name, ideally we will
34
+ # use this one to to identify an array or resources.
35
+ #
36
+ # @return resources [String] The plural form of the Resource
37
+ def resources
38
+ [resource, "s"].join
39
+ end
40
+
41
+ # Resource Key
42
+ #
43
+ # The resource key is the key that we use to build any of
44
+ # the post/put request body, and ideally it's the resoruce
45
+ # value, but occassionally it might be diffrent for any of
46
+ # the reason, so this method will keep that on portable.
47
+ #
48
+ # @return resource_key [String] The key to build a request
49
+ def resource_key
50
+ resource
51
+ end
52
+
53
+ # Resources Path
54
+ #
55
+ # This represent a restfull resoruce path, and internally
56
+ # this will be used to build the the api endpoint path for
57
+ # any specifies resources.
58
+ #
59
+ # Based on the Ribose API structure it's pretty similiar
60
+ # to the resources value with some minor exception of the
61
+ # nested resoruces, so we are keeping the +resources+ as
62
+ # default but we can override this when necessary.
63
+ #
64
+ # @return resource_path [String] The endpoit for Resources
65
+ def resources_path
66
+ resources
67
+ end
68
+
69
+ # Resource Path
70
+ #
71
+ # This represent a single resource in a Restfull API. In
72
+ # Ribose API structure, it's `resources/:id`, so we will
73
+ # use that one as default. If we need something different
74
+ # then please override this method.
75
+ #
76
+ # @return resoruce_path [String] The Single Resource path
77
+ def resource_path
78
+ [resources_path, resource_id].join("/")
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,15 @@
1
+ require "ribose/error"
2
+
3
+ module Ribose
4
+ module Response
5
+ class RaiseError < Faraday::Response::Middleware
6
+ private
7
+
8
+ def on_complete(response)
9
+ if error = Ribose::Error.from_response(response)
10
+ raise error
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ # RSpec Test Helpers
2
+ #
3
+ # Actual API requests are slow and expensive and we try not to make
4
+ # actual request when possible. For most of our tests we mock those
5
+ # API call which verifies the endpoint, http verb and headers and
6
+ # based on those it responses with an identical fixture file
7
+ #
8
+ # The main purpose of this file is to allow the user to use our test
9
+ # helpers by simplify adding this file to their application and then
10
+ # use the available helper method when necessary.
11
+ #
12
+ # We do not require this module with the gem by default, but you can
13
+ # do so by adding `require "ribose/rspec"` on top of `spec_helper`
14
+ #
15
+ require File.join(Ribose.root, "spec/support/fake_ribose_api.rb")
16
+
17
+ RSpec.configure do |config|
18
+ config.include Ribose::FakeRiboseApi
19
+ end
@@ -0,0 +1,48 @@
1
+ require "json"
2
+ require "mechanize"
3
+ require "ribose/config"
4
+
5
+ module Ribose
6
+ class Session
7
+ def initialize(username, password)
8
+ @username = username
9
+ @password = password
10
+ end
11
+
12
+ def create
13
+ JSON.parse(authenticate_user)
14
+ rescue NoMethodError, JSON::ParserError
15
+ raise Ribose::Unauthorized
16
+ end
17
+
18
+ def self.create(username:, password:)
19
+ new(username, password).create
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :username, :password
25
+
26
+ def authenticate_user
27
+ page = agent.get(ribose_url_for("login"))
28
+ find_and_submit_the_user_login_form(page)
29
+ agent.get(ribose_url_for(["settings", "general", "info"])).body
30
+ end
31
+
32
+ def find_and_submit_the_user_login_form(page)
33
+ login_form = page.form_with(id: "new_user")
34
+ login_form.field_with(id: "loginEmail").value = username
35
+ login_form.field_with(id: "loginPassword").value = password
36
+
37
+ login_form.submit
38
+ end
39
+
40
+ def agent
41
+ @agent ||= Mechanize.new
42
+ end
43
+
44
+ def ribose_url_for(*endpoint)
45
+ [Ribose.configuration.web_url, *endpoint].join("/")
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,16 @@
1
+ require "ribose/actions"
2
+
3
+ module Ribose
4
+ class Setting < Ribose::Base
5
+ include Ribose::Actions::All
6
+ include Ribose::Actions::Fetch
7
+ include Ribose::Actions::Update
8
+ include Ribose::Actions::Create
9
+
10
+ private
11
+
12
+ def resource
13
+ "setting"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ require "ribose/actions"
2
+
3
+ module Ribose
4
+ class Space < Ribose::Base
5
+ include Ribose::Actions::All
6
+ include Ribose::Actions::Fetch
7
+ include Ribose::Actions::Create
8
+ include Ribose::Actions::Update
9
+
10
+ def self.create(name:, **attributes)
11
+ new(attributes.merge(name: name)).create
12
+ end
13
+
14
+ def self.remove(space_uuid, options = {})
15
+ Ribose::Request.post("spaces/#{space_uuid}/freeze", options)
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :space
21
+
22
+ def resource
23
+ "space"
24
+ end
25
+
26
+ def extract_local_attributes
27
+ @space = attributes.delete(:space)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,49 @@
1
+ require "ribose/file_uploader"
2
+
3
+ module Ribose
4
+ class SpaceFile < Ribose::Base
5
+ include Ribose::Actions::All
6
+
7
+ # List Files for Space
8
+ #
9
+ # This interface retrieves the files for any specific space, and
10
+ # the usages is pretty simple all we need to do, provide the space
11
+ # id and it will return the files as `Sawyer::Resource`
12
+ #
13
+ # @param space_id [String] The spcific space Id
14
+ # @param options [Hash] Query parameters as a Hash
15
+ # @return [Array<Sawyer::Resource>]
16
+ #
17
+ def self.all(space_id, options = {})
18
+ new(space_id: space_id, **options).all
19
+ end
20
+
21
+ # Create a new file upload
22
+ #
23
+ # @param space_id [String] The Space UUID
24
+ # @param file [String] The complete path for the file
25
+ # @param attributes [Hash] The file attributes as Hash
26
+ # @return [Sawyer::Resource] The file upload response.
27
+ #
28
+ def self.create(space_id, file:, **attributes)
29
+ upload = FileUploader.upload(space_id, attributes.merge(file: file))
30
+ upload[:attachment]
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :space_id
36
+
37
+ def resource
38
+ "file"
39
+ end
40
+
41
+ def resources_path
42
+ ["spaces", space_id, "file", "files"].join("/")
43
+ end
44
+
45
+ def extract_local_attributes
46
+ @space_id = attributes.delete(:space_id)
47
+ end
48
+ end
49
+ end