Class: TruveoResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/truveo.rb

Overview

TruveoResponse objects are returned from Truveo methods get_videos(), get_related_categories(), get_related_channels(), get_related_tags(), and get_related_users(). For example, the following line of code creates a new TruveoResponse object as the result of a call to Truveo.get_videos().

res = t.get_videos("funny")

The video_set attribute is an array of the videos returned by the TruveoResponse.get_videos call.

res.video_set.each{|v| ... } # iterates through the videos, each video is a hash of the metadata for that video

The channel_set is a hash of the channels that match the query. The key is the name of the channel. The value is the count of the number of videos in that channel that match the query.

res.channel_set.each_pair{|key,val| ... }

The tag_set, cateogry_set, and user_set members are similar to the channel_set member described above.

The TruveoResponse also implements the each method which supports iteration through the all the videos that can be returned by the get_videos() query that created the TruveoResponse, up to 1,000 videos.

res = t.get_videos("funny")
res.each{|v| puts v['title']}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTruveoResponse

:stopdoc:



110
111
112
# File 'lib/truveo.rb', line 110

def initialize
  @vidlist = Array.new
end

Instance Attribute Details

#category_results_returnedObject

String indicating the number of category results returned

res.category_set.length == res.cagegory_results_returned.to_i # <-- true


95
96
97
# File 'lib/truveo.rb', line 95

def category_results_returned
  @category_results_returned
end

#category_setObject

Hash of categories and their related counts for your query

res.category_set # <-- hash of the matching categories


52
53
54
# File 'lib/truveo.rb', line 52

def category_set
  @category_set
end

#channel_results_returnedObject

String indicating the number of channel results returned

res.channel_set.length == res.channel_results_returned.to_i # <-- true


92
93
94
# File 'lib/truveo.rb', line 92

def channel_results_returned
  @channel_results_returned
end

#channel_setObject

Hash of channels and their related counts for your query

res.channel_set # <-- hash of the matching channels


49
50
51
# File 'lib/truveo.rb', line 49

def channel_set
  @channel_set
end

#error_codeObject

String indicating the integer code for the error if one occured



104
105
106
# File 'lib/truveo.rb', line 104

def error_code
  @error_code
end

#error_textObject

String containing text of error code if one occured.



106
107
108
# File 'lib/truveo.rb', line 106

def error_text
  @error_text
end

#first_result_positionObject

String representing the position of the first Video in the entire set of matching videos.

res.first_result_position


81
82
83
# File 'lib/truveo.rb', line 81

def first_result_position
  @first_result_position
end

#methodObject

String containing method, i.e., ‘truveo.videos.getVideos’

res.method


62
63
64
# File 'lib/truveo.rb', line 62

def method
  @method
end

#paramsObject

parameters sent to truveo api call when TruveoResponse was created



117
118
119
# File 'lib/truveo.rb', line 117

def params
  @params
end

#queryObject

String containing the query used to create this response object

res.query


65
66
67
# File 'lib/truveo.rb', line 65

def query
  @query
end

#query_suggestionObject

String containing the query suggestion, if any

res.query_suggestion


72
73
74
# File 'lib/truveo.rb', line 72

def query_suggestion
  @query_suggestion
end

#rss_urlObject

String containing the URL which will return an RSS feed for the set of videos returned in response to the submitted query.

res.rss_url


84
85
86
# File 'lib/truveo.rb', line 84

def rss_url
  @rss_url
end

#sortbyObject

String containging the sorter used to create this response

res.sortby


68
69
70
# File 'lib/truveo.rb', line 68

def sortby
  @sortby
end

#sphinxfiltersObject

sqphinx filters used



115
116
117
# File 'lib/truveo.rb', line 115

def sphinxfilters
  @sphinxfilters
end

#sphinxqueryObject

sqphinx query used



114
115
116
# File 'lib/truveo.rb', line 114

def sphinxquery
  @sphinxquery
end

#tag_results_returnedObject

String indicating the number of tag results returned

res.tag_set.length == res.tag_results_returned.to_i # <-- true


98
99
100
# File 'lib/truveo.rb', line 98

def tag_results_returned
  @tag_results_returned
end

#tag_setObject

Hash of tags and their related counts for your query

res.tag_set # <-- hash of the matching tags


