ims_caliper 0.1.1 → 0.1.2

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 (131) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +37 -0
  3. data/.idea/caliper-ruby.iml +98 -0
  4. data/.idea/encodings.xml +5 -0
  5. data/.idea/misc.xml +5 -0
  6. data/.idea/modules.xml +9 -0
  7. data/.idea/scopes/scope_settings.xml +5 -0
  8. data/.idea/vcs.xml +7 -0
  9. data/Gemfile +12 -1
  10. data/Gemfile.lock +42 -1
  11. data/LICENSE +67 -0
  12. data/NOTICE +15 -0
  13. data/README.md +13 -5
  14. data/caliper.gemspec +2 -8
  15. data/lib/caliper/defaults.rb +4 -4
  16. data/lib/caliper/entities/annotation/annotation.rb +14 -4
  17. data/lib/caliper/entities/annotation/bookmark_annotation.rb +14 -4
  18. data/lib/caliper/entities/annotation/highlight_annotation.rb +14 -3
  19. data/lib/caliper/entities/annotation/shared_annotation.rb +12 -3
  20. data/lib/caliper/entities/annotation/tag_annotation.rb +13 -3
  21. data/lib/caliper/entities/assessment/assessment.rb +27 -0
  22. data/lib/caliper/entities/assessment/assessment_item.rb +26 -0
  23. data/lib/caliper/entities/assignable/assignable_digital_resource.rb +47 -0
  24. data/lib/caliper/entities/assignable/attempt.rb +32 -0
  25. data/lib/caliper/entities/digital_resource.rb +36 -35
  26. data/lib/caliper/entities/entity.rb +50 -36
  27. data/lib/caliper/entities/foaf/agent.rb +1 -1
  28. data/lib/caliper/entities/jsonable.rb +56 -0
  29. data/lib/caliper/entities/learning_objective.rb +26 -0
  30. data/lib/caliper/entities/lis/course_offering.rb +30 -0
  31. data/lib/caliper/entities/lis/course_section.rb +27 -15
  32. data/lib/caliper/entities/lis/group.rb +27 -0
  33. data/lib/caliper/entities/lis/membership.rb +32 -0
  34. data/lib/caliper/entities/lis/organization.rb +20 -17
  35. data/lib/caliper/entities/lis/person.rb +23 -10
  36. data/lib/caliper/entities/lis/roles.rb +77 -0
  37. data/lib/caliper/entities/lis/status.rb +19 -0
  38. data/lib/caliper/entities/media/audio_object.rb +21 -0
  39. data/lib/caliper/entities/media/image_object.rb +21 -0
  40. data/lib/caliper/entities/media/media_location.rb +23 -0
  41. data/lib/caliper/entities/media/media_object.rb +38 -0
  42. data/lib/caliper/entities/media/video_object.rb +21 -0
  43. data/lib/caliper/entities/reading/epub_volume.rb +15 -7
  44. data/lib/caliper/entities/reading/frame.rb +7 -15
  45. data/lib/caliper/entities/response/fillin_blank_response.rb +20 -0
  46. data/lib/caliper/entities/response/multiple_choice_response.rb +20 -0
  47. data/lib/caliper/entities/response/multiple_response_response.rb +20 -0
  48. data/lib/caliper/entities/response/response.rb +23 -0
  49. data/lib/caliper/entities/response/response_type.rb +13 -0
  50. data/lib/caliper/entities/response/select_type_response.rb +20 -0
  51. data/lib/caliper/entities/response/true_false_response.rb +21 -0
  52. data/lib/caliper/entities/schemadotorg/software_application.rb +8 -8
  53. data/lib/caliper/entities/session.rb +25 -17
  54. data/lib/caliper/entities/software_application.rb +16 -7
  55. data/lib/caliper/entities/web_page.rb +2 -0
  56. data/lib/caliper/entity_envelope.rb +21 -0
  57. data/lib/caliper/event/annotation_event.rb +10 -6
  58. data/lib/caliper/event/assessment_event.rb +19 -0
  59. data/lib/caliper/event/assessment_item_event.rb +19 -0
  60. data/lib/caliper/event/assignable_event.rb +19 -0
  61. data/lib/caliper/event/event.rb +70 -50
  62. data/lib/caliper/event/jsonable.rb +55 -0
  63. data/lib/caliper/event/navigation_event.rb +2 -7
  64. data/lib/caliper/event/outcome_event.rb +1 -1
  65. data/lib/caliper/event/session_event.rb +13 -14
  66. data/lib/caliper/event/view_event.rb +6 -7
  67. data/lib/caliper/event_envelope.rb +22 -0
  68. data/lib/caliper/jsonable.rb +53 -0
  69. data/lib/caliper/profiles/annotation_profile.rb +56 -53
  70. data/lib/caliper/profiles/assessment_profile.rb +48 -0
  71. data/lib/caliper/profiles/assignable_profile.rb +44 -0
  72. data/lib/caliper/profiles/media_profile.rb +54 -0
  73. data/lib/caliper/profiles/profile.rb +38 -38
  74. data/lib/caliper/profiles/reading_profile.rb +31 -28
  75. data/lib/caliper/profiles/session_profile.rb +17 -18
  76. data/lib/caliper/request/event_store_requestor.rb +26 -26
  77. data/lib/caliper/sensor.rb +46 -35
  78. data/lib/caliper/version.rb +1 -1
  79. data/spec/lib/entities/entity.json +9 -0
  80. data/spec/lib/entities/entity_spec.rb +37 -0
  81. data/spec/lib/entities/lis/person_spec.rb +37 -0
  82. data/spec/lib/entities/lis/student.json +11 -0
  83. data/spec/lib/event/annotation_event_bookmark_spec.rb +159 -0
  84. data/spec/lib/event/annotation_event_highlight_spec.rb +160 -0
  85. data/spec/lib/event/annotation_event_shared_spec.rb +172 -0
  86. data/spec/lib/event/annotation_event_tag_spec.rb +159 -0
  87. data/spec/lib/event/assessment_event_spec.rb +207 -0
  88. data/spec/lib/event/assessment_item_completed_event_spec.rb +223 -0
  89. data/spec/lib/event/assessment_item_started_event_spec.rb +207 -0
  90. data/spec/lib/event/media_event_spec.rb +157 -0
  91. data/spec/lib/event/navigated_event_spec.rb +160 -0
  92. data/spec/lib/event/session_event_login_spec.rb +160 -0
  93. data/spec/lib/event/session_event_logout_spec.rb +145 -0
  94. data/spec/lib/event/session_event_timeout_spec.rb +145 -0
  95. data/spec/lib/event/viewed_event_spec.rb +149 -0
  96. data/spec/lib/event_helper.rb +145 -0
  97. data/spec/lib/sensor_spec.rb +30 -0
  98. data/spec/spec_helper.rb +3 -2
  99. metadata +84 -61
  100. data/lib/caliper/client.rb +0 -33
  101. data/lib/caliper/consumer/base.rb +0 -46
  102. data/lib/caliper/consumer/queue.rb +0 -40
  103. data/lib/caliper/consumer/socket.rb +0 -132
  104. data/lib/caliper/entities/annotation/text_position_selector.rb +0 -14
  105. data/lib/caliper/entities/schemadotorg/audio_object.rb +0 -15
  106. data/lib/caliper/entities/schemadotorg/image_object.rb +0 -15
  107. data/lib/caliper/entities/schemadotorg/media_object.rb +0 -19
  108. data/lib/caliper/entities/schemadotorg/video_object.rb +0 -15
  109. data/lib/caliper/event/.shared.rb.swp +0 -0
  110. data/spec/lib/client_spec.rb +0 -35
  111. data/spec/lib/consumer/base_spec.rb +0 -28
  112. data/spec/lib/consumer/queue_spec.rb +0 -16
  113. data/spec/lib/event/annotation_spec.rb +0 -12
  114. data/spec/lib/event/bookmarked_spec.rb +0 -17
  115. data/spec/lib/event/commented_spec.rb +0 -19
  116. data/spec/lib/event/highlighted_spec.rb +0 -18
  117. data/spec/lib/event/navigation_spec.rb +0 -15
  118. data/spec/lib/event/shared_spec.rb +0 -18
  119. data/spec/lib/event/tagged_spec.rb +0 -17
  120. data/spec/lib/event/used_spec.rb +0 -15
  121. data/spec/lib/event/viewed_spec.rb +0 -15
  122. data/test/caliper/events/session_login_event_test.rb +0 -58
  123. data/test/caliper/events/session_logout_event_test.rb +0 -55
  124. data/test/caliper/events/session_timeout_event_test.rb +0 -55
  125. data/test/caliper/request/http_requestor_test.rb +0 -59
  126. data/test/caliper/test_event.rb +0 -53
  127. data/test/caliper/test_utils.rb +0 -198
  128. data/test/fixtures/caliper_session_login_event.json +0 -71
  129. data/test/fixtures/caliper_session_logout_event.json +0 -52
  130. data/test/fixtures/caliper_session_timeout_event.json +0 -52
  131. data/test/fixtures/event_store_payload.json +0 -85
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  require 'simplecov'
2
2
  require 'simplecov-rcov'
