netologiest 0.0.1 → 0.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b6e1d7e36a3857d851d1bcc54007f3aeddfe753
4
- data.tar.gz: 7b0f8a1fc72547248c13c4f9a93ddf485af85131
3
+ metadata.gz: 5d4a464c0f9f259a1a0304671d6ddfac401644b6
4
+ data.tar.gz: 8ea14f2bdd955ae999b4f56cc95dbe38cecfc4a2
5
5
  SHA512:
6
- metadata.gz: dddf7b03ca6142c6b97ded47c1f8472ec8e534c7163a22edf2f17a8c214f01e00af1b782078dc2aa11301c614c380faf9a79886066f01c8bf6370e8a055d1892
7
- data.tar.gz: 371b2fdab4d38087abe607cfa3c2fbd90a659d80090b89a53a2a887e508134ceadfbf125546ccf9628dd17b732020c5b0097e01ebc6767d5966df779509fbd78
6
+ metadata.gz: 06132c8c86005a92955c55f76448ea21e54f7f683b1570bfeeb51f2ab0977db9ede15d00f49f898ea6139dfbe724170122eb7d90bda9b9cb607f1f1680edc112
7
+ data.tar.gz: e7bf43ccc6a8f764b891f45634a65c676fe7614e9e60220bc194a61112d9e4101993d7b780e55b8c2349dcff0ba7fb5194803d3de9a9290408bf49cc99e18f03
data/README.md CHANGED
@@ -25,7 +25,10 @@ For example (in your `secrets.yml`):
25
25
 
26
26
  ## Usage
27
27
 
28
- Netologiest having only one resource at moment (Course).
28
+ #### Course
29
+
30
+ Netologiest having only one resource at moment. It is a course.
31
+ And you can get list of courses
29
32
 
30
33
  To get a list of courses
31
34
 
@@ -78,6 +81,25 @@ Also you can get detailed information about any course:
78
81
  }
