citrix 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -0
  3. data/CHANGELOG.md +5 -0
  4. data/CONTRIBUITING.md +40 -0
  5. data/README.md +106 -1
  6. data/citrix.gemspec +5 -0
  7. data/lib/citrix.rb +1 -0
  8. data/lib/citrix/training.rb +22 -0
  9. data/lib/citrix/training/client.rb +38 -0
  10. data/lib/citrix/training/credentials.rb +27 -0
  11. data/lib/citrix/training/helpers/http_client.rb +33 -0
  12. data/lib/citrix/training/helpers/initializer.rb +13 -0
  13. data/lib/citrix/training/namespace/registrants.rb +107 -0
  14. data/lib/citrix/training/namespace/trainings.rb +91 -0
  15. data/lib/citrix/training/resource/registrant.rb +60 -0
  16. data/lib/citrix/training/resource/training.rb +67 -0
  17. data/lib/citrix/training/resource/training_date.rb +25 -0
  18. data/lib/citrix/training/serializer/registrant.rb +32 -0
  19. data/lib/citrix/training/serializer/training.rb +49 -0
  20. data/lib/citrix/training/serializer/training_date.rb +19 -0
  21. data/lib/citrix/version.rb +1 -1
  22. data/spec/citrix/training/client_spec.rb +22 -0
  23. data/spec/citrix/training/credentials_spec.rb +31 -0
  24. data/spec/citrix/training/helpers/http_client_spec.rb +54 -0
  25. data/spec/citrix/training/helpers/initializer_spec.rb +15 -0
  26. data/spec/citrix/training/namespace/registrants_spec.rb +94 -0
  27. data/spec/citrix/training/namespace/trainings_spec.rb +85 -0
  28. data/spec/citrix/training/resource/registrant_spec.rb +11 -0
  29. data/spec/citrix/training/resource/training_spec.rb +11 -0
  30. data/spec/citrix/training/serializer/registrant_spec.rb +50 -0
  31. data/spec/citrix/training/serializer/training_date_spec.rb +21 -0
  32. data/spec/citrix/training/serializer/training_spec.rb +79 -0
  33. data/spec/fixtures/register.json +5 -0
  34. data/spec/fixtures/registrant.json +10 -0
  35. data/spec/fixtures/registrants.json +31 -0
  36. data/spec/fixtures/training.json +24 -0
  37. data/spec/fixtures/trainings.json +26 -0
  38. data/spec/spec_helper.rb +94 -0
  39. metadata +103 -2
