Skip to content

Commit

Permalink
Merge pull request #4 from tristaneuan/feat/top_result
Browse files Browse the repository at this point in the history
Add a config option to return the top result for a video search
  • Loading branch information
t33chong committed Jun 8, 2015
2 parents cffdecd + 3c8038d commit 03d9f8f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -23,11 +23,13 @@ end
### Optional attributes
* `video_info` (boolean) - When set to `true`, Lita will return additional information (title, duration, etc.) about the video. Default: `false`
* `detect_urls` (boolean) - When set to `true`, Lita will return additional information about any YouTube URLs it detects. Default: `false`
* `top_result` (boolean) - When set to `true`, Lita will return the top result in response to a video search. Default: `false`, which returns a random result selected from the top 15.

``` ruby
Lita.configure do |config|
config.handlers.youtube_me.video_info = true
config.handlers.youtube_me.detect_urls = true
config.handlers.youtube_me.top_result = true
end
```

Expand Down
3 changes: 2 additions & 1 deletion lib/lita/handlers/youtube_me.rb
Expand Up @@ -9,6 +9,7 @@ class YoutubeMe < Handler
config :api_key, type: String, required: true
config :video_info, types: [TrueClass, FalseClass], default: false
config :detect_urls, types: [TrueClass, FalseClass], default: false
config :top_result, types: [TrueClass, FalseClass], default: false

route(/^(?:youtube|yt)(?: me)?\s+(.*)/i, :find_video, command: true, help: {
"youtube (me) QUERY" => "Gets a YouTube video."
Expand All @@ -29,7 +30,7 @@ def find_video(response)
)
return if http_response.status != 200
videos = MultiJson.load(http_response.body)["items"].select { |v| v["id"].key?("videoId") }
video = videos.sample
video = config.top_result ? videos.first : videos.sample
id = video["id"]["videoId"]
response.reply "https://www.youtube.com/watch?v=#{id}"
if config.video_info
Expand Down
6 changes: 6 additions & 0 deletions spec/lita/handlers/youtube_me_spec.rb
Expand Up @@ -75,6 +75,12 @@
expect(replies.count).to eq 0
end

it "returns the top video in response to a query when the top_result config variable is true" do
registry.config.handlers.youtube_me.top_result = true
send_command("yt polica lay your cards out")
expect(replies.first).to match(/Rl03afAqeFQ/)
end

it "does not return a video in response to a query when the API key is invalid" do
registry.config.handlers.youtube_me.api_key = "this key doesn't work"
send_command("youtube me soccer")
Expand Down

0 comments on commit 03d9f8f

Please sign in to comment.