netologiest 0.0.2 → 0.0.3

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: 5d4a464c0f9f259a1a0304671d6ddfac401644b6
4
- data.tar.gz: 8ea14f2bdd955ae999b4f56cc95dbe38cecfc4a2
3
+ metadata.gz: ab1fb2642d66268d3db3645c476ac5c31edc2394
4
+ data.tar.gz: '0289f3f6d664b1e66395703fac33ecf26db6b068'
5
5
  SHA512:
6
- metadata.gz: 06132c8c86005a92955c55f76448ea21e54f7f683b1570bfeeb51f2ab0977db9ede15d00f49f898ea6139dfbe724170122eb7d90bda9b9cb607f1f1680edc112
7
- data.tar.gz: e7bf43ccc6a8f764b891f45634a65c676fe7614e9e60220bc194a61112d9e4101993d7b780e55b8c2349dcff0ba7fb5194803d3de9a9290408bf49cc99e18f03
6
+ metadata.gz: 1bf045b926f9a041bb79e8cd002e3e969a0a683b162ac56aa1fe7241518defeecfdffb8061ce170e00ff9a9021f4aaecd508d86e8c279599c84bb6e6a82d4529
7
+ data.tar.gz: e26b2deabd30815a72346f03de4a6a4b7b34fd66b7adf7d28c3a7a5eb72b3a53290aef4b0267a110b5aae4982d0251fc985d221aeb0acb2536bacc4441c9795b
@@ -4,6 +4,7 @@ require 'netologiest/resources/lesson'
4
4
  require 'netologiest/config'
5
5
  require 'netologiest/exceptions'
6
6
  require 'netologiest/version'
7
+ require 'netologiest/http_client'
7
8
 
8
9
  # Ruby client for Netology API
9
10
  module Netologiest
@@ -0,0 +1,32 @@
1
+ class HttpClient
2
+ class << self
3
+ def get(url, params: {})
4
+ uri = URI(url)
5
+ uri.query = URI.encode_www_form(params)
6
+ response = Response.new(Net::HTTP.get_response(uri))
7
+ if block_given?
8
+ yield response
9
+ else
10
+ response
11
+ end
12
+ end
13
+ end
14
+
15
+ class Response
16
+ attr_reader :body, :code
17
+
18
+ def success?
19
+ response.is_a?(Net::HTTPSuccess)
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :response
25
+
26
+ def initialize(response)
27
+ @response = response
28
+ @body = response.body
29
+ @code = response.code.to_i
30
+ end
31
+ end
32
+ end
@@ -1,6 +1,4 @@
1
1
  require 'json'
2
- require 'rest-client'
3
- require 'active_support'
4
2
 
5
3
  module Netologiest
6
4
  # This class describe client for
@@ -41,18 +39,18 @@ module Netologiest
41
39
  )
42
40
  end
43
41
 
44
- # rubocop:disable Metrics/AbcSize, Metrics//MethodLength
42
+ # rubocop:disable /AbcSize, Metrics//MethodLength
45
43
  def authorize!
46
44
  url = build_url('gettoken')
47
45
  params = { client_secret: Netologiest.config.api_key }
48
- RestClient.get(url, params: params) do |response, _request, _result|
46
+ HttpClient.get(url, params: params) do |response, _request, _result|
49
47
  case response.code
50
48
  when 200
51
49
  body = JSON.parse(response.body)
52
50
  @token_expire = Time.now.to_i + body.fetch('expires_in').to_i
53
51
  @token = body['access_token']
54
52
  when 401
55
- fail Netologiest::Unauthorized, response.body
53
+ raise Netologiest::Unauthorized, response.body
56
54
  else
57
55
  response
58
56
  end
@@ -61,7 +59,8 @@ module Netologiest
61
59
  # rubocop:enable Metrics/AbcSize, Metrics//MethodLength
62
60
 
63
61
  def token_expired?
64
- return true unless token_expire.present?
62
+ return true if token_expire.to_s.empty?
63
+
65
64
  token_expire < Time.now.to_i
66
65
  end
67
66
 
@@ -75,16 +74,19 @@ module Netologiest
75
74
 
76
75
  auth_method = options[:auth_method] || :authorize!
77
76
 
78
- RestClient.get(url, params: params) do |response, _request, _result|
77
+ HttpClient.get(url, params: params) do |response, _request, _result|
79
78
  if response.code == 401
80
79
  begin
81
80
  params[:token] = send(auth_method)
82
- return RestClient.get(url, params: params)
83
- rescue RestClient::Unauthorized
84
- raise Netologiest::Unauthorized, response.body
81
+ new_response = HttpClient.get(url, params: params)
82
+ if new_response.success?
83
+ return new_response.body
84
+ else
85
+ raise Netologiest::Unauthorized, response.body
86
+ end
85
87
  end
86
88
  end
87
- response
89
+ response.body
88
90
  end
89
91
  end
90
92
  # rubocop:enable Metrics/MethodLength
@@ -94,6 +96,5 @@ module Netologiest
94
96
  def build_url(*args)
95
97
  File.join(Netologiest.config.api_url, *args.map(&:to_s))
96
98
  end
97
-
98
99
  end
99
100
  end
