seasonvar 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 88429412406d0f6005fabb4776bab5e948852423
4
+ data.tar.gz: a904ad7e314d589e6118193e9cbab1d4351bd78e
5
+ SHA512:
6
+ metadata.gz: 87799b3cc4d6b5a229bc0259b2e9b0f9431136af40d2caedddf2d4134270ee788923685d6ac66a4972a8a34b43cb6ac597255b94ba5f4c19e2b125c464af8b66
7
+ data.tar.gz: d624d8504986ade33e28310e2569b758e7338cb8df9755278d5fe3f5fd40ca1fd9707ed1f48e1d27ca656506eb595a8460e103c993175385909ba1cba66f1454
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in seasonvar.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ # Seasonvar
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/seasonvar`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'seasonvar'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install seasonvar
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/seasonvar.
36
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "seasonvar"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require 'json'
2
+ require 'logger'
3
+ require 'faraday'
4
+ require 'time'
5
+ require 'uri'
6
+
7
+ module Seasonvar; end
8
+
9
+ require 'seasonvar/version'
10
+ require 'seasonvar/request_result'
11
+ require 'seasonvar/client'
@@ -0,0 +1,140 @@
1
+ # coding: utf-8
2
+ module Seasonvar
3
+ class Client
4
+ attr_reader :domain
5
+ attr_reader :scheme
6
+ attr_reader :port
7
+ attr_reader :read_timeout
8
+ attr_reader :connection_timeout
9
+ attr_reader :adapter
10
+
11
+ def initialize(params = {})
12
+ @domain = params[:domain] || 'api.seasonvar.ru'
13
+ @scheme = 'http'
14
+ @port = 80
15
+ @read_timeout = params[:read_timeout] || 60
16
+ @connection_timeout = params[:connection_timeout] || 60
17
+ @adapter = params[:adapter] || :net_http_persistent
18
+ @key = params[:key]
19
+
20
+ init_connection
21
+ end
22
+
23
+ def countries
24
+ execute(command: 'getCountryList')
25
+ end
26
+
27
+ def genres
28
+ execute(command: 'getGenreList')
29
+ end
30
+
31
+ def updates(params = {})
32
+ days = params[:days] || 7
33
+ season_info = params[:season_info] || false
34
+ execute(command: 'getUpdateList', day_count: days, seasonInfo: season_info)
35
+ end
36
+
37
+ def list_series(params = {})
38
+ data = {}
39
+ data[:country] = params[:countries] if params[:countries]
40
+ data[:genre] = params[:genres] if params[:genres]
41
+ if [:domestic, :foreign].include?(params[:locale])
42
+ data[:locale] = params[:locale]
43
+ end
44
+ if [:kinopoisk, :imdb, :popular, :year].include?(params[:order])
45
+ sort = {}
46
+ sort[:order] = params[:order]
47
+ sort[:method] = 'desc' if params[:desc]
48
+ data[:sort] = sort
49
+ end
50
+ data[:lastSeasonInfo] = true if params[:season_info]
51
+ data[:letter] = params[:prefix] if params[:prefix]
52
+ execute(data.merge(command: 'getSerialList'))
53
+ end
54
+
55
+ def series(params = {})
56
+ data = if params[:id]
57
+ { id: params[:id] }
58
+ elsif params[:name]
59
+ { name: params[:name] }
60
+ elsif params[:prefix]
61
+ { letter: params[:prefix] }
62
+ end
63
+ raise ArgumentError, 'id, name or prefix have to be specified' unless data
64
+ execute(data.merge(command: 'getSeasonList'))
65
+ end
66
+
67
+ def season(params = {})
68
+ season_id = params[:id]
69
+ raise ArgumentError, 'id have to be specified' unless season_id
70
+ execute(command: 'getSeason', season_id: season_id)
71
+ end
72
+
73
+ def search(params = {})
74
+ raise ArgumentError, 'query have to be specified' unless params[:query]
75
+ data = { command: 'search', query: params[:query] }
76
+ data[:country] = params[:countries] if params[:countries]
77
+ data[:genre] = params[:genres] if params[:genres]
78
+ execute(data)
79
+ end
80
+
81
+ private
82
+
83
+ def execute(data)
84
+ start_time = Time.now
85
+ request_content = data.merge(key: @key)
86
+ begin
87
+ response = @connection.send(:post) do |req|
88
+ req.body = URI.encode_www_form(request_content)
89
+ req.url('')
90
+ end
91
+ rescue Faraday::ClientError => e
92
+ message = e.class.name
93
+ message += ": #{e.message}" unless e.message.nil?
94
+ raise message
95
+ end
96
+ end_time = Time.now
97
+
98
+ response_raw = response.body
99
+ response_content = JSON.parse(response_raw)
100
+ raise 'Invalid JSON.' if response_content.nil? && response.status != 503
101
+ RequestResult.new(self, request_content, response_raw, response_content,
102
+ response.status, response.headers, start_time, end_time)
103
+ end
104
+
105
+ def init_connection
106
+ @connection = Faraday.new(
107
+ url: "#{scheme}://#{domain}:#{port}/",
108
+ headers: {
109
+ 'Accept-Encoding' => 'gzip,deflate',
110
+ 'Content-Type' => 'application/x-www-form-urlencoded',
111
+ 'User-Agent' => "seasonvar.rb/#{Seasonvar::VERSION}"
112
+ },
113
+ request: { timeout: read_timeout, open_timeout: connection_timeout }
114
+ ) do |conn|
115
+ conn.adapter(*Array(adapter))
116
+ conn.response :seasonvar_decode
117
+ end
118
+ end
119
+ end
120
+
121
+ class SeasonvarDecode < Faraday::Middleware # :nodoc:
122
+ def call(env)
123
+ @app.call(env).on_complete do |response_env|
124
+ raw_body = response_env[:body]
125
+ response_env[:body] =
126
+ case response_env[:response_headers]['Content-Encoding']
127
+ when 'gzip'
128
+ io = StringIO.new raw_body
129
+ Zlib::GzipReader.new(io, external_encoding: Encoding::UTF_8).read
130
+ when 'deflate'
131
+ Zlib::Inflate.inflate raw_body
132
+ else
133
+ raw_body
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ Faraday::Response.register_middleware seasonvar_decode: -> { SeasonvarDecode }
140
+ end
@@ -0,0 +1,32 @@
1
+ module Seasonvar
2
+ class RequestResult
3
+ attr_reader :client
4
+ attr_reader :request_content
5
+ attr_reader :response_raw
6
+ attr_reader :response_content
7
+ attr_reader :status_code
8
+ attr_reader :response_headers
9
+ attr_reader :start_time
10
+ attr_reader :end_time
11
+
12
+ def initialize(client, request_content, response_raw, response_content,
13
+ status_code, response_headers, start_time, end_time) # :nodoc:
14
+ @client = client
15
+ @request_content = request_content
16
+ @response_raw = response_raw
17
+ @response_content = response_content
18
+ @status_code = status_code
19
+ @response_headers = response_headers
20
+ @start_time = start_time
21
+ @end_time = end_time
22
+ end
23
+
24
+ def time_taken
25
+ end_time - start_time
26
+ end
27
+
28
+ def key
29
+ client.key
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Seasonvar
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'seasonvar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'seasonvar'
8
+ spec.version = Seasonvar::VERSION
9
+ spec.authors = ['Sergey Avseyev']
10
+ spec.email = ['sergey.avseyev@gmail.com']
11
+
12
+ spec.summary = 'Client for seasonvar.ru API'
13
+ spec.description = 'The library which allows to work with API of series aggregator seasonvar.ru'
14
+ spec.homepage = 'https://github.com/avsej/seasonvar.rb'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'faraday', '~> 0.9.0'
22
+ spec.add_runtime_dependency 'net-http-persistent', '~> 2.9'
23
+ spec.add_runtime_dependency 'json', '~> 1.8'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.12'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'minitest', '~> 5.0'
28
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seasonvar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Avseyev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-http-persistent
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ description: The library which allows to work with API of series aggregator seasonvar.ru
98
+ email:
99
+ - sergey.avseyev@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - README.md
108
+ - Rakefile
109
+ - bin/console
110
+ - bin/setup
111
+ - lib/seasonvar.rb
112
+ - lib/seasonvar/client.rb
113
+ - lib/seasonvar/request_result.rb
114
+ - lib/seasonvar/version.rb
115
+ - seasonvar.gemspec
116
+ homepage: https://github.com/avsej/seasonvar.rb
117
+ licenses: []
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.5.2
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Client for seasonvar.ru API
139
+ test_files: []