yt 0.4.10 → 0.5.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.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/HISTORY.md +10 -0
  4. data/README.md +7 -3
  5. data/TODO.md +6 -2
  6. data/lib/yt/actions/delete.rb +1 -1
  7. data/lib/yt/actions/delete_all.rb +1 -1
  8. data/lib/yt/actions/insert.rb +1 -1
  9. data/lib/yt/actions/list.rb +8 -2
  10. data/lib/yt/actions/update.rb +1 -1
  11. data/lib/yt/associations.rb +1 -0
  12. data/lib/yt/associations/details_sets.rb +1 -1
  13. data/lib/yt/associations/ids.rb +20 -0
  14. data/lib/yt/associations/snippets.rb +1 -1
  15. data/lib/yt/associations/subscriptions.rb +2 -2
  16. data/lib/yt/associations/user_infos.rb +1 -1
  17. data/lib/yt/collections/base.rb +1 -0
  18. data/lib/yt/collections/details_sets.rb +0 -1
  19. data/lib/yt/collections/ids.rb +22 -0
  20. data/lib/yt/collections/playlist_items.rb +3 -3
  21. data/lib/yt/collections/playlists.rb +0 -1
  22. data/lib/yt/collections/snippets.rb +0 -1
  23. data/lib/yt/collections/subscriptions.rb +4 -3
  24. data/lib/yt/collections/user_infos.rb +0 -2
  25. data/lib/yt/collections/videos.rb +0 -1
  26. data/lib/yt/errors/base.rb +43 -0
  27. data/lib/yt/errors/error.rb +8 -0
  28. data/lib/yt/errors/failed.rb +17 -0
  29. data/lib/yt/errors/no_items.rb +17 -0
  30. data/lib/yt/errors/unauthenticated.rb +34 -0
  31. data/lib/yt/models/account.rb +1 -17
  32. data/lib/yt/models/base.rb +1 -0
  33. data/lib/yt/models/description.rb +11 -35
  34. data/lib/yt/models/id.rb +4 -0
  35. data/lib/yt/models/request.rb +102 -0
  36. data/lib/yt/models/resource.rb +13 -2
  37. data/lib/yt/models/subscription.rb +3 -2
  38. data/lib/yt/models/url.rb +88 -0
  39. data/lib/yt/version.rb +1 -1
  40. data/spec/associations/device_auth/details_sets_spec.rb +1 -1
  41. data/spec/associations/device_auth/ids_spec.rb +19 -0
  42. data/spec/associations/device_auth/playlist_items_spec.rb +3 -3
  43. data/spec/associations/device_auth/snippets_spec.rb +2 -2
  44. data/spec/associations/device_auth/user_infos_spec.rb +7 -2
  45. data/spec/associations/server_auth/details_sets_spec.rb +4 -4
  46. data/spec/associations/server_auth/ids_spec.rb +18 -0
  47. data/spec/associations/server_auth/snippets_spec.rb +2 -2
  48. data/spec/collections/playlist_items_spec.rb +25 -5
  49. data/spec/collections/subscriptions_spec.rb +6 -4
  50. data/spec/errors/failed_spec.rb +9 -0
  51. data/spec/errors/no_items_spec.rb +9 -0
  52. data/spec/errors/unauthenticated_spec.rb +9 -0
  53. data/spec/models/account_spec.rb +0 -0
  54. data/spec/models/description_spec.rb +2 -2
  55. data/spec/models/request_spec.rb +29 -0
  56. data/spec/models/resource_spec.rb +21 -0
  57. data/spec/models/subscription_spec.rb +6 -4
  58. data/spec/models/url_spec.rb +72 -0
  59. data/spec/support/fail_matcher.rb +14 -0
  60. metadata +32 -4
  61. data/lib/yt/actions/request.rb +0 -112
  62. data/lib/yt/actions/request_error.rb +0 -11
@@ -1,112 +0,0 @@
1
- require 'net/http' # for Net::HTTP.start
2
- require 'uri' # for URI.json
3
- require 'json' # for JSON.parse
4
- require 'active_support/core_ext' # for Hash.from_xml, Hash.to_param
5
-
6
- require 'yt/actions/request_error'
7
- require 'yt/config'
8
-
9
- module Yt
10
- class Request
11
- def initialize(options = {})
12
- options[:query] ||= options[:params].to_param
13
- @uri = URI::HTTPS.build options.slice(:host, :path, :query)
14
- @method = options[:method]
15
- @format = options[:format]
16
- @scope = options[:scope]
17
- @body = options[:body]
18
- @body_type = options[:body_type]
19
- @auth = options[:auth]
20
- @headers = {}
21
- end
22
-
23
- def run
24
- add_authorization_to_request! if requires_authorization?
25
- fetch_response.tap do |response|
26
- response.body = parse_format response.body if response.body
27
- # puts "You can try again running #{to_curl}"
28
- raise RequestError, response.body unless response.is_a? Net::HTTPSuccess
29
- end
30
- end
31
-
32
- def self.default_params
33
- {}.tap do |params|
34
- params[:format] = :json
35
- params[:host] = 'www.googleapis.com'
36
- params[:scope] = 'https://www.googleapis.com/auth/youtube'
37
- params[:body_type] = :json
38
- end
39
- end
40
-
41
- private
42
-
43
- def add_authorization_to_request!
44
- if @auth.respond_to? :access_token_for
45
- @headers['Authorization'] = "Bearer #{@auth.access_token_for @scope}"
46
- elsif Yt.configuration.api_key
47
- params = URI.decode_www_form @uri.query || ''
48
- params << [:key, Yt.configuration.api_key]
49
- @uri.query = URI.encode_www_form params
50
- else
51
- raise RequestError, missing_credentials
52
- end
53
- end
54
-
55
- def requires_authorization?
56
- @uri.host == Request.default_params[:host]
57
- end
58
-
59
- def fetch_response
60
- Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
61
- klass = "Net::HTTP::#{@method.capitalize}".constantize
62
- request = klass.new @uri.request_uri
63
- case @body_type
64
- when :json
65
- request.initialize_http_header 'Content-Type' => 'application/json'
66
- request.initialize_http_header 'Content-length' => '0' unless @body
67
- request.body = @body.to_json if @body
68
- when :form
69
- request.set_form_data @body if @body
70
- end
71
- @headers.each{|k,v| request.add_field k, v}
72
-
73
- http.request request
74
- end
75
- end
76
-
77
- def parse_format(body)
78
- case @format
79
- when :xml then Hash.from_xml body
80
- when :json then JSON body
81
- end
82
- end
83
-
84
- def missing_credentials
85
- <<-MSG
86
- In order to perform this request, you need to register your app with the
87
- Google Developers Console (https://console.developers.google.com).
88
-
89
- Make sure your app has access to the Google+ and YouTube APIs.
90
- Generate a client ID, client secret and server API key, then pass their
91
- values to Yt. One way of doing this is through an initializer:
92
-
93
- Yt.configure do |config|
94
- config.client_id = '1234567890.apps.googleusercontent.com'
95
- config.client_secret = '1234567890'
96
- config.api_key = '123456789012345678901234567890'
97
- end
98
-
99
- An alternative (but equivalent) way is throught environment variables:
100
-
101
- export YT_CLIENT_ID="1234567890.apps.googleusercontent.com"
102
- export YT_CLIENT_SECRET="1234567890"
103
- export YT_API_KEY="123456789012345678901234567890"
104
-
105
- MSG
106
- end
107
-
108
- # def to_curl
109
- # %Q{curl -X #{@method.upcase} "#{@uri}"}
110
- # end
111
- end
112
- end
@@ -1,11 +0,0 @@
1
- module Yt
2
- class RequestError < StandardError
3
- def reasons
4
- error.fetch('errors', []).map{|e| e['reason']}
5
- end
6
-
7
- def error
8
- eval(message)['error'] rescue {}
9
- end
10
- end
11
- end