viki 1.0.3 → 1.1.0

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/.gitignore CHANGED
@@ -8,3 +8,4 @@ pkg/*
8
8
  .idea/
9
9
  fixtures/
10
10
  spec_config.rb
11
+ bundler_stubs/
data/lib/viki/client.rb CHANGED
@@ -7,11 +7,13 @@ require 'multi_json'
7
7
  module Viki
8
8
  class Client
9
9
  include Viki::Transport
10
+ HOST = "http://www.viki.com"
10
11
 
11
- def initialize(client_id, client_secret)
12
+ def initialize(client_id, client_secret, host = nil)
13
+ @host = host || HOST
12
14
  @client_id = client_id
13
15
  @client_secret = client_secret
14
- @access_token = auth_request(client_id, client_secret)
16
+ @access_token = auth_request(client_id, client_secret, @host)
15
17
  end
16
18
 
17
19
  attr_reader :access_token
@@ -21,7 +23,8 @@ module Viki
21
23
  raise NoMethodError if not URL_NAMESPACES.include? name
22
24
  Viki::Request.new({client_id: @client_id,
23
25
  client_secret: @client_secret,
24
- access_token: @access_token}, name, *args)
26
+ access_token: @access_token,
27
+ host: @host}, name, *args)
25
28
  end
26
29
 
27
30
  end
data/lib/viki/request.rb CHANGED
@@ -4,6 +4,7 @@ module Viki
4
4
  @access_token = auth_data[:access_token]
5
5
  @client_id = auth_data[:client_id]
6
6
  @client_secret = auth_data[:client_secret]
7
+ @host = auth_data[:host]
7
8
 
8
9
  build_call_chain(name, *args)
9
10
  end
@@ -14,11 +15,11 @@ module Viki
14
15
 
15
16
  def get
16
17
  current_chain = @call_chain
17
- request(current_chain)
18
+ request(current_chain, @host)
18
19
  end
19
20
 
20
21
  def reset_access_token
21
- @access_token = auth_request(@client_id, @client_secret)
22
+ @access_token = auth_request(@client_id, @client_secret, @host)
22
23
  end
23
24
 
24
25
  def url
@@ -8,23 +8,23 @@ module Viki
8
8
  include Viki::Utilities
9
9
 
10
10
  private
11
- HOST = "http://www.viki.com/api/v3/"
12
11
 
13
- def auth_request(client_id, client_secret)
12
+ def auth_request(client_id, client_secret, host)
13
+ endpoint = host + '/oauth/token'
14
14
  params = {
15
15
  :grant_type => 'client_credentials',
16
16
  :client_id => client_id,
17
17
  :client_secret => client_secret
18
18
  }
19
- response = HTTParty.post('http://viki.com/oauth/token', query: params)
19
+ response = HTTParty.post(endpoint, query: params)
20
20
  json = MultiJson.load(response.body)
21
21
  raise Viki::Error.new(response.header.code, json["error_description"]) if response.header.code != "200"
22
22
  json["access_token"]
23
23
  end
24
24
 
25
- def request(call_chain)
25
+ def request(call_chain, host)
26
26
  path, params = build_url(call_chain)
27
- request_url = HOST + path.chop + ".json"
27
+ request_url = host + "/api/v3/" + path.chop + ".json"
28
28
 
29
29
  response = HTTParty.get(request_url, :query => params)
30
30
 
data/lib/viki/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Viki
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/viki.rb CHANGED
@@ -9,8 +9,8 @@ module Viki
9
9
  # Alias for viki::Client.new
10
10
  #
11
11
  # @return {viki::Client})
12
- def self.new(client_id, client_secret)
13
- Viki::Client.new(client_id, client_secret)
12
+ def self.new(client_id, client_secret, host = nil)
13
+ Viki::Client.new(client_id, client_secret, host)
14
14
  end
15
15
 
16
16
  end
@@ -1,14 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Viki::Client do
4
+ before { Viki::Client.any_instance.stub(:auth_request) }
4
5
 
5
- describe "method delegation" do
6
- before { Viki::Client.any_instance.stub(:auth_request) }
7
-
8
- def client
9
- Viki::Client.new('chickenrice', 'soronerychickenrice')
10
- end
6
+ def client(domain = nil)
7
+ domain ? Viki::Client.new('foo', 'bar', host=domain) : Viki::Client.new('foo', 'bar')
8
+ end
11
9
 
10
+ describe "method delegation" do
12
11
  it "should raise NoMethodError error if part of the call chain is not part of the URL_NAMESPACES list" do
13
12
  expect { client.methodwrong.subtitles('en') }.to raise_error(NoMethodError)
14
13
  end
@@ -17,4 +16,14 @@ describe Viki::Client do
17
16
  client.movies(1234).should be_an_instance_of(Viki::Request)
18
17
  end
19
18
  end
19
+
20
+ describe "API host toggling" do
21
+ it "should set host to http://www.viki.com when no domain parameter is set" do
22
+ client.instance_variable_get(:@host).should == 'http://www.viki.com'
23
+ end
24
+
25
+ it "should set host to custom host if domain parameter is set" do
26
+ client(host = "http://test.com").instance_variable_get(:@host).should == 'http://test.com'
27
+ end
28
+ end
20
29
  end
@@ -1,6 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Viki::Request do
4
+ DEFAULT_HOST = "http://www.viki.com"
5
+
4
6
  before do
5
7
  Viki::Client.any_instance.stub(:auth_request)
6
8
  end
@@ -13,13 +15,13 @@ describe Viki::Request do
13
15
  it "should perform a request to the right url given a call chain" do
14
16
  req = client.movies(1234).subtitles('en')
15
17
  req.should_receive(:request).with([{ :name => :movies, :resource => 1234 },
16
- { :name => :subtitles, :resource => "en" }])
18
+ { :name => :subtitles, :resource => "en" }], DEFAULT_HOST)
17
19
  req.get
18
20
  end
19
21
 
20
22
  it "should perform a request with the right params given a call chain" do
21
23
  req = client.movies(1234, { genre: 2 })
22
- req.should_receive(:request).with([{ :name => :movies, :resource => 1234, :params => { genre: 2 } }])
24
+ req.should_receive(:request).with([{ :name => :movies, :resource => 1234, :params => { genre: 2 } }], DEFAULT_HOST)
23
25
  req.get
24
26
  end
25
27
 
@@ -40,14 +40,6 @@ describe "Viki" do
40
40
  end
41
41
  end
42
42
  end
43
-
44
- it "should raise an error when platform parameter is given without watchable_in" do
45
- VCR.use_cassette "movie/platform_filter_error" do
46
- query_options.merge!({ :platform => 'mobile' })
47
- lambda { results }.should raise_error(Viki::Error,
48
- "Require watchable_in parameter when given platform parameter")
49
- end
50
- end
51
43
  end
52
44
 
53
45
  describe "/movies/:id" do
@@ -93,9 +85,13 @@ describe "Viki" do
93
85
  it "should return a list of video qualities with links to hardsubbed videos" do
94
86
  VCR.use_cassette "movies/hardsubs" do
95
87
  response = client.movies(64135).hardsubs.get
96
- response.count.should == 1
88
+ response.count.should_not == 1
97
89
  hardsubs = response.content
98
- hardsubs["res-240p"].should_not be_empty
90
+ hardsubs.each do |h|
91
+ h["language_code"].should_not be_empty
92
+ h["resolution"].should_not be_empty
93
+ h["url"].should_not be_empty
94
+ end
99
95
  end
100
96
  end
101
97
  end
@@ -122,7 +118,7 @@ describe "Viki" do
122
118
  end
123
119
 
124
120
  describe "when HTTP timeout" do
125
- before { stub_request(:any, /www.viki.com/).to_return(:status => [408]) }
121
+ before { stub_request(:get, /www.viki.com/).to_return(:status => [408]) }
126
122
  it "should handle timeout gracefully" do
127
123
  expect { client.movies.get }.should raise_error(Viki::Error, "Timeout error")
128
124
  end
@@ -210,5 +206,31 @@ describe "Viki" do
210
206
  results.should be_instance_of(Viki::APIObject)
211
207
  end
212
208
  end
209
+
210
+ describe "Custom Domain" do
211
+ let(:vikiping_client_id) { "4bd5edd4ba26ac2f3ad9d204dc6359ea8a3ebe2c95b6bc1a9195c0ce5c57d392"}
212
+ let(:vikiping_client_secret) { "f3b796a1eb6e06458a502a89171a494a9160775ed4f4e9e0008c638f7e7e7d38" }
213
+ let(:custom_client) do
214
+ VCR.use_cassette "custom_auth" do
215
+ Viki.new(vikiping_client_id, vikiping_client_secret, 'http://www.vikiping.com')
216
+ end
217
+ end
218
+
219
+ context "when initialized with a domain parameter" do
220
+ it "should contact the custom domain" do
221
+ stub_request(:get, /www.vikiping.com/).to_return(:body => '{"success": "custom"}')
222
+ results = custom_client.movies.get
223
+ results.content['success'].should == 'custom'
224
+ end
225
+ end
226
+
227
+ context "when not initialized with a domain parameter" do
228
+ it "should contact the default domain" do
229
+ stub_request(:get, /www.viki.com/).to_return(:body => '{"success": "default"}')
230
+ results = client.movies.get
231
+ results.content['success'].should == 'default'
232
+ end
233
+ end
234
+ end
213
235
  end
214
236
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: viki
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-07-13 00:00:00.000000000 Z
14
+ date: 2012-09-10 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec