Class: Videolog::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/videolog/base.rb

Direct Known Subclasses

Auth, Video

Constant Summary collapse

ENDPOINT =
"http://api.videolog.tv"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_options = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
# File 'lib/videolog/base.rb', line 15

def initialize(auth_options={})
  authorize(auth_options)
end

Instance Attribute Details

#authorizedObject (readonly)

Returns the value of attribute authorized.



7
8
9
# File 'lib/videolog/base.rb', line 7

def authorized
  @authorized
end

Class Method Details

.get_json(path, options = {}, &block) ⇒ Object



9
10
11
12
13
# File 'lib/videolog/base.rb', line 9

def self.get_json(path, options={}, &block)
  response = get("#{path}.json", options, &block)
  validate_response! response.parsed_response
  response.parsed_response
end

Instance Method Details

#authorize(options = {}) ⇒ Object

TODO: send some logic to Auth



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/videolog/base.rb', line 29

def authorize(options={}) # TODO: send some logic to Auth
  @auth = Videolog::Auth.new
  @authorized = false

  unless options[:username].nil? && options[:password].nil?
    @auth.auth_hash = Videolog::Auth.get_auth_hash(options[:username], options[:password])
  end

  unless options[:dev_token].nil?
    @auth.dev_token = options[:dev_token]
    @authorized = true
  end
end

#authorize!(options = {}) ⇒ Object



43
44
45
46
# File 'lib/videolog/base.rb', line 43

def authorize!(options={})
  authorize(options)
  raise AuthorizationNeeded unless authorized
end

#get_json(path, options = {}, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/videolog/base.rb', line 19

def get_json(path, options={}, &block)
  if authorized
    headers = {'Token' => @auth.dev_token}
    headers.merge!({'Autenticacao' => @auth.auth_hash}) unless @auth.auth_hash.nil?
    options = {headers: headers}.merge(options)
  end

  self.class.get_json(path, options, &block)
end