3
- require "caliper"
3
+ require 'require_all'
4
+ require_all 'lib/caliper/*.rb'
4
5
 
5
6
  SimpleCov.use_merging
6
7
  SimpleCov.merge_timeout(10000)
7
8
  SimpleCov.command_name('open_data_export_gem')
8
9
  SimpleCov.start('test_frameworks') do
9
- SimpleCov.coverage_dir('../../coverage')
10
+ SimpleCov.coverage_dir('./coverage')
10
11
  SimpleCov.at_exit {
11
12
  SimpleCov.result
12
13
  }
metadata CHANGED
@@ -1,77 +1,107 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ims_caliper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
- - Anthus Williams
8
- - Zhen Qian
7
+ - Caliper Dev Group
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2015-02-18 00:00:00.000000000 Z
11
+ date: 2015-04-22 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: This is the implementation of IMSGlobal Caliper in Ruby
15
14
  email:
16
- - aj@instructure.com
17
- - zqian@umich.edu
15
+ - lisa@imsglobal.org
18
16
  executables: []
19
17
  extensions: []
20
18
  extra_rdoc_files: []
21
19
  files:
20
+ - .gitignore
21
+ - .idea/caliper-ruby.iml
22
+ - .idea/encodings.xml
23
+ - .idea/misc.xml
24
+ - .idea/modules.xml
25
+ - .idea/scopes/scope_settings.xml
26
+ - .idea/vcs.xml
22
27
  - Gemfile
23
28
  - Gemfile.lock
29
+ - LICENSE
30
+ - NOTICE
24
31
  - README.md
25
32
  - caliper.gemspec
26
33
  - config/locales/en.yml
27
34
  - lib/caliper.rb
28
- - lib/caliper/client.rb
29
- - lib/caliper/consumer/base.rb
30
- - lib/caliper/consumer/queue.rb
31
- - lib/caliper/consumer/socket.rb
32
35
  - lib/caliper/defaults.rb
33
36
  - lib/caliper/entities/annotation/annotation.rb
34
37
  - lib/caliper/entities/annotation/bookmark_annotation.rb
35
38
  - lib/caliper/entities/annotation/highlight_annotation.rb
36
39
  - lib/caliper/entities/annotation/shared_annotation.rb
37
40
  - lib/caliper/entities/annotation/tag_annotation.rb
38
- - lib/caliper/entities/annotation/text_position_selector.rb
41
+ - lib/caliper/entities/assessment/assessment.rb
42
+ - lib/caliper/entities/assessment/assessment_item.rb
43
+ - lib/caliper/entities/assignable/assignable_digital_resource.rb
44
+ - lib/caliper/entities/assignable/attempt.rb
39
45
  - lib/caliper/entities/digital_resource.rb
40
46
  - lib/caliper/entities/entity.rb
41
47
  - lib/caliper/entities/foaf/agent.rb
42
48
  - lib/caliper/entities/generatable.rb
49
+ - lib/caliper/entities/jsonable.rb
43
50
  - lib/caliper/entities/learning_context.rb
51
+ - lib/caliper/entities/learning_objective.rb
52
+ - lib/caliper/entities/lis/course_offering.rb
44
53
  - lib/caliper/entities/lis/course_section.rb
54
+ - lib/caliper/entities/lis/group.rb
55
+ - lib/caliper/entities/lis/membership.rb
45
56
  - lib/caliper/entities/lis/organization.rb
46
57
  - lib/caliper/entities/lis/person.rb
58
+ - lib/caliper/entities/lis/roles.rb
59
+ - lib/caliper/entities/lis/status.rb
60
+ - lib/caliper/entities/media/audio_object.rb
61
+ - lib/caliper/entities/media/image_object.rb
62
+ - lib/caliper/entities/media/media_location.rb
63
+ - lib/caliper/entities/media/media_object.rb
64
+ - lib/caliper/entities/media/video_object.rb
47
65
  - lib/caliper/entities/outcome/result.rb
48
66
  - lib/caliper/entities/reading/epub_chapter.rb
49
67
  - lib/caliper/entities/reading/epub_part.rb
50
68
  - lib/caliper/entities/reading/epub_sub_chapter.rb
51
69
  - lib/caliper/entities/reading/epub_volume.rb
52
70
  - lib/caliper/entities/reading/frame.rb
53
- - lib/caliper/entities/schemadotorg/audio_object.rb
71
+ - lib/caliper/entities/response/fillin_blank_response.rb
72
+ - lib/caliper/entities/response/multiple_choice_response.rb
73
+ - lib/caliper/entities/response/multiple_response_response.rb
74
+ - lib/caliper/entities/response/response.rb
75
+ - lib/caliper/entities/response/response_type.rb
76
+ - lib/caliper/entities/response/select_type_response.rb
77
+ - lib/caliper/entities/response/true_false_response.rb
54
78
  - lib/caliper/entities/schemadotorg/creative_work.rb
55
- - lib/caliper/entities/schemadotorg/image_object.rb
56
- - lib/caliper/entities/schemadotorg/media_object.rb
57
79
  - lib/caliper/entities/schemadotorg/software_application.rb
58
80
  - lib/caliper/entities/schemadotorg/thing.rb
59
- - lib/caliper/entities/schemadotorg/video_object.rb
60
81
  - lib/caliper/entities/schemadotorg/web_page.rb
61
82
  - lib/caliper/entities/session.rb
62
83
  - lib/caliper/entities/software_application.rb
63
84
  - lib/caliper/entities/targetable.rb
64
85
  - lib/caliper/entities/web_page.rb
65
- - lib/caliper/event/.shared.rb.swp
86
+ - lib/caliper/entity_envelope.rb
66
87
  - lib/caliper/event/annotation_event.rb
88
+ - lib/caliper/event/assessment_event.rb
89
+ - lib/caliper/event/assessment_item_event.rb
90
+ - lib/caliper/event/assignable_event.rb
67
91
  - lib/caliper/event/event.rb
92
+ - lib/caliper/event/jsonable.rb
68
93
  - lib/caliper/event/media_event.rb
69
94
  - lib/caliper/event/navigation_event.rb
70
95
  - lib/caliper/event/outcome_event.rb
71
96
  - lib/caliper/event/session_event.rb
72
97
  - lib/caliper/event/view_event.rb
98
+ - lib/caliper/event_envelope.rb
99
+ - lib/caliper/jsonable.rb
73
100
  - lib/caliper/options.rb
74
101
  - lib/caliper/profiles/annotation_profile.rb
102
+ - lib/caliper/profiles/assessment_profile.rb
103
+ - lib/caliper/profiles/assignable_profile.rb
104
+ - lib/caliper/profiles/media_profile.rb
75
105
  - lib/caliper/profiles/profile.rb
76
106
  - lib/caliper/profiles/reading_profile.rb
77
107
  - lib/caliper/profiles/session_profile.rb
@@ -91,29 +121,26 @@ files:
91
121
  - lib/caliper/validators/session_logout_event_validator.rb
92
122
  - lib/caliper/validators/time_check.rb
93
123
  - lib/caliper/version.rb
94
- - spec/lib/client_spec.rb
95
- - spec/lib/consumer/base_spec.rb
96
- - spec/lib/consumer/queue_spec.rb
97
- - spec/lib/event/annotation_spec.rb
98
- - spec/lib/event/bookmarked_spec.rb
99
- - spec/lib/event/commented_spec.rb
100
- - spec/lib/event/highlighted_spec.rb
101
- - spec/lib/event/navigation_spec.rb
102
- - spec/lib/event/shared_spec.rb
103
- - spec/lib/event/tagged_spec.rb
104
- - spec/lib/event/used_spec.rb
105
- - spec/lib/event/viewed_spec.rb
124
+ - spec/lib/entities/entity.json
125
+ - spec/lib/entities/entity_spec.rb
126
+ - spec/lib/entities/lis/person_spec.rb
127
+ - spec/lib/entities/lis/student.json
128
+ - spec/lib/event/annotation_event_bookmark_spec.rb
129
+ - spec/lib/event/annotation_event_highlight_spec.rb
130
+ - spec/lib/event/annotation_event_shared_spec.rb
131
+ - spec/lib/event/annotation_event_tag_spec.rb
132
+ - spec/lib/event/assessment_event_spec.rb
133
+ - spec/lib/event/assessment_item_completed_event_spec.rb
134
+ - spec/lib/event/assessment_item_started_event_spec.rb
135
+ - spec/lib/event/media_event_spec.rb
136
+ - spec/lib/event/navigated_event_spec.rb
137
+ - spec/lib/event/session_event_login_spec.rb
138
+ - spec/lib/event/session_event_logout_spec.rb
139
+ - spec/lib/event/session_event_timeout_spec.rb
140
+ - spec/lib/event/viewed_event_spec.rb
141
+ - spec/lib/event_helper.rb
142
+ - spec/lib/sensor_spec.rb
106
143
  - spec/spec_helper.rb
107
- - test/caliper/events/session_login_event_test.rb
108
- - test/caliper/events/session_logout_event_test.rb
109
- - test/caliper/events/session_timeout_event_test.rb
110
- - test/caliper/request/http_requestor_test.rb
111
- - test/caliper/test_event.rb
112
- - test/caliper/test_utils.rb
113
- - test/fixtures/caliper_session_login_event.json
114
- - test/fixtures/caliper_session_logout_event.json
115
- - test/fixtures/caliper_session_timeout_event.json
116
- - test/fixtures/event_store_payload.json
117
144
  homepage: https://github.com/IMSGlobal/caliper-ruby
118
145
  licenses:
119
146
  - http://www.imsglobal.org/copyright.html
@@ -139,27 +166,23 @@ signing_key:
139
166
  specification_version: 4
140
167
  summary: Caliper Sensor API
141
168
  test_files:
142
- - spec/lib/client_spec.rb
143
- - spec/lib/consumer/base_spec.rb
144
- - spec/lib/consumer/queue_spec.rb
145
- - spec/lib/event/annotation_spec.rb
146
- - spec/lib/event/bookmarked_spec.rb
147
- - spec/lib/event/commented_spec.rb
148
- - spec/lib/event/highlighted_spec.rb
149
- - spec/lib/event/navigation_spec.rb
150
- - spec/lib/event/shared_spec.rb
151
- - spec/lib/event/tagged_spec.rb
152
- - spec/lib/event/used_spec.rb
153
- - spec/lib/event/viewed_spec.rb
169
+ - spec/lib/entities/entity.json
170
+ - spec/lib/entities/entity_spec.rb
171
+ - spec/lib/entities/lis/person_spec.rb
172
+ - spec/lib/entities/lis/student.json
173
+ - spec/lib/event/annotation_event_bookmark_spec.rb
174
+ - spec/lib/event/annotation_event_highlight_spec.rb
175
+ - spec/lib/event/annotation_event_shared_spec.rb
176
+ - spec/lib/event/annotation_event_tag_spec.rb
177
+ - spec/lib/event/assessment_event_spec.rb
178
+ - spec/lib/event/assessment_item_completed_event_spec.rb
179
+ - spec/lib/event/assessment_item_started_event_spec.rb
180
+ - spec/lib/event/media_event_spec.rb
181
+ - spec/lib/event/navigated_event_spec.rb
182
+ - spec/lib/event/session_event_login_spec.rb
183
+ - spec/lib/event/session_event_logout_spec.rb
184
+ - spec/lib/event/session_event_timeout_spec.rb
185
+ - spec/lib/event/viewed_event_spec.rb
186
+ - spec/lib/event_helper.rb
187
+ - spec/lib/sensor_spec.rb
154
188
  - spec/spec_helper.rb