@@ -21,7 +21,8 @@ module Netologiest
21
21
 
22
22
  def parse_json(response)
23
23
  data = JSON.parse(response)
24
- return unless data.present? || data.any?
24
+ return if data.kind_of?(Array) && data.empty?
25
+
25
26
  data
26
27
  end
27
28
  end
@@ -43,9 +43,10 @@ module Netologiest
43
43
 
44
44
  def handle_lesson_token(response)
45
45
  data = JSON.parse(response)
46
- return unless data.present? || data.any?
46
+ return if data.kind_of?(Hash) && data.empty?
47
+
47
48
  @lesson_token_expire = data["expires_in"]
48
49
  @lesson_token = data["access_token"]
49
50
  end
50
51
  end
51
- end
52
+ end
@@ -1,4 +1,4 @@
1
1
  # :nodoc:
2
2
  module Netologiest
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3".freeze
4
4
  end
@@ -23,8 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "webmock"
25
25
  spec.add_development_dependency 'pry'
26
+ spec.add_development_dependency 'pry-byebug'
26
27
  spec.add_dependency "anyway_config", "~> 0", ">= 0.3"
27
- spec.add_dependency "rest-client"
28
- spec.add_dependency 'activesupport'
29
28
  spec.add_dependency 'json'
30
29
  end
@@ -22,4 +22,4 @@ describe Netologiest::Lesson do
22
22
  expect(url).to eq expected
23
23
  end
24
24
  end
25
- end
25
+ end
@@ -6,6 +6,6 @@ describe Netologiest::Resource do
6
6
  it "get authorization token automaticaly" do
7
7
  res = described_class.new
8
8
  expect(res.token).to eq "S3PBVG38O1209Y01X5LEK0PYH0MT3YDZ"
9
- expect(res.token_expire.present?).to be_truthy
9
+ expect(res.token_expire.to_s.empty?).not_to be_truthy
10
10
  end
11
11
  end
@@ -8,7 +8,7 @@ begin
8
8
  rescue LoadError
9
9
  end
10
10
 
11
- ENV["NETOLOGIEST_CONF"] = File.expand_path("../fixtures/netologiest_test.yml", __FILE__)
11
+ ENV["NETOLOGIEST_CONF"] = File.expand_path('fixtures/netologiest_test.yml', __dir__)
12
12
 
13
13
  RSpec.configure do |config|
14
14
  config.mock_with :rspec
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.2
4
+ version: 0.0.3
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-08-12 00:00:00.000000000 Z
11
+ date: 2018-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -81,33 +81,13 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: anyway_config
84
+ name: pry-byebug
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
87
  - - ">="
91
- - !ruby/object:Gem::Version
92
- version: '0.3'
93
- type: :runtime
94
- prerelease: false
95
- version_requirements: !ruby/object:Gem::Requirement
96
- requirements:
97
- - - "~>"
98
88
  - !ruby/object:Gem::Version
99
89
  version: '0'
100
- - - ">="
101
- - !ruby/object:Gem::Version
102
- version: '0.3'
103
- - !ruby/object:Gem::Dependency
104
- name: rest-client
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - ">="
108
- - !ruby/object:Gem::Version
109
- version: '0'
110
- type: :runtime
90
+ type: :development
111
91
  prerelease: false
112
92
  version_requirements: !ruby/object:Gem::Requirement
113
93
  requirements:
@@ -115,19 +95,25 @@ dependencies:
115
95
  - !ruby/object:Gem::Version
116
96
  version: '0'
117
97
  - !ruby/object:Gem::Dependency
118
- name: activesupport
98
+ name: anyway_config
119
99
  requirement: !ruby/object:Gem::Requirement
120
100
  requirements:
121
- - - ">="
101
+ - - "~>"
122
102
  - !ruby/object:Gem::Version
123
103
  version: '0'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0.3'
124
107
  type: :runtime
125
108
  prerelease: false
126
109
  version_requirements: !ruby/object:Gem::Requirement
127
110
  requirements:
128
- - - ">="
111
+ - - "~>"
129
112
  - !ruby/object:Gem::Version
130
113
  version: '0'
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0.3'
131
117
  - !ruby/object:Gem::Dependency
132
118
  name: json
133
119
  requirement: !ruby/object:Gem::Requirement
@@ -159,6 +145,7 @@ files:
159
145
  - lib/netologiest.rb
160
146
  - lib/netologiest/config.rb
161
147
  - lib/netologiest/exceptions.rb
148
+ - lib/netologiest/http_client.rb
162
149
  - lib/netologiest/resource.rb
163
150
  - lib/netologiest/resources/course.rb
164
151
  - lib/netologiest/resources/lesson.rb
@@ -194,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
181
  version: '0'
195
182
  requirements: []
196
183
  rubyforge_project:
197
- rubygems_version: 2.2.2
184
+ rubygems_version: 2.5.2
198
185
  signing_key:
199
186
  specification_version: 4
200
187
  summary: Ruby API client for Netology
@@ -209,4 +196,3 @@ test_files:
209
196
  - spec/lesson_spec.rb
210
197
  - spec/resource_spec.rb
211
198
  - spec/spec_helper.rb
212
- has_rdoc: