vhx-ruby 0.0.2

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.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +2 -0
  5. data/README.md +59 -0
  6. data/Rakefile +1 -0
  7. data/lib/vhx.rb +66 -0
  8. data/lib/vhx/client.rb +86 -0
  9. data/lib/vhx/error.rb +24 -0
  10. data/lib/vhx/middleware/error_response.rb +32 -0
  11. data/lib/vhx/middleware/oauth2.rb +22 -0
  12. data/lib/vhx/oauth_token.rb +11 -0
  13. data/lib/vhx/objects/authorization.rb +5 -0
  14. data/lib/vhx/objects/collection.rb +8 -0
  15. data/lib/vhx/objects/collection_item.rb +4 -0
  16. data/lib/vhx/objects/customer.rb +8 -0
  17. data/lib/vhx/objects/product.rb +6 -0
  18. data/lib/vhx/objects/site.rb +4 -0
  19. data/lib/vhx/objects/user.rb +10 -0
  20. data/lib/vhx/objects/video.rb +7 -0
  21. data/lib/vhx/objects/video_file.rb +4 -0
  22. data/lib/vhx/utilities/api_operations/create.rb +22 -0
  23. data/lib/vhx/utilities/api_operations/delete.rb +16 -0
  24. data/lib/vhx/utilities/api_operations/list.rb +21 -0
  25. data/lib/vhx/utilities/api_operations/request.rb +17 -0
  26. data/lib/vhx/utilities/api_operations/update.rb +15 -0
  27. data/lib/vhx/utilities/vhx_helper.rb +20 -0
  28. data/lib/vhx/utilities/vhx_list_object.rb +44 -0
  29. data/lib/vhx/utilities/vhx_object.rb +106 -0
  30. data/lib/vhx/version.rb +3 -0
  31. data/spec/client_spec.rb +69 -0
  32. data/spec/fixtures/sample_array_list.json +15 -0
  33. data/spec/fixtures/sample_file_response.json +18 -0
  34. data/spec/fixtures/sample_hash_list.json +23 -0
  35. data/spec/fixtures/sample_package_response.json +185 -0
  36. data/spec/fixtures/sample_site_response.json +26 -0
  37. data/spec/fixtures/sample_user_response.json +21 -0
  38. data/spec/fixtures/sample_video_response.json +51 -0
  39. data/spec/middleware/error_response_spec.rb +11 -0
  40. data/spec/middleware/oauth2_spec.rb +14 -0
  41. data/spec/objects/file_spec.rb +29 -0
  42. data/spec/objects/package_spec.rb +74 -0
  43. data/spec/objects/site_spec.rb +44 -0
  44. data/spec/objects/user_spec.rb +51 -0
  45. data/spec/objects/video_spec.rb +36 -0
  46. data/spec/spec_helper.rb +18 -0
  47. data/spec/test_data.rb +55 -0
  48. data/spec/utilities/vhx_collection_spec.rb +27 -0
  49. data/spec/utilities/vhx_helper_spec.rb +47 -0
  50. data/spec/utilities/vhx_object_spec.rb +53 -0
  51. data/spec/vcr/Vhx_Client/application_user/_refresh_access_token/access_token_changed.yml +57 -0
  52. data/spec/vcr/Vhx_Client/application_user/_refresh_access_token/oauth_token_refreshed.yml +57 -0
  53. data/spec/vcr/Vhx_File/associations/are_present.yml +409 -0
  54. data/spec/vcr/Vhx_Middleware_ErrorResponse/unauthorized_user_credentials.yml +122 -0
  55. data/spec/vcr/Vhx_Middleware_OAuth2/access_token_refresh.yml +797 -0
  56. data/spec/vcr/Vhx_Package/_add_video/with_hypermedia/returns_package_object.yml +51 -0
  57. data/spec/vcr/Vhx_Package/_add_video/with_id/returns_package_object.yml +51 -0
  58. data/spec/vcr/Vhx_Package/_create/returns_package_object.yml +140 -0
  59. data/spec/vcr/Vhx_Package/_find/with_hypermedia.yml +243 -0
  60. data/spec/vcr/Vhx_Package/_find/with_id.yml +243 -0
  61. data/spec/vcr/Vhx_Package/_remove_video/with_hypermedia/returns_success.yml +51 -0
  62. data/spec/vcr/Vhx_Package/_remove_video/with_id/returns_success.yml +51 -0
  63. data/spec/vcr/Vhx_Package/associations/are_present.yml +195 -0
  64. data/spec/vcr/Vhx_Site/_create/returns_site_object.yml +91 -0
  65. data/spec/vcr/Vhx_Site/_find/with_hypermedia.yml +195 -0
  66. data/spec/vcr/Vhx_Site/_find/with_id.yml +195 -0
  67. data/spec/vcr/Vhx_User/_find/with_hypermedia.yml +81 -0
  68. data/spec/vcr/Vhx_User/_find/with_id.yml +81 -0
  69. data/spec/vcr/Vhx_User/_me/returns_user_object.yml +720 -0
  70. data/spec/vcr/Vhx_User/_update/returns_user_object.yml +212 -0
  71. data/spec/vcr/Vhx_VhxObject/associations/cache/retreive_if_available.yml +195 -0
  72. data/spec/vcr/Vhx_VhxObject/associations/falls_back_to_links.yml +310 -0
  73. data/spec/vcr/Vhx_Video/_create/returns_video_object.yml +125 -0
  74. data/spec/vcr/Vhx_Video/associations/are_present.yml +294 -0
  75. data/vhx.gemspec +27 -0
  76. metadata +204 -0
