movida_events 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 109b65c8a3086ee629865c0cb075fca0e24e2d9a231349b0710a44e1b5d3c0e6
4
+ data.tar.gz: 00ae815c08941d9b1ce1029e154142fdd984f030995b07822847d211aa20a285
5
+ SHA512:
6
+ metadata.gz: af2b20d8b029cb46a42b67d84aea3ba490068275c54d0b94df874edc32f103c24c07f75a47666e1a9ddea856a92b85b7b118647bd81f6e60c59e49ec11747ffd
7
+ data.tar.gz: 6cb158eab1733d19697ea0049cadc2f83321d64d8d927edf41255f94d6991fa2d71b2c6d6ecd460908b7a8ece869799f0d81b1facde1388b184ea8ce6a35f87b
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /Gemfile.lock
3
+ /vendor/
4
+ /coverage/
5
+ /doc/
6
+ /.yardoc/
7
+ .byebug_history
8
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,22 @@
1
+ ---
2
+ AllCops:
3
+ TargetRubyVersion: 2.3
4
+
5
+ Layout/AlignParameters:
6
+ EnforcedStyle: with_fixed_indentation
7
+
8
+ Layout/IndentArray:
9
+ EnforcedStyle: consistent
10
+
11
+ Layout/IndentHash:
12
+ EnforcedStyle: consistent
13
+
14
+ Layout/MultilineMethodCallIndentation:
15
+ EnforcedStyle: indented
16
+
17
+ Style/Documentation:
18
+ Enabled: false
19
+
20
+ Style/FrozenStringLiteralComment:
21
+ Enabled: true
22
+ EnforcedStyle: always
@@ -0,0 +1,37 @@
1
+ ---
2
+ language: ruby
3
+ rvm:
4
+ - 2.3
5
+ - 2.4
6
+ - 2.5
7
+ env:
8
+ global:
9
+ - COVERAGE=1
10
+ - secure: 'tpNa814wfnNKXEAN/mP70aPBkC0huXl3vPTjJh8QXBFG4Lf45TgLByv2YYM0fyP0ehILEyPcKUHb7KATUt/Sv+4HmQqzzlKxENTkvIi0kfIKoqbGqfUp5djfHNGpuuoaX1b861gXs7gGMKm902JmX53m5LfaMjZSLNWHOT/ATjfmOqMThxk82zaie/Ro27dZAAHfie2iyikesnNAY19GEJjiDvxuc2q3k2nOt5QVmzxQl1xK6SioiUhxyqoKQb0Jh5SHH+mKeHgU5jeHLY42IcW+iDajHG8yCM5BL2TWbB+d892vvN041fcKJ+vlyIHJoMVqkZ7J096s/xq+nWx+LsP/K6LzYiyfydVwla1HtALrz7FBMwGbj5futBfnnM0T9ZBPG7qthlZngg1s5irSQ8g0VuS8EHZp5h8Fwsksey0EH/x/RhfS4t4e3kviQIh5UZMac5jsNSNWpg8spOBrB0y/mZQtl++SL9ArzUqNogpVIqs03eLRzlacQWVlDWVmcMjtX0O2K3v00vOhkmKNyNXjMeWG7EED9bklPemOLNYifdfz4iCNonhoMLkUg9n6r3dWs9XRqM54Jk4vdshty2Gp3GAHHFnUuLoL/cnTJq90DnCHJa0qPs2KuW/I2j+INLqw6UOir/T1Oh8lMkD6EVBqY4Nj85pkufOmNT4Lv+Q='
11
+
12
+ before_script:
13
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
14
+ - chmod +x ./cc-test-reporter
15
+ - ./cc-test-reporter before-build
16
+
17
+ script:
18
+ - bin/rubocop
19
+ - bin/rspec --format doc
20
+
21
+ after_script:
22
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
23
+
24
+ jobs:
25
+ include:
26
+ - stage: release
27
+ rvm: 2.3
28
+ script: skip
29
+ deploy:
30
+ provider: rubygems
31
+ api_key:
32
+ secure: 'tu+TzDHhiKVy5EeiMb8NVb/EHSflqhAhie6OFJc72nBBFAF9mow71sFpOzleQJ0gNdKnrWf17AQZLO9UQLIY2gP6fOHrLfDO5yLBMEpMjoqZISSBtugNUNCf8vg37QWbG6mAzMM67I/dMqT7U9KgL8CwaUwrs75W4KmIoaYqzkjiLc++k9j9dKpdbBBCEp0KNNbuAfEc9YWoas45LWFR1VfqB0CaGX4z8kWZai+N+8JLsmEO2bK6bPp2wtE+5ofXdkr5vSZK9Sp/7vLmh7P/D6ZDvuaF8KPt3wuPdtS1ZebuGWLsExcTTIgXzWBcYQ3hSHpvOwLWhI0M8ZpxmO3PPZO8+LsAzBijCj1HyOaXIrIybebuapvRixjWEV6mk3UXpBWgS3u0dslfbYa9hzbKCtAN/MTDSwLkdSuxsTuonNd406QQvX9O+yNT5+N8QpBqlAONeurSZhIRioiIyAwu6fL1CIJ5muDPaqG/V+dCh+z3W5KqnKgr9dY/bN8XizQoZhEFPg8WUjG2/ZRwtw3u5BD1rCbJIt3tAUSwhLLoSQFkH1f07evOuArOzaJjmehdaOiERkzsVmip65F/Bt+03NYShNzw3qK416ZjoxvVXpgb+mHVB8TEHL11bmvnrJ/v//fRp2W0ClHDZBmHvuem3UIXb7vnDDtvn8HClNHlgkg='
33
+ gem: movida_events
34
+ on:
35
+ tags: true
36
+ condition: bin/check-version
37
+ repo: machinima/movida_events
@@ -0,0 +1,4 @@
1
+ --markup markdown
2
+ lib/**/*.rb
3
+ -
4
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+ gemspec
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2018 Warner Bros. Entertainment Inc.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,181 @@
1
+ # MovidaEvents
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/movida_events.svg)](https://badge.fury.io/rb/movida_events)
4
+ [![Build Status](https://travis-ci.org/machinima/movida_events.svg?branch=master)](https://travis-ci.org/machinima/movida_events)
5
+ [![Code Climate](https://codeclimate.com/github/machinima/movida_events/badges/gpa.svg)](https://codeclimate.com/github/machinima/movida_events)
6
+ [![Test Coverage](https://codeclimate.com/github/machinima/movida_events/badges/coverage.svg)](https://codeclimate.com/github/machinima/movida_events)
7
+ [![Inline docs](http://inch-ci.org/github/machinima/movida_events.svg?branch=master)](http://inch-ci.org/github/machinima/movida_events)
8
+
9
+ A BeBanjo Movida event stream processor
10
+
11
+ ## Documentation
12
+
13
+ Read below to get started, or see the [API Documentation][api-docs] for more
14
+ details.
15
+
16
+ [api-docs]: https://www.rubydoc.info/github/machinima/movida_events
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'movida_events'
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ require 'movida_events'
30
+
31
+ client = MovidaEvents::Client.new(username: 'my_user', password: 'my_pass')
32
+ poller = MovidaEvents::Poller.new(client)
33
+ poller.poll do |event|
34
+ puts event.inspect
35
+ end
36
+ ```
37
+
38
+ Above, we first create a `Client` object. This serves as the interface between
39
+ the poller and Movida. We then create a `Poller` object with that client. Then
40
+ using the `poll` method, we print each event.
41
+
42
+ By default, the Poller only processes events that occur after the poller has
43
+ started.
44
+
45
+ ### Starting at after a given event
46
+
47
+ Usually, you want to keep track of which event was processed last, so that you
48
+ can ensure that every event gets processed. Use the `newer_than` option to tell
49
+ the poller to process all events after a given ID.
50
+
51
+ ```ruby
52
+ MovidaEvents::Poller.new(client, newer_than: 1234).poll do |event|
53
+ # All events after (but not including) event 1234 will be processed here
54
+ end
55
+ ```
56
+
57
+ ### Filtering event types
58
+
59
+ You can limit the types of events by setting the `event_types` option. See
60
+ the [event type documentation][event-types] for a list of available types.
61
+
62
+ ```ruby
63
+ types = ['title_created', 'title_updated']
64
+ MovidaEvents::Poller.new(client, event_types: types).poll do |event|
65
+ # Only title_created and title_updated events will be processed here
66
+ end
67
+ ```
68
+
69
+ The `event_types` option also accepts a comma separated string like
70
+ `"title_created,title_updated"`.
71
+
72
+ [event-types]: https://github.com/bebanjo/almodovar/wiki/Movida-Events-Feed#event-type-event-type
73
+
74
+ ### The poll interval
75
+
76
+ The poller checks for new events every few seconds. The default poll interval is
77
+ 30 seconds, that can be changed with the `interval` option.
78
+
79
+ Note that even though the poller checks for new events every few seconds, the
80
+ processing block will not get called unless there is a new event.
81
+
82
+ ```ruby
83
+ # Polls every 5 seconds to check for new events
84
+ MovidaEvents::Poller.new(client, interval: 5)
85
+ ```
86
+
87
+ ### Run code for every poll
88
+
89
+ The processing block only gets called if an event is found, but we can use the
90
+ `on_poll` method to set a callback for every time the poller checks for new
91
+ events. This is especially useful for logging. It could be used to broadcast a
92
+ heartbeat, etc.
93
+
94
+
95
+ ```ruby
96
+ poller.on_poll do |stats|
97
+ log("Polled #{stats.requests} times")
98
+ end
99
+ poller.poll
100
+ ```
101
+
102
+ See below for more information about the `stats` object.
103
+
104
+ ### Stats
105
+
106
+ The blocks for `poll` and `on_poll` both accept a stats object. It contains
107
+ information about the current poller status. The available methods are:
108
+
109
+ - last: The ID of the event processed last. In the case of the `poll` method,
110
+ this is the current event ID.
111
+ - requests: The number of requests processed including the current one.
112
+ - events: The number of events processed including the current one.
113
+ - request\_events: The number of events processed in the current request
114
+ including the current one. For `on_poll` this will always be 0.
115
+
116
+ ```ruby
117
+ poller.poll do |event, stats|
118
+ log("Processing event number #{stats.events}")
119
+ end
120
+ ```
121
+
122
+ ### Stopping the poller
123
+
124
+ Use the `stop` method on the poller to manually stop polling for events. When
125
+ `stop` is called, the poller will finish processing the current request
126
+ including the associated events. Then it will exit the poll loop.
127
+
128
+ ```ruby
129
+ trap('INT') { poller.stop }
130
+ poller.poll do |event, stats|
131
+ # If the poller is interrupted in the middle of an event, it will finish
132
+ # processing before stopping
133
+ log('Processing event')
134
+ end
135
+
136
+ ### Setting the number of times to poll
137
+
138
+ The `poll` method accepts an argument `times` that sets how many times it checks
139
+ for new events. This can be useful for testing.
140
+
141
+ ```ruby
142
+ # Will only check for new events once
143
+ MovidaEvents::Poller.new(client).poll(1) do |event|
144
+ end
145
+ ```
146
+
147
+ ### Setting the API domain
148
+
149
+ The default API domain is `movida.bebanjo.net`. It can be set to a different
150
+ domain with the `domain` option in the client object.
151
+
152
+ ```ruby
153
+ MovidaEvents::Client.new(
154
+ username: 'my_user',
155
+ password: 'my_pass',
156
+ domain: 'staging-movida.bebanjo.net'
157
+ )
158
+ ```
159
+
160
+ ## Development
161
+
162
+ You can run `bin/console` for an interactive prompt that will allow you to
163
+ experiment.
164
+
165
+ Before committing, run `bin/rake` to run the linter and tests.
166
+
167
+ ## License
168
+
169
+ Copyright 2018 Warner Bros. Entertainment Inc.
170
+
171
+ Licensed under the Apache License, Version 2.0 (the "License");
172
+ you may not use this file except in compliance with the License.
173
+ You may obtain a copy of the License at
174
+
175
+ http://www.apache.org/licenses/LICENSE-2.0
176
+
177
+ Unless required by applicable law or agreed to in writing, software
178
+ distributed under the License is distributed on an "AS IS" BASIS,
179
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
180
+ See the License for the specific language governing permissions and
181
+ limitations under the License.
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+ require 'yard'
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+ RuboCop::RakeTask.new
10
+ YARD::Rake::YardocTask.new
11
+
12
+ task default: :check
13
+ task check: %i[rubocop spec]
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env sh
2
+
3
+ tag="$(git describe --abbrev=0)"
4
+ tag="${tag#v}"
5
+ gt=$(ruby -r ./lib/movida_events/version -e "puts Gem::Version.new(MovidaEvents::VERSION) >= Gem::Version.new('${tag}')")
6
+ test "$gt" = true
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'movida_events'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ require 'rubygems'
16
+ require 'bundler/setup'
17
+
18
+ load Gem.bin_path('rake', 'rake')
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ require 'rubygems'
16
+ require 'bundler/setup'
17
+
18
+ load Gem.bin_path('rspec-core', 'rspec')
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ require 'rubygems'
16
+ require 'bundler/setup'
17
+
18
+ load Gem.bin_path('rubocop', 'rubocop')
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'yard' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ require 'rubygems'
16
+ require 'bundler/setup'
17
+
18
+ load Gem.bin_path('yard', 'yard')
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'yardoc' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ require 'rubygems'
16
+ require 'bundler/setup'
17
+
18
+ load Gem.bin_path('yard', 'yardoc')
data/bin/yri ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'yri' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ require 'rubygems'
16
+ require 'bundler/setup'
17
+
18
+ load Gem.bin_path('yard', 'yri')
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A BeBanjo Movida event stream processor
4
+ module MovidaEvents
5
+ end
6
+
7
+ require 'almodovar'
8
+ require 'movida_events/version'
9
+ require 'movida_events/client'
10
+ require 'movida_events/poller'
11
+ require 'movida_events/stats'
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MovidaEvents
4
+ # The interface to the Movida event stream API
5
+ #
6
+ # Manages authentication and API configuration
7
+ class Client
8
+ # Create a new `MovidaEvents::Client`
9
+ #
10
+ # @param [Hash] options API setup options
11
+ # @option options [String] :username The username to authenticate with
12
+ # Required.
13
+ # @option options [String] :password The password to authenticate with.
14
+ # Required.
15
+ # @option options [String] :domain The API domain to use. Default:
16
+ # 'movida.bebanjo.net'.
17
+ def initialize(options = {})
18
+ @options = default_options.merge(options)
19
+ @auth = Almodovar::DigestAuth.new(
20
+ @options[:realm],
21
+ @options[:username],
22
+ @options[:password]
23
+ )
24
+ end
25
+
26
+ # Request a list of events
27
+ #
28
+ # @param [Hash] params Request parameters
29
+ # @option params [String] newer_than An event ID indicating the position in
30
+ # the event stream where the request starts.
31
+ # @option params [String] event_type A comma separated list of event types.
32
+ # @yield Each returned event
33
+ # @yieldparam event [Almodovar::Resource] The event object
34
+ # @return [Enumerator<Almodovar::Resource>] An enumerator over the returned
35
+ # events
36
+ def events(params = {}, &block)
37
+ Enumerator.new do |yielder|
38
+ events = events_collection(params)
39
+ events.each { |e| yielder << e }
40
+ end.each(&block)
41
+ end
42
+
43
+ private
44
+
45
+ # Get a `Almodovar::ResourceCollection` of events
46
+ #
47
+ # @param [Hash] params Request parameters, see {#events}.
48
+ def events_collection(params)
49
+ Almodovar::ResourceCollection.new(
50
+ api_url,
51
+ @auth,
52
+ nil,
53
+ params
54
+ )
55
+ end
56
+
57
+ # Get the events API URL
58
+ def api_url
59
+ "https://#{@options[:domain]}/api/events"
60
+ end
61
+
62
+ # Get the default options for {#initialize}
63
+ def default_options
64
+ {
65
+ realm: 'realm',
66
+ username: nil,
67
+ password: nil,
68
+ domain: 'movida.bebanjo.net'
69
+ }
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MovidaEvents
4
+ # Polls for events and calls a method for each one
5
+ class Poller
6
+ # Indicates if the stop method has been called.
7
+ #
8
+ # @see #stop
9
+ # @return [Boolean] True if the poller is stopped
10
+ attr_reader :stopped
11
+
12
+ # Create a new `MovidaEvents::Poller` object
13
+ #
14
+ # @param client [Client] The client for making API requests
15
+ # @param options [Hash] Configuration options
16
+ # @option options [Integer] :newer_than Requests events that occur
17
+ # only after the given ID
18
+ # @option options [String,Array<String>] :event_type Filter by only the
19
+ # given event types
20
+ def initialize(client, options = {})
21
+ @client = client
22
+ @options = default_options.merge(options)
23
+ @stopped = false
24
+ end
25
+
26
+ # Set a callback to run whenever an API request is made
27
+ #
28
+ # Only one callback may be set
29
+ #
30
+ # @yield Every time a poll request is made
31
+ # @yieldparam stats [Stats] The current stats
32
+ def on_poll(&block)
33
+ @on_poll = block
34
+ end
35
+
36
+ # Poll for events
37
+ #
38
+ # This method continues infinitely unless `times` is set.
39
+ #
40
+ # @param times [Integer,nil] If set, polling stops after `times` requests.
41
+ # @yield Every new event
42
+ # @yieldparam event [Almodovar::Resource] The event object
43
+ # @yieldparam stats [Stats] The current stats
44
+ def poll(times = nil)
45
+ stats = initial_stats
46
+ repeat(times) do
47
+ before_request(stats)
48
+ @client.events(request_params(stats)) do |event|
49
+ stats.receive_event(event)
50
+ yield event, stats.clone if block_given?
51
+ end
52
+ break if @stopped
53
+
54
+ sleep @options[:interval] if stats.request_events.zero?
55
+ end
56
+ end
57
+
58
+ # Stop the poller
59
+ #
60
+ # When this is called, the poller will finish processing the current request
61
+ # and any associated events before stopping.
62
+ def stop
63
+ @stopped = true
64
+ end
65
+
66
+ private
67
+
68
+ # Repeat times or forever
69
+ #
70
+ # @param times [Integer,nil] The number of times to repeat, or nil for
71
+ # infinite
72
+ # @yield `times` times or forever
73
+ def repeat(times)
74
+ times ? times.times { yield } : loop { yield }
75
+ end
76
+
77
+ # Builds the params for events requests
78
+ #
79
+ # The last event from stats determines where to start for the next API
80
+ # request
81
+ #
82
+ # @param stats [Stats] The current stats
83
+ # @return [Hash] The request params
84
+ def request_params(stats)
85
+ {
86
+ newer_than: stats.last,
87
+ event_type: event_types
88
+ }
89
+ end
90
+
91
+ # Builds the event types parameter
92
+ #
93
+ # Converts the `event_types` option into a string
94
+ # @return [String] The stringified events param
95
+ def event_types
96
+ types = @options[:event_types]
97
+ types.is_a?(Array) ? types.join(',') : types
98
+ end
99
+
100
+ # Before each request, updates stats and calls `on_poll`.
101
+ #
102
+ # @param stats [Stats] The current stats
103
+ def before_request(stats)
104
+ stats.start_request
105
+ @on_poll&.call(stats.clone)
106
+ end
107
+
108
+ # Sets up the initial stats state before polling
109
+ #
110
+ # If `newer_than` is not set, gets the latest event ID
111
+ #
112
+ # @return [Stats] The initial stats
113
+ def initial_stats
114
+ Stats.new(@options[:newer_than] || latest_id)
115
+ end
116
+
117
+ # Get the most recent event ID from the API
118
+ #
119
+ # The default behavior of the events API if given no parameters is to
120
+ # return the last 50 events. So we select the last event from those.
121
+ #
122
+ # @return [Integer] The most recent event ID
123
+ def latest_id
124
+ @client.events.to_a.last.id
125
+ end
126
+
127
+ # Get the default poller options
128
+ #
129
+ # @return [Hash] The default options
130
+ def default_options
131
+ {
132
+ newer_than: nil,
133
+ interval: 30
134
+ }
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MovidaEvents
4
+ # Tracks polling stats
5
+ class Stats
6
+ # The last-processed event ID
7
+ #
8
+ # `nil` if no events have been processed
9
+ #
10
+ # @return [Integer,nil] The event ID
11
+ attr_reader :last
12
+
13
+ # The number of requests made to the API
14
+ #
15
+ # @return [Integer] The request count
16
+ attr_reader :requests
17
+
18
+ # The number of events processed
19
+ #
20
+ # @return [Integer] The event count
21
+ attr_reader :events
22
+
23
+ # The number of events processed in the current request
24
+ #
25
+ # @return [Integer] The request event count
26
+ attr_reader :request_events
27
+
28
+ # Create a new `MovidaEvents::Stats` object
29
+ #
30
+ # @param last [Integer,nil] The last-processed event ID.
31
+ def initialize(last = nil)
32
+ @last = last
33
+ @requests = 0
34
+ @events = 0
35
+ @request_events = 0
36
+ end
37
+
38
+ # Update stats when an event is received
39
+ #
40
+ # @param event [Almodovar::Resource] The event received
41
+ def receive_event(event)
42
+ @last = event.id
43
+ @events += 1
44
+ @request_events += 1
45
+ end
46
+
47
+ # Update stats when a new request is started
48
+ def start_request
49
+ @requests += 1
50
+ @request_events = 0
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MovidaEvents
4
+ # The current gem version
5
+ VERSION = '1.0.0'
6
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'movida_events/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'movida_events'
9
+ spec.version = MovidaEvents::VERSION
10
+ spec.authors = ['Justin Howard']
11
+ spec.email = ['jmhoward0@gmail.com']
12
+ spec.license = 'Apache-2.0'
13
+
14
+ spec.summary = 'A BeBanjo Movida event stream processor'
15
+ spec.homepage = 'https://github.com/machinima/movida_events'
16
+
17
+ spec.files = `git ls-files -z`
18
+ .split("\x0")
19
+ .reject { |f| f.match(%r{^spec/}) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'activesupport', '>= 4.2'
23
+ spec.add_dependency 'almodovar', '~> 1.5'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.12'
26
+ spec.add_development_dependency 'byebug', '~> 9.0'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'redcarpet', '~> 3.4'
29
+ spec.add_development_dependency 'rspec', '~> 3.4'
30
+ spec.add_development_dependency 'rubocop', '~> 0.61'
31
+ spec.add_development_dependency 'simplecov', '~> 0.12'
32
+ spec.add_development_dependency 'webmock', '~> 2.1'
33
+ spec.add_development_dependency 'yard', '~> 0.9.11'
34
+ end
metadata ADDED
@@ -0,0 +1,221 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: movida_events
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Howard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: almodovar
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '9.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '9.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: redcarpet
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.61'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.61'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.12'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.12'
139
+ - !ruby/object:Gem::Dependency
140
+ name: webmock
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '2.1'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '2.1'
153
+ - !ruby/object:Gem::Dependency
154
+ name: yard
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 0.9.11
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 0.9.11
167
+ description:
168
+ email:
169
+ - jmhoward0@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".rubocop.yml"
177
+ - ".travis.yml"
178
+ - ".yardopts"
179
+ - Gemfile
180
+ - LICENSE.txt
181
+ - README.md
182
+ - Rakefile
183
+ - bin/check-version
184
+ - bin/console
185
+ - bin/rake
186
+ - bin/rspec
187
+ - bin/rubocop
188
+ - bin/yard
189
+ - bin/yardoc
190
+ - bin/yri
191
+ - lib/movida_events.rb
192
+ - lib/movida_events/client.rb
193
+ - lib/movida_events/poller.rb
194
+ - lib/movida_events/stats.rb
195
+ - lib/movida_events/version.rb
196
+ - movida_events.gemspec
197
+ homepage: https://github.com/machinima/movida_events
198
+ licenses:
199
+ - Apache-2.0
200
+ metadata: {}
201
+ post_install_message:
202
+ rdoc_options: []
203
+ require_paths:
204
+ - lib
205
+ required_ruby_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ required_rubygems_version: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
215
+ requirements: []
216
+ rubyforge_project:
217
+ rubygems_version: 2.7.7
218
+ signing_key:
219
+ specification_version: 4
220
+ summary: A BeBanjo Movida event stream processor
221
+ test_files: []