Class: Udemy::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/udemy/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.

Raises:



9
10
11
12
13
14
15
16
17
# File 'lib/udemy/client.rb', line 9

def initialize
  raise Error.new("Your API credentials are missing") \
    if (Udemy.client_key.blank? || Udemy.client_secret.blank?)

  @headers = {
    "X-Udemy-Client-Id" => Udemy.client_key,
    "X-Udemy-Client-Secret" => Udemy.client_secret
  }
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/udemy/client.rb', line 7

def headers
  @headers
end

Class Method Details

.get(*args) ⇒ Object

Handle Response from HTTParty



32
33
34
# File 'lib/udemy/client.rb', line 32

def self.get(*args)
  handle_response(super)
end

.handle_response(response) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/udemy/client.rb', line 40

def self.handle_response(response)
  raise Error.new("No response from the server. Make sure the URL is correct.") \
    if (response.blank? || response.parsed_response.blank?)

  json_response = JSON.parse(response.parsed_response)
  response_hash = Hashie::Mash.new(json_response)

  raise Udemy::APIResponseError.new(response_hash) unless response.code == 200

  return Hashie::Mash.new(response_hash)
end

.post(*args) ⇒ Object



36
37
38
# File 'lib/udemy/client.rb', line 36

def self.post(*args)
  handle_response(super)
end

Instance Method Details

#get(url, opts = {}) ⇒ Object



19
20
21
22
23
# File 'lib/udemy/client.rb', line 19

def get(url, opts = {})
  opts = {headers: @headers}.merge(opts)

  Udemy::Client.get(url, opts)
end

#post(url, opts = {}) ⇒ Object



25
26
27
28
29
# File 'lib/udemy/client.rb', line 25

def post(url, opts = {})
  opts = {headers: @headers}.merge(opts)

  Udemy::Client.post(url, opts)
end