Class: MyShows::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Memoize, Singleton
Defined in:
lib/myshows/api.rb

Instance Method Summary collapse

Constructor Details

#initializeAPI

Returns a new instance of API.



21
22
23
# File 'lib/myshows/api.rb', line 21

def initialize
  %w{search user_shows show_episodes}.each {|m| memoize m }
end

Instance Method Details

#authorize(login, password) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/myshows/api.rb', line 25

def authorize(, password)
  res = get '/profile/login', :login => , :password => password
  error "login or password is incorrect while authorization" if res.code == 403
  error "unknown error while authorization" if res.code != 200

  self.class.cookies.add_cookies res.headers["set-cookie"]
end

#check_episode(episode) ⇒ Object

Checks given episode as watched



70
71
72
73
74
75
76
# File 'lib/myshows/api.rb', line 70

def check_episode(episode)
  res = get "/profile/episodes/check/#{episode.id}"
  error "authorization problems while checking episode #{episode}" if res.code == 401
  error "unknown error while checking episode #{episode}" if res.code != 200

  episode
end

#search_episode_by_filename(filename) ⇒ Object

Returns array of episodes



52
53
54
55
56
57
58
# File 'lib/myshows/api.rb', line 52

def search_episode_by_filename(filename)
  res = get '/shows/search/file/', :q => filename
  return [] if res.code == 404
  error "unknown error while searching by filename \"#{filename}\"" if res.code != 200

  json2episode_with_show res.body
end

#search_show(name) ⇒ Object

Returns array of found shows



43
44
45
46
47
48
49
# File 'lib/myshows/api.rb', line 43

def search_show(name)
  res = get '/shows/search/', :q => name
  return [] if res.code == 404
  error "unknown error while searching show \"#{name}\"" if res.code != 200

  found_shows = json2shows res.body
end

#show_episodes(show) ⇒ Object

Returns episodes of given show



61
62
63
64
65
66
67
# File 'lib/myshows/api.rb', line 61

def show_episodes(show)
  res = get "/shows/#{show.id}"
  error "could not find show #{show} while getting its episodes" if res.code == 404
  error "unknown error while getting episodes of show #{show}" if res.code != 200

  json2episodes res.body, show
end

#uncheck_episode(episode) ⇒ Object

Unchecks given episode as watched



79
80
81
82
83
84
85
# File 'lib/myshows/api.rb', line 79

def uncheck_episode(episode)
  res = get "/profile/episodes/uncheck/#{episode.id}"
  error "authorization problems while unchecking episode #{episode}" if res.code == 401
  error "unknown error while unchecking episode #{episode}" if res.code != 200

  episode
end

#user_showsObject

Returns all user shows



34
35
36
37
38
39
40
# File 'lib/myshows/api.rb', line 34

def user_shows
  res = get '/profile/shows/'
  error "authorization problems while getting user shows" if res.code == 401
  error "unknown error while getting user shows" if res.code != 200

  json2shows res.body
end