the_tv_db 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/README.md +1 -1
- data/Rakefile +7 -0
- data/lib/the_tv_db/connection.rb +5 -1
- data/lib/the_tv_db/response/raise_error.rb +10 -0
- data/lib/the_tv_db/response/unzip.rb +33 -0
- data/lib/the_tv_db/response/xmlize.rb +4 -4
- data/lib/the_tv_db/response.rb +2 -2
- data/lib/the_tv_db/series.rb +15 -7
- data/lib/the_tv_db/version.rb +1 -1
- data/spec/fixtures/series/en/actors.xml +73 -0
- data/spec/fixtures/series/en/banners.xml +2275 -0
- data/spec/fixtures/series/en/en.xml +2762 -0
- data/spec/fixtures/series/en.zip +0 -0
- data/spec/the_tv_db/series_spec.rb +40 -11
- data/the_tv_db.gemspec +1 -1
- metadata +33 -22
    
        data/.travis.yml
    ADDED
    
    
    
        data/Gemfile
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    
    
        data/Rakefile
    CHANGED
    
    
    
        data/lib/the_tv_db/connection.rb
    CHANGED
    
    | @@ -1,6 +1,8 @@ | |
| 1 1 | 
             
            require "faraday"
         | 
| 2 2 | 
             
            require "the_tv_db/response"
         | 
| 3 | 
            +
            require "the_tv_db/response/unzip"
         | 
| 3 4 | 
             
            require "the_tv_db/response/xmlize"
         | 
| 5 | 
            +
            require "the_tv_db/response/raise_error"
         | 
| 4 6 |  | 
| 5 7 | 
             
            module TheTvDB
         | 
| 6 8 | 
             
              module Connection
         | 
| @@ -11,8 +13,10 @@ module TheTvDB | |
| 11 13 | 
             
                def connection
         | 
| 12 14 | 
             
                  @connection ||= Faraday.new(:url => TheTvDB::ENDPOINT) do |faraday|
         | 
| 13 15 | 
             
                    faraday.response :logger if ENV['DEBUG'] # log requests to STDOUT
         | 
| 14 | 
            -
                    faraday.adapter  : | 
| 16 | 
            +
                    faraday.adapter  :net_http # make requests with NetHTTP
         | 
| 15 17 | 
             
                    faraday.use      TheTvDB::Response::Xmlize
         | 
| 18 | 
            +
                    faraday.use      TheTvDB::Response::Unzip
         | 
| 19 | 
            +
                    faraday.use      TheTvDB::Response::RaiseError
         | 
| 16 20 | 
             
                  end
         | 
| 17 21 | 
             
                end
         | 
| 18 22 |  | 
| @@ -0,0 +1,10 @@ | |
| 1 | 
            +
            module TheTvDB
         | 
| 2 | 
            +
              class Response::RaiseError < Faraday::Response::Middleware
         | 
| 3 | 
            +
             | 
| 4 | 
            +
                def on_complete(env)
         | 
| 5 | 
            +
                  status_code = env[:status].to_i
         | 
| 6 | 
            +
                  raise TheTvDB::RecordNotFound.new(env) if (400...600).include? status_code
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              end # Response::RaiseError
         | 
| 10 | 
            +
            end # TheTvDB
         | 
| @@ -0,0 +1,33 @@ | |
| 1 | 
            +
            module TheTvDB
         | 
| 2 | 
            +
              class Response::Unzip < Response
         | 
| 3 | 
            +
                dependency 'zip/zip'
         | 
| 4 | 
            +
                
         | 
| 5 | 
            +
                define_parser do |body|
         | 
| 6 | 
            +
                  ::Zip::ZipFile.new body
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                def parse(body)
         | 
| 10 | 
            +
                  case body
         | 
| 11 | 
            +
                  when String
         | 
| 12 | 
            +
                    zip = Tempfile.new('unzip_me')
         | 
| 13 | 
            +
                    begin
         | 
| 14 | 
            +
                       zip.write(body)
         | 
| 15 | 
            +
                       unzipped = self.class.parser.call(zip)
         | 
| 16 | 
            +
                       files = {}
         | 
| 17 | 
            +
                       unzipped.each do |file|
         | 
| 18 | 
            +
                         files[file.name] = file.get_input_stream.read
         | 
| 19 | 
            +
                       end
         | 
| 20 | 
            +
                    ensure
         | 
| 21 | 
            +
                       zip.close
         | 
| 22 | 
            +
                       zip.unlink
         | 
| 23 | 
            +
                    end
         | 
| 24 | 
            +
                    files
         | 
| 25 | 
            +
                  else
         | 
| 26 | 
            +
                    body
         | 
| 27 | 
            +
                  end
         | 
| 28 | 
            +
                rescue
         | 
| 29 | 
            +
                  body
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
                
         | 
| 32 | 
            +
              end # Response::Unzip
         | 
| 33 | 
            +
            end # TheTvDB
         | 
| @@ -1,12 +1,10 @@ | |
| 1 | 
            -
            require "multi_xml"
         | 
| 2 | 
            -
             | 
| 3 1 | 
             
            module TheTvDB
         | 
| 4 2 | 
             
              class Response::Xmlize < Response
         | 
| 5 3 | 
             
                dependency 'multi_xml'
         | 
| 6 4 |  | 
| 7 5 | 
             
                define_parser do |body|
         | 
| 8 | 
            -
                  MultiXml.parser = :ox
         | 
| 9 | 
            -
                  MultiXml.parse body
         | 
| 6 | 
            +
                  ::MultiXml.parser = :ox
         | 
| 7 | 
            +
                  ::MultiXml.parse body
         | 
| 10 8 | 
             
                end
         | 
| 11 9 |  | 
| 12 10 | 
             
                def parse(body)
         | 
| @@ -17,6 +15,8 @@ module TheTvDB | |
| 17 15 | 
             
                    true
         | 
| 18 16 | 
             
                  when 'false'
         | 
| 19 17 | 
             
                    false
         | 
| 18 | 
            +
                  when Hash
         | 
| 19 | 
            +
                    body.each { |k, v| body[k] = self.class.parser.call(v) }
         | 
| 20 20 | 
             
                  else
         | 
| 21 21 | 
             
                    self.class.parser.call body
         | 
| 22 22 | 
             
                  end
         | 
    
        data/lib/the_tv_db/response.rb
    CHANGED
    
    | @@ -7,7 +7,7 @@ module TheTvDB | |
| 7 7 | 
             
                class << self
         | 
| 8 8 | 
             
                  attr_accessor :parser
         | 
| 9 9 | 
             
                end
         | 
| 10 | 
            -
             | 
| 10 | 
            +
                
         | 
| 11 11 | 
             
                def self.define_parser(&block)
         | 
| 12 12 | 
             
                  @parser = block
         | 
| 13 13 | 
             
                end
         | 
| @@ -19,6 +19,6 @@ module TheTvDB | |
| 19 19 | 
             
                def parse_response?(env)
         | 
| 20 20 | 
             
                  env[:body].respond_to? :to_str
         | 
| 21 21 | 
             
                end
         | 
| 22 | 
            -
             | 
| 22 | 
            +
                
         | 
| 23 23 | 
             
              end # Response
         | 
| 24 24 | 
             
            end # TheTvDB
         | 
    
        data/lib/the_tv_db/series.rb
    CHANGED
    
    | @@ -19,13 +19,21 @@ module TheTvDB | |
| 19 19 | 
             
                    end
         | 
| 20 20 | 
             
                  end
         | 
| 21 21 |  | 
| 22 | 
            -
                  def find(id)
         | 
| 23 | 
            -
                     | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 22 | 
            +
                  def find(id, lang="en")
         | 
| 23 | 
            +
                    if TheTvDB.api_key
         | 
| 24 | 
            +
                      files = request("#{TheTvDB.api_key}/series/#{id}/all/#{lang}.zip")
         | 
| 25 | 
            +
                      data = files["#{lang}.xml"].fetch("Data")
         | 
| 26 | 
            +
                      record = new(data["Series"])
         | 
| 27 | 
            +
                      record.episodes = data["Episode"]
         | 
| 28 | 
            +
                      record.banners = files["banners.xml"]["Banners"]["Banner"]
         | 
| 29 | 
            +
                      record.actors = files["actors.xml"]["Actors"]["Actor"]
         | 
| 30 | 
            +
                    else
         | 
| 31 | 
            +
                      data = request("/data/series/#{id}/all/").fetch("Data")
         | 
| 32 | 
            +
                      record = new(data["Series"])
         | 
| 33 | 
            +
                      record.episodes = data["Episode"]
         | 
| 34 | 
            +
                    end
         | 
| 35 | 
            +
                    
         | 
| 26 36 | 
             
                    return record
         | 
| 27 | 
            -
                  rescue MultiXml::ParseError
         | 
| 28 | 
            -
                    raise RecordNotFound, "Couldn't find series with ID=#{id}"
         | 
| 29 37 | 
             
                  end
         | 
| 30 38 | 
             
                  alias :get :find
         | 
| 31 39 | 
             
                end
         | 
| @@ -58,7 +66,7 @@ module TheTvDB | |
| 58 66 | 
             
                  :zap2it_id        => "zap2it_id"
         | 
| 59 67 | 
             
                }.freeze
         | 
| 60 68 |  | 
| 61 | 
            -
                attr_accessor *ATTRS_MAP.keys, :episodes
         | 
| 69 | 
            +
                attr_accessor *ATTRS_MAP.keys, :episodes, :banners, :actors
         | 
| 62 70 |  | 
| 63 71 | 
             
                def episodes=(episodes)
         | 
| 64 72 | 
             
                  @episodes = case episodes
         | 
    
        data/lib/the_tv_db/version.rb
    CHANGED
    
    
| @@ -0,0 +1,73 @@ | |
| 1 | 
            +
            <?xml version="1.0" encoding="UTF-8" ?>
         | 
| 2 | 
            +
            <Actors>
         | 
| 3 | 
            +
            <Actor>
         | 
| 4 | 
            +
              <id>77049</id>
         | 
| 5 | 
            +
              <Image>actors/77049.jpg</Image>
         | 
| 6 | 
            +
              <Name>Anna Torv</Name>
         | 
| 7 | 
            +
              <Role>Olivia Dunham</Role>
         | 
| 8 | 
            +
              <SortOrder>0</SortOrder>
         | 
| 9 | 
            +
            </Actor>
         | 
| 10 | 
            +
            <Actor>
         | 
| 11 | 
            +
              <id>77045</id>
         | 
| 12 | 
            +
              <Image>actors/77045.jpg</Image>
         | 
| 13 | 
            +
              <Name>Joshua Jackson</Name>
         | 
| 14 | 
            +
              <Role>Peter Bishop</Role>
         | 
| 15 | 
            +
              <SortOrder>0</SortOrder>
         | 
| 16 | 
            +
            </Actor>
         | 
| 17 | 
            +
            <Actor>
         | 
| 18 | 
            +
              <id>77047</id>
         | 
| 19 | 
            +
              <Image>actors/77047.jpg</Image>
         | 
| 20 | 
            +
              <Name>John Noble</Name>
         | 
| 21 | 
            +
              <Role>Dr. Walter Bishop</Role>
         | 
| 22 | 
            +
              <SortOrder>1</SortOrder>
         | 
| 23 | 
            +
            </Actor>
         | 
| 24 | 
            +
            <Actor>
         | 
| 25 | 
            +
              <id>77046</id>
         | 
| 26 | 
            +
              <Image>actors/77046.jpg</Image>
         | 
| 27 | 
            +
              <Name>Jasika Nicole</Name>
         | 
| 28 | 
            +
              <Role>Astrid</Role>
         | 
| 29 | 
            +
              <SortOrder>1</SortOrder>
         | 
| 30 | 
            +
            </Actor>
         | 
| 31 | 
            +
            <Actor>
         | 
| 32 | 
            +
              <id>77044</id>
         | 
| 33 | 
            +
              <Image>actors/77044.jpg</Image>
         | 
| 34 | 
            +
              <Name>Blair Brown</Name>
         | 
| 35 | 
            +
              <Role>Nina Sharp</Role>
         | 
| 36 | 
            +
              <SortOrder>1</SortOrder>
         | 
| 37 | 
            +
            </Actor>
         | 
| 38 | 
            +
            <Actor>
         | 
| 39 | 
            +
              <id>77050</id>
         | 
| 40 | 
            +
              <Image>actors/77050.jpg</Image>
         | 
| 41 | 
            +
              <Name>Mark Valley</Name>
         | 
| 42 | 
            +
              <Role>John Scott</Role>
         | 
| 43 | 
            +
              <SortOrder>2</SortOrder>
         | 
| 44 | 
            +
            </Actor>
         | 
| 45 | 
            +
            <Actor>
         | 
| 46 | 
            +
              <id>77042</id>
         | 
| 47 | 
            +
              <Image>actors/77042.jpg</Image>
         | 
| 48 | 
            +
              <Name>Kirk Acevedo</Name>
         | 
| 49 | 
            +
              <Role>Charlie Francis</Role>
         | 
| 50 | 
            +
              <SortOrder>2</SortOrder>
         | 
| 51 | 
            +
            </Actor>
         | 
| 52 | 
            +
            <Actor>
         | 
| 53 | 
            +
              <id>284298</id>
         | 
| 54 | 
            +
              <Image>actors/284298.jpg</Image>
         | 
| 55 | 
            +
              <Name>Seth Gabel</Name>
         | 
| 56 | 
            +
              <Role>Lincoln Lee</Role>
         | 
| 57 | 
            +
              <SortOrder>2</SortOrder>
         | 
| 58 | 
            +
            </Actor>
         | 
| 59 | 
            +
            <Actor>
         | 
| 60 | 
            +
              <id>77048</id>
         | 
| 61 | 
            +
              <Image>actors/77048.jpg</Image>
         | 
| 62 | 
            +
              <Name>Lance Reddick</Name>
         | 
| 63 | 
            +
              <Role>Phillip Broyles</Role>
         | 
| 64 | 
            +
              <SortOrder>3</SortOrder>
         | 
| 65 | 
            +
            </Actor>
         | 
| 66 | 
            +
            <Actor>
         | 
| 67 | 
            +
              <id>303768</id>
         | 
| 68 | 
            +
              <Image>actors/303768.jpg</Image>
         | 
| 69 | 
            +
              <Name>Georgina Haig</Name>
         | 
| 70 | 
            +
              <Role>Etta</Role>
         | 
| 71 | 
            +
              <SortOrder>3</SortOrder>
         | 
| 72 | 
            +
            </Actor>
         | 
| 73 | 
            +
            </Actors>
         |