@@ -0,0 +1,91 @@
1
+ module Citrix
2
+ module Training
3
+ module Namespace
4
+ class Trainings
5
+ include Helpers::Initializer
6
+ include Helpers::HttpClient
7
+
8
+ # Set the credentials.
9
+ attr_accessor :credentials
10
+
11
+ # Create a new training.
12
+ #
13
+ # response, training = client.trainings.create({
14
+ # name: 'Ruby on Rails',
15
+ # description: 'Getting started with Ruby on Rails',
16
+ # timezone: 'America/Sao_Paulo',
17
+ # dates: [date],
18
+ # web_registration: false,
19
+ # confirmation_email: false,
20
+ # organizers: [organizer]
21
+ # })
22
+ #
23
+ # if response.ok?
24
+ # else
25
+ # end
26
+ #
27
+ # Options:
28
+ #
29
+ # - `name`: Name of the training. Required.
30
+ # - `description`: Description of the training. Required.
31
+ # - `timezone`: Time zone of the training. Required.
32
+ # - `dates`: Array containing instances of `Citrix::Training::TrainingDate`. Required.
33
+ # - `organizers`: Array containing instances of `Citrix::Training::Organizer`. Optional.
34
+ # - `web_registration`: Disable/Enable web registration. Optional.
35
+ # - `confirmation_email`: Disable/Enable confirmation e-mails. Optional.
36
+ #
37
+ # Endpoint: https://developer.citrixonline.com/api/gototraining-rest-api/apimethod/create-training-0
38
+ #
39
+ def create(attributes)
40
+ training = Resource::Training.new(attributes)
41
+
42
+ url = url_for('organizers', credentials.organizer_key, 'trainings')
43
+ response = http_client.post(url, training.serialize)
44
+
45
+ training.key = json_parser.load(response.body) if response.ok?
46
+
47
+ [response, training]
48
+ end
49
+
50
+ # Retrieve information on all scheduled trainings for a given organizer.
51
+ #
52
+ # The trainings are returned in the order in which they were created.
53
+ # Completed trainings are not included; ongoing trainings with past
54
+ # sessions are included along with the past sessions. If the organizer
55
+ # does not have any scheduled trainings, the response will be empty.
56
+ #
57
+ # Endpoint: https://developer.citrixonline.com/api/gototraining-rest-api/apimethod/get-trainings
58
+ #
59
+ def all
60
+ url = url_for('organizers', credentials.organizer_key, 'trainings')
61
+ response = http_client.get(url)
62
+
63
+ if response.ok?
64
+ trainings = response.json.map do |attrs|
65
+ Resource::Training.new Resource::Training.deserialize(attrs)
66
+ end
67
+ end
68
+
69
+ [response, trainings]
70
+ end
71
+
72
+ # Deletes a scheduled or completed training.
73
+ #
74
+ # For scheduled trainings, it deletes all scheduled sessions of the
75
+ # training. For completed trainings, the sessions remain in the
76
+ # database. No email is sent to organizers or registrants, but when
77
+ # participants attempt to start or join the training, they are directed
78
+ # to a page that states: `Training Not Found: The training you are
79
+ # trying to join is no longer available.`
80
+ #
81
+ # response = client.trainings.remove(training)
82
+ # response.ok? #=> successfully removed
83
+ #
84
+ def remove(training)
85
+ url = url_for('organizers', credentials.organizer_key, 'trainings', training.key)
86
+ http_client.delete(url)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,60 @@
1
+ module Citrix
2
+ module Training
3
+ module Resource
4
+ class Registrant
5
+ include Helpers::Initializer
6
+
7
+ # Set the first name of the user.
8
+ attr_accessor :first_name
9
+
10
+ # Set the last name of the user.
11
+ attr_accessor :last_name
12
+
13
+ # Set the e-mail.
14
+ attr_accessor :email
15
+
16
+ # Set the join url.
17
+ attr_accessor :join_url
18
+
19
+ # Set the confirmation url.
20
+ attr_accessor :confirmation_url
21
+
22
+ # Set the registrant key.
23
+ attr_accessor :key
24
+
25
+ # Set the status.
26
+ attr_accessor :status
27
+
28
+ ATTRIBUTES = %i[
29
+ first_name
30
+ last_name
31
+ email
32
+ ]
33
+
34
+ # Convert `attributes` into parameters that
35
+ # Citrix API can understand.
36
+ def self.serialize(attributes)
37
+ Serializer::Registrant.new(attributes: attributes).serialize
38
+ end
39
+
40
+ # Convert `attributes` into parameters that
41
+ # Citrix::Training::Resource::Registrant can understand.
42
+ def self.deserialize(attributes)
43
+ Serializer::Registrant.new(attributes: attributes).deserialize
44
+ end
45
+
46
+ # Return a hash containing all attributes.
47
+ def attributes
48
+ ATTRIBUTES.each_with_object({}) do |name, buffer|
49
+ buffer[name] = public_send(name)
50
+ end
51
+ end
52
+
53
+ # Serialize the attributes.
54
+ def serialize
55
+ self.class.serialize(attributes)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,67 @@
1
+ module Citrix
2
+ module Training
3
+ module Resource
4
+ class Training
5
+ include Helpers::Initializer
6
+
7
+ # Set the name of the training.
8
+ attr_accessor :name
9
+
10
+ # Set the description of the training.
11
+ attr_accessor :description
12
+
13
+ # Set the timezone.
14
+ attr_accessor :timezone
15
+
16
+ # Set the dates.
17
+ attr_accessor :dates
18
+
19
+ # Set the organizers.
20
+ attr_accessor :organizers
21
+
22
+ # Set confirmation e-mail delivery.
23
+ attr_accessor :confirmation_email
24
+
25
+ # Set web registration.
26
+ attr_accessor :web_registration
27
+
28
+ # Set the training key.
29
+ attr_accessor :key
30
+
31
+ ATTRIBUTES = %i[
32
+ name
33
+ description
34
+ timezone
35
+ web_registration
36
+ confirmation_email
37
+ organizers
38
+ dates
39
+ ]
40
+
41
+ # Convert `attributes` into parameters that
42
+ # Citrix API can understand.
43
+ def self.serialize(attributes)
44
+ Serializer::Training.new(attributes: attributes).serialize
45
+ end
46
+
47
+ # Convert `attributes` into parameters that
48
+ # Citrix::Training::Resource::Training can understand.
49
+ def self.deserialize(attributes)
50
+ Serializer::Training.new(attributes: attributes).deserialize
51
+ end
52
+
53
+ # Return a hash containing all attributes.
54
+ def attributes
55
+ ATTRIBUTES.each_with_object({}) do |name, buffer|
56
+ buffer[name] = public_send(name)
57
+ end
58
+ end
59
+
60
+ # Serialize the attributes.
61
+ def serialize
62
+ self.class.serialize(attributes)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,25 @@
1
+ module Citrix
2
+ module Training
3
+ module Resource
4
+ class TrainingDate
5
+ # The starting datetime.
6
+ attr_reader :starts_at
7
+
8
+ # The ending datetime.
9
+ attr_reader :ends_at
10
+
11
+ def initialize(starts_at, ends_at)
12
+ @starts_at = starts_at
13
+ @ends_at = ends_at
14
+ end
15
+
16
+ def serialize
17
+ Serializer::TrainingDate.new(attributes: {
18
+ starts_at: starts_at,
19
+ ends_at: ends_at
20
+ }).serialize
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ module Citrix
2
+ module Training
3
+ module Serializer
4
+ class Registrant
5
+ include Helpers::Initializer
6
+
7
+ # Set attributes that can be (de)serialized.
8
+ attr_accessor :attributes
9
+
10
+ def serialize
11
+ {
12
+ givenName: attributes[:first_name],
13
+ surname: attributes[:last_name],
14
+ email: attributes[:email]
15
+ }
16
+ end
17
+
18
+ def deserialize
19
+ {
20
+ first_name: attributes['givenName'],
21
+ last_name: attributes['surname'],
22
+ email: attributes['email'],
23
+ join_url: attributes['joinUrl'],
24
+ confirmation_url: attributes['confirmationUrl'],
25
+ key: attributes['registrantKey'],
26
+ status: attributes['status'] ? attributes['status'].downcase : nil
27
+ }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,49 @@
1
+ module Citrix
2
+ module Training
3
+ module Serializer
4
+ class Training
5
+ include Helpers::Initializer
6
+
7
+ # Set attributes that can be (de)serialized.
8
+ attr_accessor :attributes
9
+
10
+ def serialize
11
+ {
12
+ name: attributes[:name],
13
+ description: attributes[:description],
14
+ timeZone: attributes[:timezone],
15
+ times: (attributes[:dates] || []).map(&:serialize),
16
+ organizers: (attributes[:organizers] || []).map(&:key),
17
+ registrationSettings: {
18
+ disableWebRegistration: !attributes.fetch(:web_registration, true),
19
+ disableConfirmationEmail: !attributes.fetch(:confirmation_email, true),
20
+ }
21
+ }
22
+ end
23
+
24
+ def deserialize
25
+ {
26
+ key: attributes['trainingKey'],
27
+ name: attributes['name'],
28
+ description: attributes['description'],
29
+ timezone: attributes['timeZone'],
30
+ dates: deserialize_dates(attributes['times'] || []),
31
+ web_registration: !attributes['registrationSettings']['disableWebRegistration'],
32
+ confirmation_email: !attributes['registrationSettings']['disableConfirmationEmail']
33
+ }
34
+ end
35
+
36
+ private
37
+
38
+ def deserialize_dates(dates)
39
+ dates.map do |date|
40
+ Resource::TrainingDate.new(
41
+ Time.parse(date['startDate']),
42
+ Time.parse(date['endDate'])
43
+ )
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,19 @@
1
+ module Citrix
2
+ module Training
3
+ module Serializer
4
+ class TrainingDate
5
+ include Helpers::Initializer
6
+
7
+ # Set attributes.
8
+ attr_accessor :attributes
9
+
10
+ def serialize
11
+ {
12
+ startDate: attributes[:starts_at].iso8601,
13
+ endDate: attributes[:ends_at].iso8601
14
+ }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Citrix
2
- VERSION = '0.0.0'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Citrix::Training::Client do
4
+ describe '.build' do
5
+ it 'returns client' do
6
+ client = Citrix::Training::Client.build({})
7
+ expect(client).to be_a(Citrix::Training::Client)
8
+ end
9
+
10
+ it 'sets credentials' do
11
+ client = Citrix::Training::Client.build({})
12
+ expect(client.credentials).to be_a(Citrix::Training::Credentials)
13
+ end
14
+ end
15
+
16
+ describe '#trainings' do
17
+ it 'returns namespace' do
18
+ client = Citrix::Training::Client.build({})
19
+ expect(client.trainings).to be_a(Citrix::Training::Namespace::Trainings)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Citrix::Training::Credentials do
4
+ describe '#initialize' do
5
+ subject(:credentials) {
6
+ Citrix::Training::Credentials.new(
7
+ oauth_token: 'OAUTH_TOKEN',
8
+ organizer_key: 'ORGANIZER_KEY',
9
+ account_key: 'ACCOUNT_KEY'
10
+ )
11
+ }
12
+
13
+ it { expect(credentials.oauth_token).to eq('OAUTH_TOKEN') }
14
+ it { expect(credentials.organizer_key).to eq('ORGANIZER_KEY') }
15
+ it { expect(credentials.account_key).to eq('ACCOUNT_KEY') }
16
+ end
17
+
18
+ describe '.build' do
19
+ it 'returns new instance' do
20
+ credentials = Citrix::Training::Credentials.build({})
21
+ expect(credentials).to be_a(Citrix::Training::Credentials)
22
+ end
23
+
24
+ it 'returns credentials' do
25
+ credentials = Citrix::Training::Credentials.new
26
+ new_credentials = Citrix::Training::Credentials.build(credentials)
27
+
28
+ expect(new_credentials).to eq(credentials)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Citrix::Training::Helpers::HttpClient do
4
+ let(:helper) { Object.new.extend(described_class) }
5
+
6
+ describe '#json_parser' do
7
+ it 'returns parser' do
8
+ expect(helper.json_parser).to eq(helper.http_client.configuration.json_parser)
9
+ end
10
+ end
11
+
12
+ describe '#url_for' do
13
+ it 'returns url' do
14
+ url = helper.url_for('trainings', 1234)
15
+ expect(url).to eq(File.join(Citrix::Training::API_ENDPOINT, 'trainings', '1234'))
16
+ end
17
+ end
18
+
19
+ describe '#http_client' do
20
+ let(:config) { helper.http_client.configuration }
21
+
22
+ it 'enabled debug mode' do
23
+ $DEBUG = true
24
+ expect(config.logger).to be_a(Logger)
25
+ end
26
+
27
+ it 'skips debug mode' do
28
+ $DEBUG = false
29
+ expect(config.logger).to be_falsy
30
+ end
31
+
32
+ it 'sets user agent' do
33
+ expect(config.user_agent).to eq("Citrix::Rubygems/#{Citrix::VERSION}")
34
+ end
35
+
36
+ context 'default headers' do
37
+ it 'sets content type' do
38
+ expect(config.default_headers['Content-Type']).to eq('application/json')
39
+ end
40
+
41
+ it 'sets accept' do
42
+ expect(config.default_headers['Accept']).to eq('application/json')
43
+ end
44
+
45
+ it 'sets authorization' do
46
+ credentials = double(oauth_token: 'OAUTH_TOKEN')
47
+ allow(helper).to receive(:credentials).and_return(credentials)
48
+ auth_header = config.default_headers['Authorization'].call
49
+
50
+ expect(auth_header).to eq('OAuth oauth_token=OAUTH_TOKEN')
51
+ end
52
+ end
53
+ end
54
+ end