yt-andrewroth 0.25.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. data/.gitignore +27 -0
  2. data/.rspec +3 -0
  3. data/.travis.yml +9 -0
  4. data/.yardopts +5 -0
  5. data/CHANGELOG.md +732 -0
  6. data/Gemfile +4 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +489 -0
  9. data/Rakefile +11 -0
  10. data/YOUTUBE_IT.md +835 -0
  11. data/bin/yt +30 -0
  12. data/gemfiles/Gemfile.activesupport-3.x +4 -0
  13. data/gemfiles/Gemfile.activesupport-4.x +4 -0
  14. data/lib/yt.rb +21 -0
  15. data/lib/yt/actions/base.rb +32 -0
  16. data/lib/yt/actions/delete.rb +19 -0
  17. data/lib/yt/actions/delete_all.rb +32 -0
  18. data/lib/yt/actions/insert.rb +42 -0
  19. data/lib/yt/actions/list.rb +139 -0
  20. data/lib/yt/actions/modify.rb +37 -0
  21. data/lib/yt/actions/patch.rb +19 -0
  22. data/lib/yt/actions/update.rb +19 -0
  23. data/lib/yt/associations/has_attribute.rb +55 -0
  24. data/lib/yt/associations/has_authentication.rb +214 -0
  25. data/lib/yt/associations/has_many.rb +22 -0
  26. data/lib/yt/associations/has_one.rb +22 -0
  27. data/lib/yt/associations/has_reports.rb +320 -0
  28. data/lib/yt/collections/advertising_options_sets.rb +34 -0
  29. data/lib/yt/collections/annotations.rb +62 -0
  30. data/lib/yt/collections/assets.rb +58 -0
  31. data/lib/yt/collections/authentications.rb +47 -0
  32. data/lib/yt/collections/base.rb +62 -0
  33. data/lib/yt/collections/channels.rb +31 -0
  34. data/lib/yt/collections/claim_histories.rb +34 -0
  35. data/lib/yt/collections/claims.rb +56 -0
  36. data/lib/yt/collections/content_details.rb +30 -0
  37. data/lib/yt/collections/content_owner_details.rb +34 -0
  38. data/lib/yt/collections/content_owners.rb +32 -0
  39. data/lib/yt/collections/device_flows.rb +23 -0
  40. data/lib/yt/collections/file_details.rb +30 -0
  41. data/lib/yt/collections/ids.rb +27 -0
  42. data/lib/yt/collections/live_streaming_details.rb +30 -0
  43. data/lib/yt/collections/ownerships.rb +34 -0
  44. data/lib/yt/collections/partnered_channels.rb +28 -0
  45. data/lib/yt/collections/players.rb +30 -0
  46. data/lib/yt/collections/playlist_items.rb +53 -0
  47. data/lib/yt/collections/playlists.rb +28 -0
  48. data/lib/yt/collections/policies.rb +28 -0
  49. data/lib/yt/collections/ratings.rb +23 -0
  50. data/lib/yt/collections/references.rb +46 -0
  51. data/lib/yt/collections/related_playlists.rb +43 -0
  52. data/lib/yt/collections/reports.rb +161 -0
  53. data/lib/yt/collections/resources.rb +57 -0
  54. data/lib/yt/collections/resumable_sessions.rb +51 -0
  55. data/lib/yt/collections/snippets.rb +27 -0
  56. data/lib/yt/collections/statistics_sets.rb +30 -0
  57. data/lib/yt/collections/statuses.rb +27 -0
  58. data/lib/yt/collections/subscribed_channels.rb +46 -0
  59. data/lib/yt/collections/subscribers.rb +33 -0
  60. data/lib/yt/collections/subscriptions.rb +50 -0
  61. data/lib/yt/collections/user_infos.rb +36 -0
  62. data/lib/yt/collections/video_categories.rb +35 -0
  63. data/lib/yt/collections/videos.rb +137 -0
  64. data/lib/yt/config.rb +54 -0
  65. data/lib/yt/errors/forbidden.rb +13 -0
  66. data/lib/yt/errors/missing_auth.rb +81 -0
  67. data/lib/yt/errors/no_items.rb +13 -0
  68. data/lib/yt/errors/request_error.rb +74 -0
  69. data/lib/yt/errors/server_error.rb +13 -0
  70. data/lib/yt/errors/unauthorized.rb +50 -0
  71. data/lib/yt/models/account.rb +216 -0
  72. data/lib/yt/models/advertising_options_set.rb +38 -0
  73. data/lib/yt/models/annotation.rb +132 -0
  74. data/lib/yt/models/asset.rb +111 -0
  75. data/lib/yt/models/asset_metadata.rb +38 -0
  76. data/lib/yt/models/asset_snippet.rb +46 -0
  77. data/lib/yt/models/authentication.rb +83 -0
  78. data/lib/yt/models/base.rb +32 -0
  79. data/lib/yt/models/channel.rb +302 -0
  80. data/lib/yt/models/claim.rb +156 -0
  81. data/lib/yt/models/claim_event.rb +67 -0
  82. data/lib/yt/models/claim_history.rb +29 -0
  83. data/lib/yt/models/configuration.rb +70 -0
  84. data/lib/yt/models/content_detail.rb +65 -0
  85. data/lib/yt/models/content_owner.rb +48 -0
  86. data/lib/yt/models/content_owner_detail.rb +18 -0
  87. data/lib/yt/models/description.rb +58 -0
  88. data/lib/yt/models/device_flow.rb +16 -0
  89. data/lib/yt/models/file_detail.rb +21 -0
  90. data/lib/yt/models/id.rb +9 -0
  91. data/lib/yt/models/iterator.rb +16 -0
  92. data/lib/yt/models/live_streaming_detail.rb +23 -0
  93. data/lib/yt/models/match_policy.rb +34 -0
  94. data/lib/yt/models/ownership.rb +75 -0
  95. data/lib/yt/models/player.rb +18 -0
  96. data/lib/yt/models/playlist.rb +218 -0
  97. data/lib/yt/models/playlist_item.rb +112 -0
  98. data/lib/yt/models/policy.rb +36 -0
  99. data/lib/yt/models/policy_rule.rb +124 -0
  100. data/lib/yt/models/rating.rb +37 -0
  101. data/lib/yt/models/reference.rb +172 -0
  102. data/lib/yt/models/resource.rb +136 -0
  103. data/lib/yt/models/resumable_session.rb +52 -0
  104. data/lib/yt/models/right_owner.rb +58 -0
  105. data/lib/yt/models/snippet.rb +50 -0
  106. data/lib/yt/models/statistics_set.rb +26 -0
  107. data/lib/yt/models/status.rb +32 -0
  108. data/lib/yt/models/subscription.rb +38 -0
  109. data/lib/yt/models/timestamp.rb +13 -0
  110. data/lib/yt/models/url.rb +90 -0
  111. data/lib/yt/models/user_info.rb +26 -0
  112. data/lib/yt/models/video.rb +630 -0
  113. data/lib/yt/models/video_category.rb +12 -0
  114. data/lib/yt/request.rb +278 -0
  115. data/lib/yt/version.rb +3 -0
  116. data/spec/collections/claims_spec.rb +30 -0
  117. data/spec/collections/playlist_items_spec.rb +44 -0
  118. data/spec/collections/playlists_spec.rb +27 -0
  119. data/spec/collections/policies_spec.rb +30 -0
  120. data/spec/collections/references_spec.rb +30 -0
  121. data/spec/collections/reports_spec.rb +30 -0
  122. data/spec/collections/subscriptions_spec.rb +25 -0
  123. data/spec/collections/videos_spec.rb +43 -0
  124. data/spec/errors/forbidden_spec.rb +10 -0
  125. data/spec/errors/missing_auth_spec.rb +24 -0
  126. data/spec/errors/no_items_spec.rb +10 -0
  127. data/spec/errors/request_error_spec.rb +44 -0
  128. data/spec/errors/server_error_spec.rb +10 -0
  129. data/spec/errors/unauthorized_spec.rb +10 -0
  130. data/spec/models/account_spec.rb +138 -0
  131. data/spec/models/annotation_spec.rb +180 -0
  132. data/spec/models/asset_spec.rb +20 -0
  133. data/spec/models/channel_spec.rb +127 -0
  134. data/spec/models/claim_event_spec.rb +62 -0
  135. data/spec/models/claim_history_spec.rb +27 -0
  136. data/spec/models/claim_spec.rb +211 -0
  137. data/spec/models/configuration_spec.rb +44 -0
  138. data/spec/models/content_detail_spec.rb +45 -0
  139. data/spec/models/content_owner_detail_spec.rb +6 -0
  140. data/spec/models/description_spec.rb +94 -0
  141. data/spec/models/file_detail_spec.rb +13 -0
  142. data/spec/models/live_streaming_detail_spec.rb +6 -0
  143. data/spec/models/ownership_spec.rb +59 -0
  144. data/spec/models/player_spec.rb +13 -0
  145. data/spec/models/playlist_item_spec.rb +120 -0
  146. data/spec/models/playlist_spec.rb +138 -0
  147. data/spec/models/policy_rule_spec.rb +63 -0
  148. data/spec/models/policy_spec.rb +41 -0
  149. data/spec/models/rating_spec.rb +12 -0
  150. data/spec/models/reference_spec.rb +249 -0
  151. data/spec/models/request_spec.rb +163 -0
  152. data/spec/models/resource_spec.rb +57 -0
  153. data/spec/models/right_owner_spec.rb +71 -0
  154. data/spec/models/snippet_spec.rb +13 -0
  155. data/spec/models/statistics_set_spec.rb +13 -0
  156. data/spec/models/status_spec.rb +13 -0
  157. data/spec/models/subscription_spec.rb +30 -0
  158. data/spec/models/url_spec.rb +78 -0
  159. data/spec/models/video_category_spec.rb +21 -0
  160. data/spec/models/video_spec.rb +669 -0
  161. data/spec/requests/as_account/account_spec.rb +125 -0
  162. data/spec/requests/as_account/authentications_spec.rb +139 -0
  163. data/spec/requests/as_account/channel_spec.rb +259 -0
  164. data/spec/requests/as_account/channels_spec.rb +18 -0
  165. data/spec/requests/as_account/playlist_item_spec.rb +56 -0
  166. data/spec/requests/as_account/playlist_spec.rb +244 -0
  167. data/spec/requests/as_account/resource_spec.rb +18 -0
  168. data/spec/requests/as_account/thumbnail.jpg +0 -0
  169. data/spec/requests/as_account/video.mp4 +0 -0
  170. data/spec/requests/as_account/video_spec.rb +408 -0
  171. data/spec/requests/as_content_owner/account_spec.rb +25 -0
  172. data/spec/requests/as_content_owner/advertising_options_set_spec.rb +15 -0
  173. data/spec/requests/as_content_owner/asset_spec.rb +20 -0
  174. data/spec/requests/as_content_owner/channel_spec.rb +1934 -0
  175. data/spec/requests/as_content_owner/claim_history_spec.rb +20 -0
  176. data/spec/requests/as_content_owner/content_owner_spec.rb +241 -0
  177. data/spec/requests/as_content_owner/match_policy_spec.rb +17 -0
  178. data/spec/requests/as_content_owner/ownership_spec.rb +25 -0
  179. data/spec/requests/as_content_owner/playlist_spec.rb +782 -0
  180. data/spec/requests/as_content_owner/video_spec.rb +1239 -0
  181. data/spec/requests/as_server_app/channel_spec.rb +74 -0
  182. data/spec/requests/as_server_app/playlist_item_spec.rb +30 -0
  183. data/spec/requests/as_server_app/playlist_spec.rb +53 -0
  184. data/spec/requests/as_server_app/video_spec.rb +58 -0
  185. data/spec/requests/as_server_app/videos_spec.rb +40 -0
  186. data/spec/requests/unauthenticated/video_spec.rb +22 -0
  187. data/spec/spec_helper.rb +20 -0
  188. data/spec/support/fail_matcher.rb +15 -0
  189. data/spec/support/global_hooks.rb +48 -0
  190. data/yt.gemspec +32 -0
  191. metadata +416 -0
data/bin/yt ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'yt'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'yt'
8
+ end
9
+
10
+ Yt.configuration.log_level = :debug
11
+ id = ARGV[1] || 'BPNYv0vd78A'
12
+
13
+ case ARGV[0]
14
+ when 'info'
15
+ puts "Yt version #{Yt::VERSION}"
16
+ video = Yt::Video.new id: id
17
+ puts video.annotations.first.text
18
+ when 'annotations'
19
+ video = Yt::Video.new id: id
20
+ count = video.annotations.size
21
+ puts "Video #{id} has #{count} #{'annotation'.pluralize count}"
22
+ video.annotations.each.with_index do |annotation, i|
23
+ puts "#{i+1}) #{annotation.text}"
24
+ end
25
+ when 'video'
26
+ video = Yt::Video.new id: id
27
+ views = "#{video.view_count} #{'view'.pluralize video.view_count}"
28
+ likes = "#{video.like_count} #{'like'.pluralize video.like_count}"
29
+ puts "'#{video.title}' by #{video.channel_title} has #{views} and #{likes}"
30
+ end
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'activesupport', '~> 3.0'
4
+ gemspec path: '../'
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'activesupport', '~> 4.0'
4
+ gemspec path: '../'
data/lib/yt.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'yt/config'
2
+ require 'yt/version'
3
+ require 'yt/models/account'
4
+ require 'yt/models/channel'
5
+ require 'yt/models/claim'
6
+ require 'yt/models/claim_history'
7
+ require 'yt/models/content_owner'
8
+ require 'yt/models/match_policy'
9
+ require 'yt/models/playlist'
10
+ require 'yt/models/playlist_item'
11
+ require 'yt/models/video'
12
+ require 'yt/models/ownership'
13
+ require 'yt/models/advertising_options_set'
14
+
15
+ # An object-oriented Ruby client for YouTube.
16
+ # Helps creating applications that need to interact with YouTube objects.
17
+ # Inclused methods to access YouTube Data API V3 resources (channels, videos,
18
+ # ...), YouTube Analytics API V2 resources (metrics, earnings, ...), and
19
+ # objects not available through the API (annotations).
20
+ module Yt
21
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ module Yt
3
+ # @private
4
+ module Actions
5
+ # Abstract module that contains methods common to every action
6
+ module Base
7
+
8
+ private
9
+
10
+ # If we dropped support for ActiveSupport 3, then we could simply
11
+ # invoke transform_keys!{|key| key.to_s.underscore.to_sym}
12
+ def underscore_keys!(hash)
13
+ hash.dup.each_key{|key| hash[underscore key] = hash.delete key}
14
+ end
15
+
16
+ def underscore(value)
17
+ value.to_s.underscore.to_sym
18
+ end
19
+
20
+ # @return [Hash] the original hash with angle brackets characters in its
21
+ # values replaced with similar Unicode characters accepted by Youtube.
22
+ # @see https://support.google.com/youtube/answer/57404?hl=en
23
+ def sanitize_brackets!(source)
24
+ case source
25
+ when String then source.gsub('<', '‹').gsub('>', '›')
26
+ when Array then source.map{|string| sanitize_brackets! string}
27
+ when Hash then source.each{|k,v| source[k] = sanitize_brackets! v}
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ require 'yt/actions/modify'
2
+
3
+ module Yt
4
+ module Actions
5
+ module Delete
6
+ include Modify
7
+
8
+ private
9
+
10
+ def do_delete(extra_delete_params = {}, &block)
11
+ do_modify delete_params.merge(extra_delete_params), &block
12
+ end
13
+
14
+ def delete_params
15
+ modify_params.tap{|params| params[:method] = :delete}
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ require 'yt/actions/list'
3
+ require 'yt/request'
4
+
5
+ module Yt
6
+ module Actions
7
+ module DeleteAll
8
+ include List
9
+
10
+ private
11
+
12
+ def do_delete_all(params = {}, options = {})
13
+ list_all(params).map do |item|
14
+ item.delete options
15
+ end.tap { @items = [] }
16
+ end
17
+
18
+ def list_all(params = {})
19
+ list.find_all do |item|
20
+ params.all? do |method, value|
21
+ # TODO: could be symbol etc...
22
+ item.respond_to?(method) && case value
23
+ when Regexp then item.send(method) =~ value
24
+ when Array then item.send(method) == value.map{|item| item.to_s.gsub('<', '‹').gsub('>', '›')}
25
+ else item.send(method) == value.to_s.gsub('<', '‹').gsub('>', '›')
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,42 @@
1
+ require 'yt/request'
2
+ require 'yt/actions/base'
3
+ require 'yt/config'
4
+
5
+ module Yt
6
+ module Actions
7
+ module Insert
8
+ include Base
9
+
10
+ private
11
+
12
+ def do_insert(extra_insert_params = {})
13
+ response = insert_request(extra_insert_params).run
14
+ @items = []
15
+ new_item extract_data_from(response)
16
+ end
17
+
18
+ def insert_request(params = {})
19
+ Yt::Request.new(insert_params.deep_merge params).tap do |request|
20
+ print "#{request.as_curl}\n" if Yt.configuration.developing?
21
+ end
22
+ end
23
+
24
+ def insert_params
25
+ path = "/youtube/v3/#{self.class.to_s.demodulize.camelize :lower}"
26
+
27
+ {}.tap do |params|
28
+ params[:path] = path
29
+ params[:host] = 'www.googleapis.com'
30
+ params[:method] = :post
31
+ params[:auth] = @auth
32
+ params[:expected_response] = Net::HTTPOK
33
+ params[:api_key] = Yt.configuration.api_key if Yt.configuration.api_key
34
+ end
35
+ end
36
+
37
+ def extract_data_from(response)
38
+ response.body
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,139 @@
1
+ require 'yt/request'
2
+ require 'yt/models/iterator'
3
+ require 'yt/errors/no_items'
4
+ require 'yt/config'
5
+
6
+ module Yt
7
+ module Actions
8
+ module List
9
+ delegate :any?, :count, :each, :each_cons, :each_slice, :find, :first,
10
+ :flat_map, :map, :select, :size, to: :list
11
+
12
+ def first!
13
+ first.tap{|item| raise Errors::NoItems, error_message unless item}
14
+ end
15
+
16
+ private
17
+
18
+ def list
19
+ @last_index, @page_token = 0, nil
20
+ Yt::Iterator.new(-> {total_results}) do |items|
21
+ while next_item = find_next
22
+ items << next_item
23
+ end
24
+ @where_params = {}
25
+ end
26
+ end
27
+
28
+ # Returns the total number of items that YouTube can provide for the
29
+ # given request, either all in one page or in consecutive pages.
30
+ #
31
+ # This number comes from the 'totalResults' component of the 'pageInfo'
32
+ # which, accordingly to YouTube documentation, *does not always match
33
+ # the actual number of items in the response*.
34
+ #
35
+ # For instance, when retrieving a list of channels, 'totalResults' might
36
+ # include inactive channels, which are filtered out from the response.
37
+ #
38
+ # The only way to obtain the *real* number of returned items is to
39
+ # iterate through all the pages, which can results in many requests.
40
+ # To avoid this, +total_results+ is provided as a good size estimation.
41
+ def total_results
42
+ response = list_request(list_params).run
43
+ total_results = response.body.fetch('pageInfo', {})['totalResults']
44
+ total_results ||= extract_items(response.body).size
45
+ end
46
+
47
+ def find_next
48
+ @items ||= []
49
+ if @items[@last_index].nil? && more_pages?
50
+ more_items = next_page.map{|data| new_item data}
51
+ @items.concat more_items
52
+ end
53
+ @items[(@last_index +=1) -1]
54
+ end
55
+
56
+ def resource_class
57
+ resource_name = list_resources.singularize
58
+ require "yt/models/#{resource_name.underscore}"
59
+ "Yt::Models::#{resource_name}".constantize
60
+ end
61
+
62
+ # @return [resource_class] a new resource initialized with one
63
+ # of the items returned by asking YouTube for a list of resources.
64
+ # Can be overwritten by subclasses that initialize instance with
65
+ # a different set of parameters.
66
+ def new_item(data)
67
+ resource_class.new attributes_for_new_item(data)
68
+ end
69
+
70
+ # @private
71
+ # Can be overwritten by subclasses that initialize instance with
72
+ # a different set of parameters.
73
+ def attributes_for_new_item(data)
74
+ {data: data, auth: @auth}
75
+ end
76
+
77
+ def more_pages?
78
+ @last_index.zero? || !@page_token.nil?
79
+ end
80
+
81
+ def next_page
82
+ params = list_params.dup
83
+ params[:params][:page_token] = @page_token if @page_token
84
+ next_page = fetch_page params
85
+ @page_token = next_page[:token]
86
+ eager_load_items_from next_page[:items]
87
+ end
88
+
89
+ def eager_load_items_from(items)
90
+ items
91
+ end
92
+
93
+ def fetch_page(params = {})
94
+ @last_response = list_request(params).run
95
+ token = @last_response.body['nextPageToken']
96
+ items = extract_items @last_response.body
97
+ {items: items, token: token}
98
+ end
99
+
100
+ def list_request(params = {})
101
+ @list_request = Yt::Request.new(params).tap do |request|
102
+ print "#{request.as_curl}\n" if Yt.configuration.developing?
103
+ end
104
+ end
105
+
106
+ def error_message
107
+ {}.tap do |message|
108
+ message[:request_curl] = @list_request.as_curl
109
+ message[:response_body] = JSON(@last_response.body)
110
+ end.to_json if @list_request && @last_response
111
+ end
112
+
113
+ def list_params
114
+ path = "/youtube/v3/#{list_resources.camelize :lower}"
115
+
116
+ {}.tap do |params|
117
+ params[:method] = :get
118
+ params[:host] = 'www.googleapis.com'
119
+ params[:auth] = @auth
120
+ params[:path] = path
121
+ params[:exptected_response] = Net::HTTPOK
122
+ params[:api_key] = Yt.configuration.api_key if Yt.configuration.api_key
123
+ end
124
+ end
125
+
126
+ def extract_items(list)
127
+ list.fetch items_key, []
128
+ end
129
+
130
+ def items_key
131
+ 'items'
132
+ end
133
+
134
+ def list_resources
135
+ self.class.to_s.demodulize
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,37 @@
1
+ require 'yt/request'
2
+ require 'yt/actions/base'
3
+ require 'yt/config'
4
+
5
+ module Yt
6
+ module Actions
7
+ # Abstract module that contains methods common to Delete and Update
8
+ module Modify
9
+ include Base
10
+
11
+ private
12
+
13
+ def do_modify(params = {})
14
+ response = modify_request(params).run
15
+ yield response.body if block_given?
16
+ end
17
+
18
+ def modify_request(params = {})
19
+ Yt::Request.new(params).tap do |request|
20
+ print "#{request.as_curl}\n" if Yt.configuration.developing?
21
+ end
22
+ end
23
+
24
+ def modify_params
25
+ path = "/youtube/v3/#{self.class.to_s.demodulize.pluralize.camelize :lower}"
26
+
27
+ {}.tap do |params|
28
+ params[:path] = path
29
+ params[:auth] = @auth
30
+ params[:host] = 'www.googleapis.com'
31
+ params[:expected_response] = Net::HTTPNoContent
32
+ params[:api_key] = Yt.configuration.api_key if Yt.configuration.api_key
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ require 'yt/actions/modify'
2
+
3
+ module Yt
4
+ module Actions
5
+ module Patch
6
+ include Modify
7
+
8
+ private
9
+
10
+ def do_patch(extra_patch_params = {}, &block)
11
+ do_modify patch_params.deep_merge(extra_patch_params), &block
12
+ end
13
+
14
+ def patch_params
15
+ modify_params.tap{|params| params[:method] = :patch}
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'yt/actions/modify'
2
+
3
+ module Yt
4
+ module Actions
5
+ module Update
6
+ include Modify
7
+
8
+ private
9
+
10
+ def do_update(extra_update_params = {}, &block)
11
+ do_modify update_params.deep_merge(extra_update_params), &block
12
+ end
13
+
14
+ def update_params
15
+ modify_params.tap{|params| params[:method] = :put}
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,55 @@
1
+ require 'yt/models/timestamp'
2
+
3
+ module Yt
4
+ # @private
5
+ module Associations
6
+ # @private
7
+ module HasAttribute
8
+ def self.included(base)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ private
13
+
14
+ def type_cast(value, type)
15
+ case [type]
16
+ when [Time] then Yt::Timestamp.parse(value) if value
17
+ when [Integer] then value.to_i if value
18
+ when [Float] then value.to_f if value
19
+ when [Symbol] then value.to_sym if value
20
+ when [Hash] then value || {}
21
+ end
22
+ end
23
+
24
+ module ClassMethods
25
+ def has_attribute(attribute, options = {}, &block)
26
+ define_memoized_method(attribute) do
27
+ field = options.fetch(:from, attribute).to_s
28
+ field = field.camelize(:lower) if options.fetch(:camelize, true)
29
+ value = @data.fetch field, options[:default]
30
+ value = type_cast value, options[:type] if options[:type]
31
+ block_given? ? instance_exec(value, &block) : value
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ # A wrapper around Ruby’s +define_method+ that, in addition to adding an
38
+ # instance method called +name+, adds an instance variable called +@name+
39
+ # that stores the result of +name+ the first time is invoked, and returns
40
+ # it every other time. Especially useful if invoking +name+ takes a long
41
+ # time.
42
+ def define_memoized_method(name, &method)
43
+ ivar_name = "@#{name.to_s.gsub /[?!]$/, ''}"
44
+
45
+ define_method name do
46
+ value = instance_variable_get ivar_name
47
+ # don't ever rely on cache; it consistently causes wrong results
48
+ # (though I can't consistently reproduce the problem.. yet)
49
+ instance_variable_set ivar_name, instance_eval(&method)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end