@@ -0,0 +1,26 @@
1
+ {
2
+ "_links": {
3
+ "self": { "href": "https://api.vhx.tv/sites/1" },
4
+ "home_page": { "href": "http://www.myproject.com" },
5
+ "followers": { "href": "https://api.vhx.tv/sites/1/followers" },
6
+ "subscription": { "href": "https://api.vhx.tv/subscriptions/1" }
7
+ },
8
+ "_embedded": {
9
+ "packages": [],
10
+ "subscription": {}
11
+ },
12
+ "id": 1,
13
+ "title": "My Project",
14
+ "description": "My project description.",
15
+ "domain": "www.myproject.com",
16
+ "subdomain": "myproject",
17
+ "key": "myproject",
18
+ "color": "#000000",
19
+ "facebook_url": "https://facebook.com/ksheurs",
20
+ "twitter_name": "ksheurs",
21
+ "packages_count": 5,
22
+ "videos_count": 10,
23
+ "followers_count": 100,
24
+ "created_at": "2014-02-25T20:19:30Z",
25
+ "updated_at": "2014-02-25T20:19:30Z"
26
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "_links": {
3
+ "self": { "href": "http://api.crystal.dev/users/1560703" }
4
+ },
5
+ "_embedded": {
6
+ "packages": [],
7
+ "sites": []
8
+ },
9
+ "id": 1560703,
10
+ "name": "Random User",
11
+ "has_password": true,
12
+ "thumbnail": {
13
+ "small": "https://cdn.vhx.tv/assets/thumbnails/default-portrait-small.png",
14
+ "medium": "https://cdn.vhx.tv/assets/thumbnails/default-portrait-medium.png",
15
+ "large": "https://cdn.vhx.tv/assets/thumbnails/default-portrait-large.png"
16
+ },
17
+ "packages_count": 5,
18
+ "sites_count": 1,
19
+ "created_at": "2014-02-25T20:19:30Z",
20
+ "updated_at": "2014-02-25T20:19:30Z"
21
+ }
@@ -0,0 +1,51 @@
1
+ {
2
+ "_links": {
3
+ "self": { "href": "http://api.crystal.dev/videos/1" },
4
+ "site": { "href": "http://api.crystal.dev/sites/1" },
5
+ "files": { "href": "http://api.crystal.dev/videos/1/files" }
6
+ },
7
+ "_embedded": {
8
+ "site": {},
9
+ "subtitles": [{
10
+ "_links": {
11
+ "self": { "href": "http://cdn.crystal.dev/assets/spanish.srt" },
12
+ "video": { "href": "http://api.crystal.dev/videos/1" },
13
+ "site": { "href": "http://api.crystal.dev/sites/1" }
14
+ },
15
+ "language": "Spanish",
16
+ "type": "srt"
17
+ }],
18
+ "files": [{
19
+ "_links": {
20
+ "self": { "href": "http://api.crystal.dev/videos/1/files?quality=adaptive&format=m3u8" },
21
+ "video": { "href": "http://api.crystal.dev/videos/1" },
22
+ "site": { "href": "http://api.crystal.dev/sites/1" }
23
+ },
24
+ "quality": "adaptive",
25
+ "format": "m3u8",
26
+ "method": "hls",
27
+ "size": {
28
+ "bytes": 1073741824,
29
+ "formatted": "1 GB"
30
+ },
31
+ "mime_type": "application/x-mpegURL"
32
+ }]
33
+ },
34
+ "id": 1,
35
+ "title": "My Video",
36
+ "description": "My video description.",
37
+ "status": "uploaded",
38
+ "duration": {
39
+ "seconds": 7200,
40
+ "formatted": "02:00:00"
41
+ },
42
+ "thumbnail": {
43
+ "small": "https://cdn.vhx.tv/assets/thumbnails/default-small.png",
44
+ "medium": "https://cdn.vhx.tv/assets/thumbnails/default-medium.png",
45
+ "large": "https://cdn.vhx.tv/assets/thumbnails/default-large.png"
46
+ },
47
+ "subtitles_count": 3,
48
+ "files_count": 5,
49
+ "created_at": "2014-02-25T20:19:30Z",
50
+ "updated_at": "2014-02-25T20:19:30Z"
51
+ }
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vhx::Middleware::ErrorResponse, :vcr do
4
+ before{
5
+ Vhx.setup(unauthorized_user_credentials)
6
+ }
7
+
8
+ it 'unauthorized_user_credentials' do
9
+ expect{Vhx::User.me}.to raise_error(Vhx::UnauthorizedError)
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vhx::Middleware::OAuth2, :vcr do
4
+ before{
5
+ Vhx.setup(expired_credentials)
6
+ }
7
+
8
+ it 'access_token refresh' do
9
+ original_access_token = Vhx.client.oauth_token.access_token
10
+ Vhx::Client.any_instance.should_receive(:refresh_access_token!).and_call_original
11
+ Vhx::User.me
12
+ expect(Vhx.client.oauth_token.access_token).to_not eq original_access_token
13
+ end
14
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vhx::File, :vcr do
4
+ let(:sample_file_response){ JSON.parse(File.read("spec/fixtures/sample_file_response.json")) }
5
+ let(:vhx_file){Vhx::File.new(sample_file_response)}
6
+
7
+ before {
8
+ Vhx.setup(test_credentials)
9
+ }
10
+
11
+ describe 'attributes' do
12
+ it 'are present' do
13
+ expect(vhx_file.created_at).to_not be_nil
14
+ expect(vhx_file.quality).to_not be_nil
15
+ expect(vhx_file.mime_type).to_not be_nil
16
+ end
17
+ end
18
+
19
+ describe 'associations' do
20
+ it 'are present' do
21
+ expect(vhx_file.video).to_not raise_error(NoMethodError)
22
+ expect(vhx_file.site).to_not raise_error(NoMethodError)
23
+ end
24
+
25
+ it 'errors if not present' do
26
+ expect{vhx_file.file}.to raise_error(NoMethodError)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vhx::Package, :vcr do
4
+ let(:sample_package_response){ JSON.parse(File.read("spec/fixtures/sample_package_response.json")) }
5
+ let(:vhx_package){Vhx::Package.new(sample_package_response)}
6
+
7
+ before {
8
+ Vhx.setup(test_credentials)
9
+ }
10
+
11
+ describe '::find' do
12
+ it 'with id' do
13
+ expect(Vhx::Package.find(1025)).to be_instance_of(Vhx::Package)
14
+ end
15
+
16
+ it 'with hypermedia' do
17
+ expect(Vhx::Package.find('http://api.crystal.dev/packages/1025')).to be_instance_of(Vhx::Package)
18
+ end
19
+ end
20
+
21
+ describe '::create' do
22
+ it 'returns package object' do
23
+ attributes = {title: 'test package', description: 'test description', price_cents: 1000, site: "http://api.crystal.dev/sites/1900"}
24
+ expect(Vhx::Package.create(attributes)).to be_instance_of(Vhx::Package)
25
+ end
26
+ end
27
+
28
+ describe '#add_video' do
29
+ context 'with id' do
30
+ it 'returns package object' do
31
+ expect(vhx_package.add_video(7830)).to be_instance_of(Vhx::Package)
32
+ end
33
+ end
34
+
35
+ context 'with hypermedia' do
36
+ it 'returns package object' do
37
+ expect(vhx_package.add_video('http://api.crystal.dev/videos/7830')).to be_instance_of(Vhx::Package)
38
+ end
39
+ end
40
+ end
41
+
42
+ describe '#remove_video' do
43
+ context 'with id' do
44
+ it 'returns success' do
45
+ expect(vhx_package.remove_video(7726)).to be_instance_of(Vhx::Package)
46
+ end
47
+ end
48
+
49
+ context 'with hypermedia' do
50
+ it 'returns success' do
51
+ expect(vhx_package.remove_video('http://api.crystal.dev/videos/7726')).to be_instance_of(Vhx::Package)
52
+ end
53
+ end
54
+ end
55
+
56
+ describe 'attributes' do
57
+ it 'are present' do
58
+ expect(vhx_package.id).to_not be_nil
59
+ expect(vhx_package.title).to_not be_nil
60
+ end
61
+ end
62
+
63
+ describe 'associations' do
64
+ it 'are present' do
65
+ expect{vhx_package.site}.to_not raise_error(NoMethodError)
66
+ expect{vhx_package.videos}.to_not raise_error(NoMethodError)
67
+ end
68
+
69
+ it 'errors if not present' do
70
+ expect{vhx_package.package}.to raise_error(NoMethodError)
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vhx::Site, :vcr do
4
+ let(:sample_site_response){ JSON.parse(File.read("spec/fixtures/sample_site_response.json")) }
5
+ let(:vhx_site){Vhx::Site.new(sample_site_response)}
6
+
7
+ before {
8
+ Vhx.setup(test_credentials)
9
+ }
10
+
11
+ describe '::find' do
12
+ it 'with id' do
13
+ expect(Vhx::Site.find(1900)).to be_instance_of(Vhx::Site)
14
+ end
15
+
16
+ it 'with hypermedia' do
17
+ expect(Vhx::Site.find('http://api.crystal.dev/sites/1900')).to be_instance_of(Vhx::Site)
18
+ end
19
+ end
20
+
21
+ describe '::create' do
22
+ it 'returns site object' do
23
+ attributes = {title: 'Gem New Site', description: 'For testing gem'}
24
+ expect(Vhx::Site.create(attributes)).to be_instance_of(Vhx::Site)
25
+ end
26
+ end
27
+
28
+ describe 'attributes' do
29
+ it 'are present' do
30
+ expect(vhx_site.id).to_not be_nil
31
+ expect(vhx_site.title).to_not be_nil
32
+ end
33
+ end
34
+
35
+ describe 'associations' do
36
+ it 'are present' do
37
+ expect{vhx_site.packages}.to_not raise_error(NoMethodError)
38
+ end
39
+
40
+ it 'errors if not present' do
41
+ expect{vhx_site.site}.to raise_error(NoMethodError)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vhx::User do
4
+ let(:sample_user_response){ JSON.parse(File.read("spec/fixtures/sample_user_response.json")) }
5
+ let(:vhx_user){Vhx::User.new(sample_user_response)}
6
+
7
+ before {
8
+ Vhx.setup(test_credentials)
9
+ }
10
+
11
+ describe '::find', :vcr do
12
+ it 'with id' do
13
+ expect(Vhx::User.find(1560703)).to be_instance_of(Vhx::User)
14
+ end
15
+
16
+ it 'with hypermedia' do
17
+ expect(Vhx::User.find('http://api.crystal.dev/users/1560703')).to be_instance_of(Vhx::User)
18
+ end
19
+ end
20
+
21
+ describe '::me', :vcr do
22
+ it 'returns user object' do
23
+ expect(Vhx::User.me).to be_instance_of(Vhx::User)
24
+ end
25
+ end
26
+
27
+ describe '#update', :vcr do
28
+ it 'returns user object' do
29
+ vhx_user.update(name: 'Test Name')
30
+ expect(vhx_user.name).to eq 'Test Name'
31
+ end
32
+ end
33
+
34
+ describe 'attributes' do
35
+ it 'are present' do
36
+ expect(vhx_user.id).to_not be_nil
37
+ expect(vhx_user.name).to_not be_nil
38
+ end
39
+ end
40
+
41
+ describe 'associations' do
42
+ it 'are present' do
43
+ expect{vhx_user.packages}.to_not raise_error(NoMethodError)
44
+ expect{vhx_user.sites}.to_not raise_error(NoMethodError)
45
+ end
46
+
47
+ it 'errors if not present' do
48
+ expect{vhx_user.users}.to raise_error(NoMethodError)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vhx::Video, :vcr do
4
+ let(:sample_video_response){ JSON.parse(File.read("spec/fixtures/sample_video_response.json")) }
5
+ let(:vhx_video){Vhx::Video.new(sample_video_response)}
6
+
7
+ before {
8
+ Vhx.setup(test_credentials)
9
+ }
10
+
11
+ describe '::create' do
12
+ it 'returns video object' do
13
+ attributes = {title: 'gem test video', description: 'this video is for gem testing', site: 'http://api.crystal.dev/sites/1900'}
14
+ expect(Vhx::Video.create(attributes)).to be_instance_of(Vhx::Video)
15
+ end
16
+ end
17
+
18
+ describe 'attributes' do
19
+ it 'are present' do
20
+ expect(vhx_video.id).to_not be_nil
21
+ expect(vhx_video.title).to_not be_nil
22
+ end
23
+ end
24
+
25
+ describe 'associations' do
26
+ it 'are present' do
27
+ expect{vhx_video.files}.to_not raise_error(NoMethodError)
28
+ expect{vhx_video.site}.to_not raise_error(NoMethodError)
29
+ end
30
+
31
+ it 'errors if not present' do
32
+ expect{vhx_video.videos}.to raise_error(NoMethodError)
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,18 @@
1
+ require 'vhx'
2
+ require 'vcr'
3
+ require 'json'
4
+ require 'test_data'
5
+
6
+ VCR.configure do |vcr_config|
7
+ vcr_config.cassette_library_dir = 'spec/vcr'
8
+ vcr_config.hook_into :webmock
9
+ vcr_config.configure_rspec_metadata!
10
+ end
11
+
12
+ RSpec.configure do |config|
13
+ config.include TestData
14
+ config.treat_symbols_as_metadata_keys_with_true_values = true
15
+ config.run_all_when_everything_filtered = true
16
+ config.filter_run :focus
17
+ config.order = 'random'
18
+ end
@@ -0,0 +1,55 @@
1
+ module TestData
2
+ def test_credentials
3
+ {
4
+ client_id: "34ac1dec21fc20afd5099a2e6e0e62745dead7acd20c7b87320d8e5a7c5a33cc",
5
+ client_secret: "6a04e2cf039db7ff57850f677fe0e419cdc132b3cc04aac9f3c2cd1c193a326d",
6
+ oauth_token: {
7
+ access_token: "cc7d6c93bde201968f077b73acd5ee11a2768cbcb9b43122eb294004bf5b4480",
8
+ refresh_token: "3b9aacbeb03d05e7b901c35580bcc45e6e714cd4921d8bc8699342632f68bd63",
9
+ expires_in: 7200
10
+ }
11
+ }
12
+ end
13
+
14
+ def application_only_credentials
15
+ {api_key: "-HyMMMvuWsXSezKvYFJ1N_1xRe9dymeh"}
16
+ end
17
+
18
+ def application_user_credentials
19
+ {
20
+ client_id: '7ed37300580e27f1acb5f16112e91aba43931d1e8e8c9829cc0fc36358f2cd44',
21
+ client_secret: 'ce0fc20cad4cb74b4e5c30803446066b6d8556f413217a6a102aaab03372ac77',
22
+ oauth_token: {
23
+ access_token: "5eb4f0d3f9e6ef8a3afdf7f3d8288d5067d626f1904b73b5695e4f65104be1c2",
24
+ refresh_token: "132916e95bacc977a7890b31bf608e3d641d8a5c19b4ff4ffcf50e5b4273ed99",
25
+ expires_at: 1430330123,
26
+ expires_in: 7200
27
+ }
28
+ }
29
+ end
30
+
31
+ def unauthorized_user_credentials
32
+ {
33
+ client_id: 'unauthorized',
34
+ client_secret: 'unauthorized',
35
+ oauth_token: {
36
+ access_token: "unauthorized",
37
+ refresh_token: "unauthorized",
38
+ expires_at: 1430330123,
39
+ expires_in: 1
40
+ }
41
+ }
42
+ end
43
+
44
+ def expired_credentials
45
+ {
46
+ client_id: "34ac1dec21fc20afd5099a2e6e0e62745dead7acd20c7b87320d8e5a7c5a33cc",
47
+ client_secret: "6a04e2cf039db7ff57850f677fe0e419cdc132b3cc04aac9f3c2cd1c193a326d",
48
+ oauth_token: {
49
+ access_token: "cc7d6c93bde201968f077b73acd5ee11a2768cbcb9b43122eb294004bf5b4480",
50
+ refresh_token: "3b9aacbeb03d05e7b901c35580bcc45e6e714cd4921d8bc8699342632f68bd63",
51
+ expires_in: 1
52
+ }
53
+ }
54
+ end
55
+ end