155
- - test/caliper/events/session_login_event_test.rb
156
- - test/caliper/events/session_logout_event_test.rb
157
- - test/caliper/events/session_timeout_event_test.rb
158
- - test/caliper/request/http_requestor_test.rb
159
- - test/caliper/test_event.rb
160
- - test/caliper/test_utils.rb
161
- - test/fixtures/caliper_session_login_event.json
162
- - test/fixtures/caliper_session_logout_event.json
163
- - test/fixtures/caliper_session_timeout_event.json
164
- - test/fixtures/event_store_payload.json
165
- has_rdoc:
@@ -1,33 +0,0 @@
1
-
2
- module Caliper
3
- class Client
4
- attr_accessor :api_key, :options, :stats
5
- def initialize(key, opts = {})
6
- if opts[:consumer]
7
- opts[:consumer] = Object.const_get(opts[:consumer]) if opts[:consumer].is_a?(String)
8
- opts[:consumer] = opts[:consumer].new(key, opts) if opts[:consumer].respond_to?(:new)
9
- raise ArgumentError, "Expecting Caliper::Consumer but got #{opts[:consumer].class.to_s}" unless opts[:consumer].is_a?(Caliper::Consumer::Base)
10
- end
11
- @consumer = opts[:consumer] || Caliper::Consumer::Socket.new(key, opts)
12
- end
13
-
14
- def measure(event, timestamp=nil)
15
- raise ArgumentError, "Expecting Caliper::Event but got #{event.class.to_s}" unless event.is_a?(Caliper::Event::Base)
16
- #todo ensure timestamp is iso8601
17
- timestamp ||= Time.now
18
- action = event.action
19
- # returned json will serialize nil contexts as []
20
- learning_context = event.learning_context ? event.learning_context : nil
21
- activity_context = event.activity_context ? event.activity_context : nil
22
-
23
- @consumer.measure(action, learning_context, activity_context, timestamp)
24
- end
25
-
26
- def describe(entity, timestamp=nil)
27
- raise ArgumentError, "Expecting Caliper::Entity but got #{entity.class.to_s}" unless entity.is_a?(Caliper::Entity::Base)
28
- #todo ensure timestamp is iso8601
29
- timestamp ||= Time.now
30
- @consumer.describe(entity.type, entity.id, entity.properties, Time.now)
31
- end
32
- end
33
- end
@@ -1,46 +0,0 @@
1
- module Caliper
2
- module Consumer
3
- class Base
4
- attr_reader :api_key
5
-
6
- def initialize(key, opts = {})
7
- @api_key = key
8
- @options = validate_options(opts)
9
- end
10
-
11
- def describe(type, id, props, timestamp); end
12
-
13
- def measure(action, learning_context, activity_context, timestamp); end
14
-
15
- def with_debug?
16
- @options[:debug] || false
17
- end
18
-
19
- def with_ssl?
20
- @options[:ssl] || false
21
- end
22
-
23
- def on_error(error)
24
- if with_debug?
25
- #todo: log if debug
26
- end
27
-
28
- if @options[:on_error]
29
- @options[:on_error].call(error)
30
- end
31
- end
32
-
33
- def validate_options(opts)
34
- hsh = {}
35
- hsh[:debug] = opts[:debug] ? true : false
36
- hsh[:ssl] = opts[:ssl] ? true : false
37
- if opts[:on_error]
38
- raise ArgumentError, "on_error option should be callable" unless opts[:on_error].is_a?(Proc)
39
- hsh[:on_error] = opts[:on_error]
40
- end
41
- hsh
42
- end
43
-
44
- end
45
- end
46
- end
@@ -1,40 +0,0 @@
1
- module Caliper
2
- module Consumer
3
- class Queue < Base
4
- attr_reader :max_queue_size, :batch_size
5
-
6
- def initialize(key, opts = {})
7
- super
8
- @queue = []
9
- end
10
-
11
- def validate_options(opts)
12
- hsh = super
13
- @max_queue_size = hsh[:max_queue_size] = opts[:max_queue_size].to_i
14
- @batch_size = hsh[:batch_size] = opts[:batch_size].to_i
15
- hsh
16
- end
17
-
18
- def measure(action, learning_context, activity_context, timestamp)
19
- enqueue OpenStruct.new(api_key: @api_key, action: action, learning_context: learning_context, activity_context: activity_context, timestamp: timestamp, proc_name: "measure")
20
- end
21
-
22
- def describe(type, id, properties, timestamp)
23
- enqueue OpenStruct.new(api_key: @api_key, entity_type: type, entity_id: id, properties: properties, timestamp: timestamp, proc_name: "describe")
24
- end
25
-
26
- def enqueue(item)
27
- if item.proc_name == "measure"
28
- flush_measure(item)
29
- elsif item.proc_name == "describe"
30
- flush_describe(item)
31
- end
32
- end
33
-
34
- def flush_measure(item); end
35
-
36
- def flush_describe(item); end
37
-
38
- end
39
- end
40
- end
@@ -1,132 +0,0 @@
1
- require 'socket'
2
- require 'openssl'
3
- require "active_support"
4
-
5
- module Caliper
6
- module Consumer
7
- class Socket < Queue
8
- attr_reader :socket
9
-
10
- def initialize(key, opts = {})
11
- super
12
- end
13
-
14
- def validate_options(opts)
15
- hsh = super(opts)
16
- hsh[:timeout] = opts[:timeout] || 0.5
17
- hsh[:host] = opts[:host] || "localhost"
18
- hsh[:port] = opts[:port] || ( hsh[:ssl] ? 443 : 80 )
19
- hsh[:max_response_size] = opts[:max_response_size] || 2048
20
- hsh
21
- end
22
-
23
- private
24
-
25
- def flush_describe(item)
26
- socket = init_socket
27
- return unless socket
28
-
29
- send_request( socket, describe_body( @options[:host], ActiveSupport::JSON.encode(item)) )
30
- end
31
-
32
- def flush_measure(item)
33
- socket = init_socket
34
- return unless socket
35
-
36
- send_request( socket, measure_body( @options[:host], ActiveSupport::JSON.encode(item)) )
37
- end
38
-
39
- # both describe_body and measure_body hardcode a URL as is the case with caliper-php
40
- # todo: make this configurable
41
- def describe_body(host, content)
42
- <<-STR.gsub(/^ {10}/,'')
43
- PUT /v1/describe HTTP/1.1/
44
- Host: #{host}
45
- Content-Type: application/json
46
- Accept: application\json
47
- Content-length: #{content.length}
48
-
49
- #{content}
50
- STR
51
- end
52
-
53
- def measure_body(host, content)
54
- <<-STR.gsub(/^ {10}/,'')
55
- PUT /v1/learningevent HTTP/1.1/
56
- Host: #{host}
57
- Content-Type: application/json
58
- Accept: application\json
59
- Content-length: #{content.length}
60
-
61
- #{content}
62
- STR
63
- end
64
-
65
- def init_socket
66
- # if socket has failed, don't keep trying to connect on future events
67
- return if @socket_failed
68
- begin
69
- s = TCPSocket.new(@options[:host], @options[:port])
70
- if with_ssl?
71
- s = OpenSSL::SSL::SSLSocket.new(s)
72
- s.sync_close
73
- s.connect
74
- end
75
- rescue SocketError => e
76
- on_error(e)
77
- @socket_failed = true
78
- return
79
- end
80
- s
81
- end
82
-
83
- def send_request(socket, body, with_retries = 1)
84
- written = 0
85
- closed = false
86
- while !closed && bytes_written < body.bytesize
87
- begin
88
- bytes_written = socket.write(body[bytes_written..-1])
89
- if bytes_written.zero?
90
- closed = true
91
- else
92
- written += bytes_written
93
- end
94
- rescue SocketError => e
95
- on_error(e)
96
- closed = true
97
- end
98
- end
99
-
100
- if closed
101
- socket.close
102
- # attempt to retry connection up to with_retries times
103
- unless with_retries.zero?
104
- socket = init_socket
105
- return send_request(socket, body, with_retries - 1) if socket
106
- end
107
- return false
108
- end
109
-
110
- success = true
111
-
112
- # if debug is enabled, wait for response
113
- if with_debug?
114
- r = parse_response(socket.read(@options[:max_response_size]))
115
- unless r.status == 200
116
- #todo build exception and send it to on_error
117
- success = false
118
- end
119
- end
120
-
121
- return success
122
- end
123
-
124
- def parse_response(resp)
125
- contents = resp.split("\n")
126
- status = contents.first.split(" ")
127
- OpenStruct.new(:status => (status.length > 1 ? status[1] : nil), :message => contents.last)
128
- end
129
-
130
- end
131
- end
132
- end