Skip to content

Commit

Permalink
New Embedding implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
felipepessoto committed Apr 21, 2015
1 parent 00a57b6 commit 3f1131e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
29 changes: 8 additions & 21 deletions src/InstaSharp.Tests/Embedding.cs
@@ -1,8 +1,7 @@
using System;
using System.Threading.Tasks;
using InstaSharp.Models;
using InstaSharp.Models;
using InstaSharp.Models.Responses;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;

namespace InstaSharp.Tests
{
Expand Down Expand Up @@ -36,21 +35,9 @@ public async Task MediaInfo()
}

[TestMethod, TestCategory("Embedding")]
public async Task MediaInfoWithDimension()
{
var result = await embedding.MediaInfo("http://instagr.am/p/BUG/", maximumWidth: 600); // this dimension is less than the original
AssertResponseCorrect(result);
Assert.AreEqual(null, result.Height);
Assert.AreEqual(600, result.Width);
Assert.AreEqual(306, result.ThumbnailHeight);
Assert.AreEqual(306, result.ThumbnailWidth);
Assert.AreEqual("https://instagramimages-a.akamaihd.net/media/2010/10/02/7e4051fdcf1d45ab9bc1fba2582c0c6b_6.jpg", result.ThumbnailUrl);
}

[TestMethod, TestCategory("Embedding")]
public async Task MediaInfoWithHeightDimension()
public async Task MediaInfoWidthDimension()
{
var result = await embedding.MediaInfo("http://instagr.am/p/BUG/", 600); // this dimension is less than the original
var result = await embedding.MediaInfo("http://instagr.am/p/BUG/", null, 600, null, null); // this dimension is less than the original
AssertResponseCorrect(result);
Assert.AreEqual(null, result.Height);
Assert.AreEqual(600, result.Width);
Expand All @@ -62,27 +49,27 @@ public async Task MediaInfoWithHeightDimension()
[TestMethod, TestCategory("Embedding")]
public async Task MediaInfoWithHideCaption()
{
var result = await embedding.MediaInfo("http://instagr.am/p/BUG/", 600, hidecaption: true); // this dimension is less than the original
var result = await embedding.MediaInfo("http://instagr.am/p/BUG/", null, 600, true, null); // this dimension is less than the original
AssertResponseCorrect(result);
Assert.AreEqual(null, result.Height);
//Assert.AreEqual(600, result.Width);
//Assert.AreEqual(612, result.ThumbnailHeight);
//Assert.AreEqual(612, result.ThumbnailWidth);
Assert.IsFalse(result.Html.Contains("data-instgrm-captioned"));
Assert.AreEqual("https://instagramimages-a.akamaihd.net/media/2010/10/02/7e4051fdcf1d45ab9bc1fba2582c0c6b_7.jpg", result.ThumbnailUrl);
Assert.AreEqual("https://instagramimages-a.akamaihd.net/media/2010/10/02/7e4051fdcf1d45ab9bc1fba2582c0c6b_6.jpg", result.ThumbnailUrl);
}

[TestMethod, TestCategory("Embedding")]
public async Task MediaInfoWithOmitScript()
{
var result = await embedding.MediaInfo("http://instagr.am/p/BUG/", 600, omitscript: true); // this dimension is less than the original
var result = await embedding.MediaInfo("http://instagr.am/p/BUG/", null, 600, null, true); // this dimension is less than the original
AssertResponseCorrect(result);
Assert.AreEqual(null, result.Height);
//Assert.AreEqual(600, result.Width);
//Assert.AreEqual(612, result.ThumbnailHeight);
//Assert.AreEqual(612, result.ThumbnailWidth);
Assert.IsFalse(result.Html.Contains("embeds.js"));
Assert.AreEqual("https://instagramimages-a.akamaihd.net/media/2010/10/02/7e4051fdcf1d45ab9bc1fba2582c0c6b_7.jpg", result.ThumbnailUrl); //different url
Assert.AreEqual("https://instagramimages-a.akamaihd.net/media/2010/10/02/7e4051fdcf1d45ab9bc1fba2582c0c6b_6.jpg", result.ThumbnailUrl); //different url
}

private static void AssertResponseCorrect(OEmbedResponse result)
Expand Down
6 changes: 3 additions & 3 deletions src/InstaSharp.Tests/Locations.cs
Expand Up @@ -49,10 +49,10 @@ public async Task SearchFacebookPlaces()
{
var result = await locations.Search(104999729569954);
var locationItem = result.Data.Single();
Assert.AreEqual(locationItem.Latitude, 1.379678091M);
Assert.AreEqual(locationItem.Longitude, 103.848781586M);
Assert.AreEqual(1.379678091M, locationItem.Latitude);
Assert.AreEqual(103.848876663M, locationItem.Longitude);
Assert.AreEqual(locationItem.Id, 906462);
Assert.AreEqual(locationItem.Name, "Nanyang Polytechnic (NYP)");
Assert.AreEqual("Nanyang Polytechnic", locationItem.Name);
}
}
}
49 changes: 36 additions & 13 deletions src/InstaSharp/Endpoints/Embedding.cs
Expand Up @@ -23,28 +23,51 @@ public Embedding()
{
}



/// <summary>
/// Given a short link, returns information about the media associated with that link.
/// </summary>
/// <param name="shortlink">A short link, like http://instagr.am/p/fA9uwTtkSN/.</param>
/// <returns></returns>
public Task<OEmbedResponse> MediaInfo(string shortlink)
{
return MediaInfo(shortlink, null, null, null, null);
}

/// <summary>
/// Given a short link, returns information about the media associated with that link.
/// </summary>
/// <param name="shortlink">The shortlink.</param>
/// <param name="maximumHeight">The maximum height.</param>
/// <param name="shortlink">A short link, like http://instagr.am/p/fA9uwTtkSN/.</param>
/// <param name="callback">A JSON callback to be invoked.</param>
/// <param name="maximumWidth">The maximum width.</param>
/// <param name="hidecaption">If set to true, the embed code hides the caption. Defaults to false.
/// NOTE: If this is set to true the <see cref="maximumHeight"/> property appears to be disregarded</param>
/// <param name="omitscript">If set to true, the embed code does not include the script tag. This is useful for websites that want to handle the loading of the embeds.js script by themselves.
/// NOTE: If this is set to true the <see cref="maximumHeight"/> property appears to be disregarded. A different thumbnail url appears to also be returned</param>
/// <param name="hidecaption">If set to true, the embed code hides the caption. Defaults to false.</param>
/// <param name="omitscript">If set to true, the embed code does not include the script tag. This is useful for websites that want to handle the loading of the embeds.js script by themselves.</param>
/// <returns></returns>
public Task<OEmbedResponse> MediaInfo(string shortlink, int? maximumHeight = null, int? maximumWidth = null, bool? hidecaption = null, bool? omitscript = null)
public Task<OEmbedResponse> MediaInfo(string shortlink, string callback, int? maximumWidth, bool? hidecaption, bool? omitscript)
{
if (maximumWidth != null && maximumWidth < 320)
if (maximumWidth <= 320)
{
throw new ArgumentException("Maximum width must be greater or equal to 320 if specified", "maximumWidth");
}

var request = Request("oembed");
request.AddParameter("url", shortlink);
request.AddParameter("callback", callback);

if (omitscript.HasValue)
{
request.AddParameter("omitscript", omitscript.Value.ToString().ToLower());
}
if (hidecaption.HasValue)
{
request.AddParameter("hidecaption", hidecaption.Value.ToString().ToLower());
}
if (maximumWidth.HasValue)
{
throw new ArgumentException("Maximum width must be greater or equal to 320 if specified","maximumWidth");
request.AddParameter("maxwidth", maximumWidth.Value.ToString());
}

var request = Request("/oembed/?url=" + shortlink + (maximumHeight == null ? String.Empty : "&maxheight=" + maximumHeight)
+ (maximumWidth == null ? String.Empty : "&maxwidth=" + maximumWidth)
+ (hidecaption == null ? String.Empty : "&hidecaption=" + hidecaption)
+ (omitscript == null ? String.Empty : "&omitscript=" + omitscript));
return Client.ExecuteAsync<OEmbedResponse>(request);
}

Expand Down

0 comments on commit 3f1131e

Please sign in to comment.