active_public_resources 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50bb4c74f2c87274aaf825004f9b111d32844ba3
4
- data.tar.gz: ea0f21d7a62d6a0d205445fcaa20e2bde2dbd78f
3
+ metadata.gz: 2b3ff207dc356c86315616be0944184327567a53
4
+ data.tar.gz: 5cc13fc5b7af5cdfad8b9502e6f0b8a84856bc7b
5
5
  SHA512:
6
- metadata.gz: 6324461f27ba94f8f8c59cb3d857dd54f8232ba6c54994287f8ffad76cb1e67180b1130bc0648a258513c4698f679bf9e524487949970e0e867f13ae6410ffd5
7
- data.tar.gz: d49032b26bd2a5eda55afb267e3f1e6d7958b4baca6762ad5fd3fe1c0961e61e6aee1c39d900a0156976f9ae21efa9af74d243e78f632d949cc060473f98c2c7
6
+ metadata.gz: f3f9d07a7926783c11f89803ec2913333341641e9aba0974861baef5c7eeb44f28dad7d4d5656fc32942e896d29d5312c9165f2da23f23be3e423da923415cb5
7
+ data.tar.gz: 0a37e389e9519c2b2a5344e387054521d7521e209dc34d3138a36d02b64bb9983d7d2363b40dd978d8dc22a30432a4e30d165dc391a3326bc114253b6a144820
data/Gemfile CHANGED
@@ -7,7 +7,7 @@ group :test do
7
7
  gem 'rspec', '~> 2.14.1'
8
8
  # gem 'rspec-mocks', '~> 2.14.4'
9
9
  gem 'vcr'
10
- gem 'webmock'
10
+ gem 'webmock', '~> 3.0.1'
11
11
  gem 'simplecov', '~> 0.8.1', :require => false
12
12
  gem 'growl'
13
13
  gem 'growl-rspec'
@@ -19,7 +19,7 @@ end
19
19
 
20
20
  group :development, :test do
21
21
  gem 'pry-remote'
22
- gem 'pry'
22
+ gem 'pry-byebug'
23
23
  end
24
24
 
25
25
  gemspec
data/README.md CHANGED
@@ -188,14 +188,12 @@ A driver in this context is a ruby class which performs requests and returns a r
188
188
 
189
189
  To use the Vimeo API, you must have credentials already. This requires a Vimeo app to
190
190
  be registered. You can do it at [https://developer.vimeo.com/apps](https://developer.vimeo.com/apps).
191
- There are 4 params which are necessary to perform the requests:
191
+ There are two params which are necessary to perform the requests:
192
192
 
193
193
  | Name | Required? | Description
194
194
  | ------------------- |:---------:| -------------
195
195
  | consumer_key | Yes | Vimeo API Client ID
196
196
  | consumer_secret | Yes | Vimeo API Client Secret
197
- | access_token | Yes | Vimeo API OAuth Access Token
198
- | access_token_secret | Yes | Vimeo API OAuth Access Token Secret
199
197
 
200
198
  #### Return Types
201
199
 
@@ -204,22 +202,20 @@ Vimeo returns **Iframe** and **URL** return types.
204
202
  #### Example:
205
203
 
206
204
  ```ruby
207
- criteria = APR::RequestCriteria.new( query: "education" )
205
+ criteria = APR::RequestCriteria.new(query: "education")
208
206
 
209
207
  vimeo = APR::Drivers::Vimeo.new(
210
208
  :consumer_key => 'VIMEO_CONSUMER_KEY',
211
- :consumer_secret => 'VIMEO_CONSUMER_SECRET',
212
- :access_token => 'VIMEO_ACCESS_TOKEN',
213
- :access_token_secret => 'VIMEO_ACCESS_TOKEN_SECRET'
209
+ :consumer_secret => 'VIMEO_CONSUMER_SECRET'
214
210
  )
215
211
 
216
- results = vimeo.perform_request( criteria )
212
+ results = vimeo.perform_request(criteria)
217
213
  results.items.length # => 25
218
214
  results.total_items # => 145063
219
215
  results.next_criteria # => #<ActivePublicResources::RequestCriteria:0x007fa48392d388 @query="education", @page=2, @per_page=25>
220
216
  results.items.first.title # => "Kynect 'education'"
221
217
 
222
- more_results = vimeo.perform_request( results.next_criteria )
218
+ more_results = vimeo.perform_request(results.next_criteria)
223
219
  # ...
224
220
  ```
225
221
 
@@ -21,9 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_dependency "activesupport", ">= 4.0.0"
22
22
  spec.add_dependency "activemodel", ">= 4.0.0"
23
23
  spec.add_dependency "iso8601", "~> 0.8.6"
24
-
25
- # Drivers
26
- spec.add_dependency "vimeo", "~> 1.5.3"
24
+ spec.add_dependency "httparty", "~> 0.15.6"
27
25
 
28
26
  spec.add_development_dependency "bundler", "~> 1.3"
29
27
  end
@@ -1,8 +1,6 @@
1
1
  vimeo:
2
2
  consumer_key: VIMEO_CONSUMER_KEY
3
3
  consumer_secret: VIMEO_CONSUMER_SECRET
4
- access_token: VIMEO_ACCESS_TOKEN
5
- access_token_secret: VIMEO_ACCESS_TOKEN_SECRET
6
4
  youtube:
7
5
  schooltube:
8
6
  khan_academy:
@@ -1,4 +1,5 @@
1
- require 'vimeo'
1
+ require_relative '../oauth/vimeo.rb'
2
+ require 'httparty'
2
3
 
3
4
  module ActivePublicResources
4
5
  module Drivers
@@ -18,18 +19,13 @@ module ActivePublicResources
18
19
  # @param [Hash] config_options the options which the vimeo gem requires
19
20
  # @option config_options [String] :consumer_key Vimeo consumer key (required)
20
21
  # @option config_options [String] :consumer_secret Vimeo consumer secret (required)
21
- # @option config_options [String] :access_token Vimeo access token (required)
22
- # @option config_options [String] :access_token_secret Vimeo access token secret (required)
23
22
  def initialize(config_options={})
24
- validate_options(config_options,
25
- [:consumer_key, :consumer_secret, :access_token, :access_token_secret])
26
-
27
- @client = ::Vimeo::Advanced::Video.new(
23
+ validate_options(config_options, [:consumer_key, :consumer_secret])
24
+ @client = ActivePublicResources::OAuth::Vimeo.new(
28
25
  config_options[:consumer_key],
29
- config_options[:consumer_secret],
30
- token: config_options[:access_token],
31
- secret: config_options[:access_token_secret]
26
+ config_options[:consumer_secret]
32
27
  )
28
+ @access_token = @client.get_access_token
33
29
  end
34
30
 
35
31
  # Perform search request to Vimeo with search criteria
@@ -84,16 +80,23 @@ module ActivePublicResources
84
80
  request_criteria.validate_presence!([:query])
85
81
  raise StandardError.new("driver has not been initialized properly") unless @client
86
82
 
87
- results = @client.search(request_criteria.query, {
88
- :page => request_criteria.page || 1,
89
- :per_page => request_criteria.per_page || 25,
90
- :full_response => 1,
91
- :sort => request_criteria.sort || "relevant",
92
- :user_id => nil,
93
- :content_filter => request_criteria.content_filter || 'safe'
94
- })
83
+ results = HTTParty.get('https://api.vimeo.com/videos',
84
+ query: {
85
+ query: request_criteria.query,
86
+ page: request_criteria.page || 1,
87
+ per_page: request_criteria.per_page || 25,
88
+ sort: 'relevant',
89
+ filter: 'content_rating',
90
+ filter_content_rating: 'safe'
91
+ },
92
+ headers: { "Authorization" => "Bearer #{@access_token}" }
93
+ )
95
94
 
96
- return parse_results(request_criteria, results)
95
+ return parse_results(request_criteria, JSON.parse(results)) unless results.code == 401
96
+ if !@client.verify_token?(@access_token)
97
+ @access_token = @client.get_access_token
98
+ perform_request(request_criteria) if !@access_token.blank?
99
+ end
97
100
  end
98
101
 
99
102
  private
@@ -126,15 +129,16 @@ module ActivePublicResources
126
129
  @driver_response = DriverResponse.new(
127
130
  :criteria => request_criteria,
128
131
  :next_criteria => next_criteria(request_criteria, results),
129
- :total_items => results['videos']['total'].to_i,
130
- :items => results['videos']['video'].map { |data| parse_video(data) }
132
+ :total_items => results['total'].to_i,
133
+ :items => results['data'].map { |data| parse_video(data) }
131
134
  )
132
135
  end
133
136
 
134
137
  def next_criteria(request_criteria, results)
135
- page = results['videos']['page'].to_i
136
- per_page = results['videos']['perpage'].to_i
137
- total = results['videos']['total'].to_i
138
+ total = results['total'].to_i
139
+ page = results['page'].to_i
140
+ per_page = results['per_page'].to_i
141
+
138
142
  if ((page * per_page) < total)
139
143
  return RequestCriteria.new({
140
144
  :query => request_criteria.query,
@@ -146,18 +150,18 @@ module ActivePublicResources
146
150
 
147
151
  def parse_video(data)
148
152
  video = ActivePublicResources::ResponseTypes::Video.new
149
- video.id = data['id']
150
- video.title = data['title']
151
- video.description = data['description']
152
- video.thumbnail_url = data['thumbnails']['thumbnail'][0]['_content']
153
- video.url = data['urls']['url'][0]['_content']
154
- video.embed_url = "https://player.vimeo.com/video/#{data['id']}"
153
+ video.id = "#{data['uri']}".gsub(/[^\d]/, '').to_i
154
+ video.title = data['name']
155
+ video.description = data['description'] || "No description found"
156
+ video.thumbnail_url = data['pictures']['sizes'][0]['link']
157
+ video.url = data['link']
158
+ video.embed_url = "https://player.vimeo.com/video/#{video.id}"
155
159
  video.duration = data['duration'].to_i
156
- video.num_views = data['number_of_plays'].to_i
157
- video.num_likes = data['number_of_likes'].to_i
158
- video.num_comments = data['number_of_comments'].to_i
159
- video.created_date = Date.parse(data['upload_date'])
160
- video.username = data['owner']['display_name']
160
+ video.num_views = data['stats']['plays'].to_i
161
+ video.num_likes = data['metadata']['connections']['likes']['total'].to_i
162
+ video.num_comments = data['metadata']['connections']['comments']['total'].to_i
163
+ video.created_date = Date.parse(data['created_time'])
164
+ video.username = data['user']['name']
161
165
  video.width = 640
162
166
  video.height = 360
163
167
 
@@ -172,13 +176,12 @@ module ActivePublicResources
172
176
  video.return_types << APR::ReturnTypes::Iframe.new(
173
177
  :driver => DRIVER_NAME,
174
178
  :remote_id => video.id,
175
- :url => "https://player.vimeo.com/video/#{data['id']}",
179
+ :url => "https://player.vimeo.com/video/#{video.id}",
176
180
  :text => video.title,
177
181
  :title => video.title,
178
182
  :width => 640,
179
183
  :height => 360
180
184
  )
181
-
182
185
  video
183
186
  end
184
187
 
@@ -0,0 +1,35 @@
1
+ require 'httparty'
2
+ require 'base64'
3
+
4
+ module ActivePublicResources
5
+ module OAuth
6
+ class Vimeo
7
+ attr_reader :token
8
+
9
+ AUTHORIZE_URL = 'https://api.vimeo.com/oauth/authorize/client'
10
+ VERIFY_URL = 'https://api.vimeo.com/oauth/verify'
11
+
12
+ def initialize(consumer_key, consumer_secret)
13
+ @token = Base64.urlsafe_encode64("#{consumer_key}:#{consumer_secret}")
14
+ end
15
+
16
+ def get_access_token
17
+ response = HTTParty.post(
18
+ AUTHORIZE_URL,
19
+ body: { grant_type: 'client_credentials' },
20
+ headers: { "Authorization" => "Basic #{@token}" }
21
+ )
22
+ response['access_token']
23
+ end
24
+
25
+ def verify_token?(token)
26
+ response = HTTParty.get(
27
+ VERIFY_URL,
28
+ headers: { "Authorization" => "Bearer #{token}" }
29
+ )
30
+ response.code == 200
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module ActivePublicResources
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -3,16 +3,16 @@ require 'spec_helper'
3
3
  describe APR::Drivers::Vimeo do
4
4
 
5
5
  let(:driver) { APR::Drivers::Vimeo.new(config_data[:vimeo]) }
6
-
6
+
7
7
  describe ".initialize" do
8
- it "should throw error when intializing without proper config options" do
8
+ it "should throw error when initializing without proper config options" do
9
9
  expect {
10
10
  APR::Drivers::Vimeo.new
11
11
  }.to raise_error(ArgumentError)
12
12
  end
13
13
 
14
14
  it "should build a vimeo client on initialize" do
15
- driver.client.should be_an_instance_of(::Vimeo::Advanced::Video)
15
+ driver.client.should be_an_instance_of(APR::OAuth::Vimeo)
16
16
  end
17
17
  end
18
18
 
@@ -71,5 +71,5 @@ describe APR::Drivers::Vimeo do
71
71
  item.height.should eq(360)
72
72
  end
73
73
  end
74
-
74
+
75
75
  end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'base64'
3
+ require 'securerandom'
4
+
5
+ describe APR::OAuth::Vimeo do
6
+
7
+ let(:client) { APR::OAuth::Vimeo.new(consumer_key, consumer_secret) }
8
+ let(:consumer_key) { 'key' }
9
+ let(:consumer_secret) { 'secret' }
10
+ let(:token) { Base64.urlsafe_encode64("#{consumer_key}:#{consumer_secret}") }
11
+ let(:access_token) { "#{SecureRandom.uuid}" }
12
+
13
+ describe ".initialize" do
14
+ it "should build a base64 encoded token" do
15
+ expect(client.token).to eq token
16
+ end
17
+ end
18
+
19
+ describe "#get_access_token" do
20
+ let(:response) { { "access_token" => access_token } }
21
+
22
+ it "should generate an access token" do
23
+ allow(HTTParty).to receive(:post) { response }
24
+ expect(client.get_access_token).to eq access_token
25
+ end
26
+ end
27
+
28
+ describe "#verify_token?" do
29
+ let(:response_ok) { double(:code => 200) }
30
+ let(:response_unauthorized) { double(:code => 401) }
31
+
32
+ it "should return 200 if token is verified" do
33
+ allow(HTTParty).to receive(:get) { response_ok }
34
+ expect(client.verify_token?(token)).to eq true
35
+ end
36
+
37
+ it "should return 401 if token is unauthorized" do
38
+ allow(HTTParty).to receive(:get) { response_unauthorized }
39
+ expect(client.verify_token?(token)).to eq false
40
+ end
41
+ end
42
+
43
+ end
data/spec/spec_helper.rb CHANGED
@@ -10,9 +10,11 @@ rescue LoadError => e
10
10
  end
11
11
 
12
12
  require 'active_public_resources'
13
+ require 'webmock/rspec'
13
14
  require 'vcr'
14
15
  require 'pry'
15
16
 
17
+
16
18
  def config_data
17
19
  yaml_path = File.join(ActivePublicResources.root, 'active_public_resources_config.yml')
18
20
  unless File.exist? yaml_path
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_public_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Berry
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-10 00:00:00.000000000 Z
11
+ date: 2017-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -53,19 +53,19 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.8.6
55
55
  - !ruby/object:Gem::Dependency
56
- name: vimeo
56
+ name: httparty
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 1.5.3
61
+ version: 0.15.6
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 1.5.3
68
+ version: 0.15.6
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +108,7 @@ files:
108
108
  - lib/active_public_resources/drivers/schooltube.rb
109
109
  - lib/active_public_resources/drivers/vimeo.rb
110
110
  - lib/active_public_resources/drivers/youtube.rb
111
+ - lib/active_public_resources/oauth/vimeo.rb
111
112
  - lib/active_public_resources/request_criteria.rb
112
113
  - lib/active_public_resources/response_types/exercise.rb
113
114
  - lib/active_public_resources/response_types/folder.rb
@@ -129,6 +130,7 @@ files:
129
130
  - spec/lib/active_public_resources/drivers/vimeo_spec.rb
130
131
  - spec/lib/active_public_resources/drivers/youtube_spec.rb
131
132
  - spec/lib/active_public_resources/live_client_spec.rb
133
+ - spec/lib/active_public_resources/oauth/vimeo_spec.rb
132
134
  - spec/lib/active_public_resources/request_criteria_spec.rb
133
135
  - spec/spec_helper.rb
134
136
  - spec/vcr/active_public_resources/client/khan_academy_should_traverse_folders.yml
@@ -186,6 +188,7 @@ test_files:
186
188
  - spec/lib/active_public_resources/drivers/vimeo_spec.rb
187
189
  - spec/lib/active_public_resources/drivers/youtube_spec.rb
188
190
  - spec/lib/active_public_resources/live_client_spec.rb
191
+ - spec/lib/active_public_resources/oauth/vimeo_spec.rb
189
192
  - spec/lib/active_public_resources/request_criteria_spec.rb
190
193
  - spec/spec_helper.rb
191
194
  - spec/vcr/active_public_resources/client/khan_academy_should_traverse_folders.yml