Module: YouTubeIt::Request::FieldSearch

Included in:
UserSearch, VideoSearch
Defined in:
lib/youtube_it/request/base_search.rb

Instance Method Summary collapse

Instance Method Details

#default_fieldsObject



27
28
29
# File 'lib/youtube_it/request/base_search.rb', line 27

def default_fields
  "id,updated,openSearch:totalResults,openSearch:startIndex,openSearch:itemsPerPage"
end

#fields_to_params(fields) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/youtube_it/request/base_search.rb', line 31

def fields_to_params(fields)
  return "" unless fields

  fields_param = [default_fields]

  if fields[:recorded]
    if fields[:recorded].is_a? Range
      fields_param << "entry[xs:date(yt:recorded) > xs:date('#{formatted_date(fields[:recorded].first)}') and xs:date(yt:recorded) < xs:date('#{formatted_date(fields[:recorded].last)}')]"
    else
      fields_param << "entry[xs:date(yt:recorded) = xs:date('#{formatted_date(fields[:recorded])}')]"
    end
  end

  if fields[:published]
    if fields[:published].is_a? Range
      fields_param << "entry[xs:dateTime(published) > xs:dateTime('#{formatted_date(fields[:published].first)}T00:00:00') and xs:dateTime(published) < xs:dateTime('#{formatted_date(fields[:published].last)}T00:00:00')]"
    else
      fields_param << "entry[xs:date(published) = xs:date('#{formatted_date(fields[:published])}')]"
    end
  end

  if fields[:view_count]
    fields_param << "entry[yt:statistics/@viewCount > #{fields[:view_count]}]"
  end


  return "&fields=#{URI.escape(fields_param.join(","))}"
end

#formatted_date(date) ⇒ Object

youtube taked dates that look like ‘YYYY-MM-DD’



61
62
63
64
65
66
67
68
# File 'lib/youtube_it/request/base_search.rb', line 61

def formatted_date(date)
  return date if date.is_a? String
  if date.respond_to? :strftime
    date.strftime("%Y-%m-%d")
  else
    ""
  end
end