55
56
57
# File 'lib/truveo.rb', line 55

def tag_set
  @tag_set
end

#total_results_availableObject

String indicating the number of total results that matched the query

res.total_results_available


75
76
77
# File 'lib/truveo.rb', line 75

def total_results_available
  @total_results_available
end

#total_results_returnedObject

String indicating the number of resutls returned in this result set. For get_videos() the following should be true: video_set.length == total_resutls_returned.to_i)

res.total_results_returned


78
79
80
# File 'lib/truveo.rb', line 78

def total_results_returned
  @total_results_returned
end

#truveoObject

truveo object used to create the results, used for next_video



118
119
120
# File 'lib/truveo.rb', line 118

def truveo
  @truveo
end

#user_results_returnedObject

String indicating the number of user results returned

res.user_set.length == res.user_results_returned.to_i # <-- true


101
102
103
# File 'lib/truveo.rb', line 101

def user_results_returned
  @user_results_returned
end

#user_setObject

Hash of users and their related counts for your query

res.user_set # <-- hash of the matching users


58
59
60
# File 'lib/truveo.rb', line 58

def user_set
  @user_set
end

#video_setObject

Array of videos returned by the query.

res.video_set # <-- array of the videos that matched query


46
47
48
# File 'lib/truveo.rb', line 46

def video_set
  @video_set
end

#video_set_titleObject

String containing a human-readable title for the set of videos returned in response to the submitted request. For example, this field would return a string such as “Most popular ‘madonna’ videos in Music on MTV” for the query ‘madonna category:Music channel:MTV sort:mostPopular’.

res.video_set_title


88
89
90
# File 'lib/truveo.rb', line 88

def video_set_title
  @video_set_title
end

Instance Method Details

#eachObject

Iterate through all the videos in the response. Each video is a hash where the key is the metadata field, like title, and the value is the actual metadata. The videos are returned in whatever order was specified by the sorter, if any, used in the query that created the TruveoResponse object.

The following goes through all the videos that match the query and prints the title. If more than one thousand videos match the query, the each method will only iterate through the first thousand.

# create a Truveo object with my app id (apply for a free app id at http://developer.truveo.com/)
t = Truveo.new("appid")
res = t.get_videos("funny")

# print lots of titles
res.each{|vid| puts vid['title']}

Note that the each method will invoke another get_videos() method behind the scenes. These calls will count against your daily limit. This means iterating through a thousand results using each will result in 100 calls to get_videos() by default.



168
169
170
171
172
173
174
175
176
# File 'lib/truveo.rb', line 168

def each #  :yields: video
  # if we've stored the results so fare
  @vidlist.each { |v| yield v }
  # get any more videos, storing the results for later calls to self.each
  while v = next_video do
    @vidlist << v
    yield v
  end
end

#next_self(res) ⇒ Object

copy over state for next_video call



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/truveo.rb', line 121

def next_self(res)
  self.video_set = res.video_set
  self.channel_set = res.channel_set
  self.category_set = res.category_set
  self.tag_set = res.tag_set
  self.user_set = res.user_set

  self.query_suggestion = res.query_suggestion
  self.total_results_available = res.total_results_available
  self.total_results_returned = res.total_results_returned
  self.first_result_position = res.first_result_position
  self.rss_url = res.rss_url
  self.video_set_title = res.video_set_title

  self.channel_results_returned = res.channel_results_returned
  self.category_results_returned = res.category_results_returned
  self.tag_results_returned = res.tag_results_returned
  self.user_results_returned = res.user_results_returned

  self.sphinxquery = res.sphinxquery
  self.sphinxfilters = res.sphinxfilters
end

#next_videoObject

get the next video, assumes a query has already been made, @video_set, @params, and @next_start have been set in get_videos_hash()



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/truveo.rb', line 181

def next_video
  return nil if video_set.nil?

  # get more video if video_set has been depleted
  if video_set.length  < 1
    next_start = first_result_position.to_i + total_results_returned.to_i      
    # check for valid start, we never return more than 1000 results
    return nil if next_start >= 1000
    # try to get another video_set
    self.params[:start] = next_start.to_s
    res = truveo.get_videos_hash(params)
    next_self(res)
    return nil if video_set.nil? || video_set.length < 1
  end

  video_set.shift
end