Skip to content

Commit

Permalink
Merge branch 'more-metrics'
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiob committed Jun 26, 2014
2 parents c209fa4 + 76701d8 commit 6a00607
Show file tree
Hide file tree
Showing 34 changed files with 411 additions and 319 deletions.
2 changes: 2 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
--no-private
lib/yt/modules/reports.rb
lib/**/*.rb
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
yt (0.7.3)
yt (0.7.4)
activesupport

GEM
Expand All @@ -24,7 +24,7 @@ GEM
i18n (0.6.9)
json (1.8.1)
mime-types (2.3)
minitest (5.3.4)
minitest (5.3.5)
multi_json (1.10.1)
rake (10.3.2)
rest-client (1.6.7)
Expand Down
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ v0.7 - 2014/06/18
* More status methods for PlaylistItem (privacy_status, public?, private?, unlisted?)
* Add video.update to update title, description, tags and categoryId of a video
* Sort channel.videos by most recent first
* Extract Reports (earnings, views) into module with macro `has_report`
* New channel reports: comments, likes, dislikes, shares and impressions

v0.6 - 2014/06/05
-----------------
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Use [Yt::Channel](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Models/Chan
* access the playlists of a channel
* subscribe to and unsubscribe from a channel
* create and delete playlists from a channel
* retrieve the estimated daily earnings of a channel
* retrieve the daily earnings, views, comments, likes, dislikes, shares and impressions of a channel

```ruby
channel = Yt::Channel.new id: 'UCxO1tY8h1AhOz0T4ENwmpow'
Expand Down Expand Up @@ -131,10 +131,12 @@ content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.
channel = Yt::Channel.new id: 'UCxO1tY8h1AhOz0T4ENwmpow', auth: content_owner

channel.earnings_on 5.days.ago #=> 12.23
channel.earnings until: 2.days.ago #=> {Wed, 28 May 2014 => 1.34, Thu, 29 May 2014 => 0.47}

channel.views_on 5.days.ago #=> 44
channel.views since: 3.days.ago #=> {Wed, 28 May 2014 => 12, Thu, 29 May 2014 => 3}
channel.views since: 7.days.ago #=> {Wed, 28 May 2014 => 12.0, Thu, 29 May 2014 => 3.0, …}
channel.comments until: 2.days.ago #=> {Wed, 28 May 2014 => 9.0, Thu, 29 May 2014 => 4.0, …}
channel.ch.likes from: 8.days.ago #=> {Tue, 27 May 2014 => 7.0, Wed, 28 May 2014 => 0.0, …}
channel.ch.dislikes to: 2.days.ago #=> {Tue, 27 May 2014 => 0.0, Wed, 28 May 2014 => 1.0, …}
channel.shares since: 7.days.ago, until: 7.days.ago #=> {Wed, 28 May 2014 => 3.0}
channel.impressions_on 5.days.ago #=> 157.0
```

*The methods above require to be authenticated as the channel’s content owner (see below).*
Expand Down Expand Up @@ -431,7 +433,7 @@ To install on your system, run

To use inside a bundled Ruby project, add this line to the Gemfile:

gem 'yt', '~> 0.7.3'
gem 'yt', '~> 0.7.4'

Since the gem follows [Semantic Versioning](http://semver.org),
indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
Expand Down Expand Up @@ -459,7 +461,7 @@ How to test
Yt comes with two different sets of tests:

1. tests in `spec/models`, `spec/collections` and `spec/errors` **do not hit** the YouTube API
1. tests in `spec/associations` **hit** the YouTube API and require authentication
1. tests in `spec/requests` **hit** the YouTube API and require authentication

The reason why some tests actually hit the YouTube API is because they are
meant to really integrate Yt with YouTube. YouTube API is not exactly
Expand Down
44 changes: 0 additions & 44 deletions lib/yt/associations/earnings.rb

This file was deleted.

44 changes: 0 additions & 44 deletions lib/yt/associations/views.rb

This file was deleted.

14 changes: 0 additions & 14 deletions lib/yt/collections/earnings.rb

This file was deleted.

8 changes: 2 additions & 6 deletions lib/yt/collections/reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Yt
module Collections
class Reports < Base
attr_writer :metric

def within(days_range)
@days_range = days_range
Expand All @@ -24,7 +25,7 @@ def list_params
params['filters'] = "channel==#{@parent.id}"
params['start-date'] = @days_range.begin
params['end-date'] = @days_range.end
params['metrics'] = metrics
params['metrics'] = @metric
params['dimensions'] = :day
end
end
Expand All @@ -40,11 +41,6 @@ def value_of(data)
data.last
end

# To be overriden by superclasses
# def metrics
# ''
# end

def items_key
'rows'
end
Expand Down
18 changes: 0 additions & 18 deletions lib/yt/collections/views.rb

This file was deleted.

5 changes: 3 additions & 2 deletions lib/yt/models/account.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require 'yt/models/base'
require 'yt/associations/authentications'
require 'yt/modules/authentication'

module Yt
module Models
# Provides methods to interact with YouTube accounts.
# @see https://developers.google.com/youtube/v3/guides/authentication
class Account < Base
include Associations::Authentications
# Includes methods to authenticate with YouTube API.
include Modules::Authentication

# @!attribute [r] channel
# @return [Yt::Models::Channel] the account’s channel.
Expand Down
36 changes: 3 additions & 33 deletions lib/yt/models/base.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,15 @@
require 'yt/actions/delete'
require 'yt/actions/update'
require 'yt/modules/associations'
require 'yt/errors/request_error'

require 'active_support' # does not load anything by default but is required
require 'active_support/core_ext/module/delegation' # for delegate
require 'active_support/core_ext/string/inflections' # for camelize/constantize

module Yt
module Models
class Base
extend Modules::Associations

include Actions::Delete
include Actions::Update

# @private
def self.has_many(attributes)
require "yt/collections/#{attributes}"
collection_name = attributes.to_s.sub(/.*\./, '').camelize.pluralize
collection = "Yt::Collections::#{collection_name}".constantize
define_memoized_method(attributes) { collection.of self }
end

# @private
def self.has_one(attribute)
attributes = attribute.to_s.pluralize
has_many attributes
define_memoized_method(attribute) { send(attributes).first! }
end

private

# A wrapper around Ruby’s +define_method+ that, in addition to adding an
# instance method called +name+, adds an instance variable called +@name+
# that stores the result of +name+ the first time is invoked, and returns
# it every other time. Especially useful if invoking +name+ takes a long
# time.
def self.define_memoized_method(name, &method)
define_method name do
ivar = instance_variable_get "@#{name}"
instance_variable_set "@#{name}", ivar || instance_eval(&method)
end
end
end
end

Expand Down
28 changes: 24 additions & 4 deletions lib/yt/models/channel.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
require 'yt/models/resource'
require 'yt/associations/earnings'
require 'yt/associations/views'
require 'yt/modules/reports'

module Yt
module Models
# A channel resource contains information about a YouTube channel.
# @see https://developers.google.com/youtube/v3/docs/channels
class Channel < Resource
include Associations::Earnings
include Associations::Views
# Includes the +:has_report+ method to access YouTube Analytics reports.
extend Modules::Reports

# @!attribute [r] subscriptions
# @return [Yt::Collections::Subscriptions] the channel’s subscriptions.
Expand All @@ -22,6 +21,27 @@ class Channel < Resource
# @return [Yt::Collections::Playlists] the channel’s playlists.
has_many :playlists

# @macro has_report
has_report :earnings

# @macro has_report
has_report :views

# @macro has_report
has_report :comments

# @macro has_report
has_report :likes

# @macro has_report
has_report :dislikes

# @macro has_report
has_report :shares

# @macro has_report
has_report :impressions

# @!attribute [r] statistics_set
# @return [Yt::Models::StatisticsSet] the statistics for the video.
has_one :statistics_set
Expand Down
47 changes: 47 additions & 0 deletions lib/yt/modules/associations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'active_support' # does not load anything by default but is required
require 'active_support/core_ext/module/delegation' # for delegate
require 'active_support/core_ext/string/inflections' # for camelize/constantize

module Yt
module Modules
# Associations are a set of macro-like class methods to express
# relationship between YouTube resources like "Channel has many Videos" or
# "Account has one Id". They are inspired by ActiveRecord::Associations.
module Associations
# @example Adds the +videos+ method to the Channel resource.
# class Channel < Resource
# has_many :videos
# end
def has_many(attributes)
require "yt/collections/#{attributes}"
collection_name = attributes.to_s.sub(/.*\./, '').camelize.pluralize
collection = "Yt::Collections::#{collection_name}".constantize
define_memoized_method(attributes) { collection.of self }
end

# @example Adds the +status+ method to the Video resource.
# class Video < Resource
# has_one :status
# end
def has_one(attribute)
attributes = attribute.to_s.pluralize
has_many attributes
define_memoized_method(attribute) { send(attributes).first! }
end

private

# A wrapper around Ruby’s +define_method+ that, in addition to adding an
# instance method called +name+, adds an instance variable called +@name+
# that stores the result of +name+ the first time is invoked, and returns
# it every other time. Especially useful if invoking +name+ takes a long
# time.
def define_memoized_method(name, &method)
define_method name do
ivar = instance_variable_get "@#{name}"
instance_variable_set "@#{name}", ivar || instance_eval(&method)
end
end
end
end
end
Loading

0 comments on commit 6a00607

Please sign in to comment.