Class: Hooloo

Inherits:
Object
  • Object
show all
Defined in:
lib/hooloo/hooloo.rb,
lib/hooloo/version.rb

Defined Under Namespace

Classes: Company, Genre, MozartHash, Rollup, Show, Video

Constant Summary collapse

VERSION =
"0.3.2"

Class Method Summary collapse

Class Method Details

.api_uriObject



13
14
15
# File 'lib/hooloo/hooloo.rb', line 13

def api_uri
  hulu_uri + '/mozart/v1.h2o/'
end

.auto_cast(obj) ⇒ Object



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

def auto_cast(obj)
  # TODO: Automatically cast an object received from Mozart into the corresponding Ruby Class
end

.hulu_uriObject

TODO: Automatically cast an object received from Mozart into the corresponding Ruby Class



10
11
12
# File 'lib/hooloo/hooloo.rb', line 10

def hulu_uri
  URI("http://www.hulu.com/")
end

.paginated_request(path, args = {}, page_size = 70, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hooloo/hooloo.rb', line 27

def paginated_request(path, args={}, page_size=70, &block)
  # TODO: cache responses
  Enumerator.new do |g|
    loop.with_index do |_, i|
      response = request(path, {
        items_per_page: page_size,
        position: i * page_size
      }.merge(args))['data']
      response.each { |x| block.call(g, x) }
      break if response.length < page_size
    end
  end
end

.request(*args) ⇒ Object



23
24
25
26
# File 'lib/hooloo/hooloo.rb', line 23

def request(*args)
  # TODO: check if we get 403'd and request a new token
  MultiJson.load open(request_uri(*args)).read
end

.request_uri(path, query = {}) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/hooloo/hooloo.rb', line 16

def request_uri(path, query={})
  uri = api_uri + path
  uri.query = URI.encode_www_form({
    access_token: tokens["API"]
  }.merge(query))
  uri
end

.tokensObject



40
41
42
43
44
# File 'lib/hooloo/hooloo.rb', line 40

def tokens
  @tokens or begin
    @tokens = Hash[open(hulu_uri).read.scan(/([A-Z]+)_DONUT = '([a-zA-Z0-9\-_\/]+)';$/)]
  end
end

.tokens!Object



45
46
47
48
# File 'lib/hooloo/hooloo.rb', line 45

def tokens!
  @tokens = nil
  tokens
end