79
82
  ```
80
83
 
84
+ #### Lesson's video
85
+
86
+ Lesson class is used to obtain iframe token or iframe url (with token)
87
+
88
+ ```ruby
89
+ # returns complete URL to video iframe
90
+ Netologiest::Lesson.video_url(course_id, lesson_id)
91
+
92
+ # OR
93
+
94
+ lesson = Netologiest::Lesson.new(course_id, lesson_id)
95
+ # makes request to API and refresh lesson_token
96
+ lesson.video_token
97
+ # returns token
98
+ lesson.lesson_token
99
+ # returns complete URL
100
+ lesson.video_url
101
+ ```
102
+
81
103
  ## Contributing
82
104
 
83
105
  1. Fork it ( https://github.com/[my-github-username]/netologiest/fork )
data/lib/netologiest.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'netologiest/resource'
2
2
  require 'netologiest/resources/course'
3
+ require 'netologiest/resources/lesson'
3
4
  require 'netologiest/config'
4
5
  require 'netologiest/exceptions'
5
6
  require 'netologiest/version'
@@ -69,21 +69,16 @@ module Netologiest
69
69
 
70
70
  def handle_detail(_response); end
71
71
 
72
- protected
73
-
74
- def build_url(*args)
75
- File.join(Netologiest.config.api_url, *args.map(&:to_s))
76
- end
77
-
78
72
  # rubocop:disable Metrics/MethodLength
79
73
  def get(url, options = {})
80
- params = { token: token }.merge!(options)
74
+ params = { token: (options[:token] || token) }.merge!(options)
75
+
76
+ auth_method = options[:auth_method] || :authorize!
81
77
 
82
78
  RestClient.get(url, params: params) do |response, _request, _result|
83
79
  if response.code == 401
84
80
  begin
85
- authorize!
86
- params[:token] = token
81
+ params[:token] = send(auth_method)
87
82
  return RestClient.get(url, params: params)
88
83
  rescue RestClient::Unauthorized
89
84
  raise Netologiest::Unauthorized, response.body
@@ -93,5 +88,12 @@ module Netologiest
93
88
  end
94
89
  end
95
90
  # rubocop:enable Metrics/MethodLength
91
+
92
+ protected
93
+
94
+ def build_url(*args)
95
+ File.join(Netologiest.config.api_url, *args.map(&:to_s))
96
+ end
97
+
96
98
  end
97
99
  end
@@ -0,0 +1,51 @@
1
+ module Netologiest
2
+ class Lesson < Netologiest::Resource
3
+ self.resource_name = 'lessons'
4
+
5
+ attr_reader :lesson_token, :lesson_token_expire,
6
+ :course_id, :id, :iframe_url
7
+
8
+ def initialize(course_id, lesson_id)
9
+ authorize!
10
+ @course_id = course_id
11
+ @id = lesson_id
12
+ end
13
+
14
+ def self.video_url(course_id, lesson_id)
15
+ lesson = new(course_id, lesson_id)
16
+ lesson.video_token
17
+ lesson.video_url
18
+ end
19
+
20
+ def video_token
21
+ url = build_url(
22
+ Netologiest::Course.resource_name,
23
+ course_id,
24
+ self.class.resource_name,
25
+ id,
26
+ 'gettoken'
27
+ )
28
+
29
+ handle_lesson_token(get(url))
30
+ end
31
+
32
+ def video_url
33
+ @iframe_url = build_url(
34
+ Netologiest::Course.resource_name,
35
+ course_id,
36
+ self.class.resource_name,
37
+ id,
38
+ "iframe?token=#{lesson_token}"
39
+ )
40
+ end
41
+
42
+ private
43
+
44
+ def handle_lesson_token(response)
45
+ data = JSON.parse(response)
46
+ return unless data.present? || data.any?
47
+ @lesson_token_expire = data["expires_in"]
48
+ @lesson_token = data["access_token"]
49
+ end
50
+ end
51
+ end
@@ -1,4 +1,4 @@
1
1
  # :nodoc:
2
2
  module Netologiest
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
@@ -0,0 +1,4 @@
1
+ {
2
+ "access_token": "26C1WYAIWT3LRX12W8H4DTA9FRF3NLH9",
3
+ "expires_in": 600
4
+ }
@@ -76,4 +76,19 @@ module NetologiestWebMock
76
76
  body: File.read("spec/fixtures/course.json")
77
77
  )
78
78
  end
79
+
80
+ ## Lessons methods
81
+
82
+ def lesson_token_stub(course_id, lesson_id)
83
+ token = "S3PBVG38O1209Y01X5LEK0PYH0MT3YDZ"
84
+ url = Netologiest.config.api_url
85
+ url += "/courses/#{course_id}/lessons/#{lesson_id}"
86
+ url += "/gettoken?token=#{token}"
87
+ stub_request(:get, url)
88
+ .to_return(
89
+ headers: { 'Content-Type' => 'application/json' },
90
+ status: 200,
91
+ body: File.read("spec/fixtures/lesson_token.json")
92
+ )
93
+ end
79
94
  end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe Netologiest::Lesson do
4
+ describe "valid access token" do
5
+ before(:each) do
6
+ mock_api
7
+ lesson_token_stub(931, 2268)
8
+ end
9
+
10
+ it "return token" do
11
+ lesson = described_class.new(931, 2268)
12
+ expect(lesson.video_token)
13
+ .to eq "26C1WYAIWT3LRX12W8H4DTA9FRF3NLH9"
14
+ expect(lesson.lesson_token_expire)
15
+ .to eq 600
16
+ end
17
+
18
+ it "return iframe url with token" do
19
+ url = described_class.video_url(931, 2268)
20
+ expected = "#{Netologiest.config.api_url}/courses/931/lessons/2268/"
21
+ expected += "iframe?token=26C1WYAIWT3LRX12W8H4DTA9FRF3NLH9"
22
+ expect(url).to eq expected
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netologiest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vlad Dem
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-30 00:00:00.000000000 Z
11
+ date: 2015-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -161,14 +161,17 @@ files:
161
161
  - lib/netologiest/exceptions.rb
162
162
  - lib/netologiest/resource.rb
163
163
  - lib/netologiest/resources/course.rb
164
+ - lib/netologiest/resources/lesson.rb
164
165
  - lib/netologiest/version.rb
165
166
  - netologiest.gemspec
166
167
  - spec/course_spec.rb
167
168
  - spec/fixtures/auth.json
168
169
  - spec/fixtures/course.json
169
170
  - spec/fixtures/courses.json
171
+ - spec/fixtures/lesson_token.json
170
172
  - spec/fixtures/netologiest_test.yml
171
173
  - spec/helpers/webmock_helpers.rb
174
+ - spec/lesson_spec.rb
172
175
  - spec/resource_spec.rb
173
176
  - spec/spec_helper.rb
174
177
  homepage: ''
@@ -191,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
194
  version: '0'
192
195
  requirements: []
193
196
  rubyforge_project:
194
- rubygems_version: 2.4.5
197
+ rubygems_version: 2.2.2
195
198
  signing_key:
196
199
  specification_version: 4
197
200
  summary: Ruby API client for Netology
@@ -200,8 +203,10 @@ test_files:
200
203
  - spec/fixtures/auth.json
201
204
  - spec/fixtures/course.json
202
205
  - spec/fixtures/courses.json
206
+ - spec/fixtures/lesson_token.json
203
207
  - spec/fixtures/netologiest_test.yml
204
208
  - spec/helpers/webmock_helpers.rb
209
+ - spec/lesson_spec.rb
205
210
  - spec/resource_spec.rb
206
211
  - spec/spec_helper.rb
207
212
  has_rdoc: