예제 #1
0
        public void SetUp()
        {
            _api = _factory.Create<ILinqApi>();

            ILinqApi linqApi = _api.Object;
            _queryable = new TwitterQueryable<IUser>(new TwitterQueryProvider(() => new TwitterQuery(linqApi)));
        }
예제 #2
0
파일: Twitter.cs 프로젝트: ArildF/linqtwit
        public Twitter(ILinqApi linqApi)
        {
            FriendsTimeLine = new TwitterQueryable<Status>(new TwitterQueryProvider(() => new TimelineQuery(linqApi.FriendsTimeLine)));
            MentionsTimeLine =
                new TwitterQueryable<Status>(
                    new TwitterQueryProvider(
                        () => new TimelineQuery(linqApi.MentionsTimeLine)));

            Users =
                new TwitterQueryable<IUser>(
                    new TwitterQueryProvider(() => new TimelineQuery(linqApi.FriendsTimeLine)));
        }
        public void StreamingCallback_Sets_TwitterExecutor_StreamCallback()
        {
            var authMock = new Mock<ITwitterAuthorizer>();
            var execMock = new Mock<ITwitterExecute>();
            execMock.SetupGet(exec => exec.AuthorizedClient).Returns(authMock.Object);
            var ctxMock = new Mock<TwitterContext>(execMock.Object);
            var twQueryable = new TwitterQueryable<Streaming>(ctxMock.Object);
            Action<StreamContent> callback = content => Console.WriteLine(content.Content);

            twQueryable.StreamingCallback(callback);

            execMock.VerifySet(exec => exec.StreamingCallback = It.IsAny<Action<StreamContent>>());
        }
예제 #4
0
        public void StreamingCallback_Sets_TwitterExecutor_StreamCallback()
        {
            var authMock = new Mock <ITwitterAuthorizer>();
            var execMock = new Mock <ITwitterExecute>();

            execMock.SetupGet(exec => exec.AuthorizedClient).Returns(authMock.Object);
            var ctxMock     = new Mock <TwitterContext>(execMock.Object);
            var twQueryable = new TwitterQueryable <Streaming>(ctxMock.Object);
            Action <StreamContent> callback = content => Console.WriteLine(content.Content);

            twQueryable.StreamingCallback(callback);

            execMock.VerifySet(exec => exec.StreamingCallback = It.IsAny <Action <StreamContent> >());
        }
예제 #5
0
        private const int DEFAULT_TIMEOUT       = 10000; // 10 seconds

        public IRecord[] Execute(string query,
                                 int?maximumResults,
                                 string OAuthConsumerKey,
                                 string OAuthConsumerSecret,
                                 string OAuthToken,
                                 string OAuthTokenSecret,
                                 System.Collections.Generic.IDictionary <string, object> parameters,
                                 System.Collections.Generic.IList <string> aliases)
        {
            Guard.ArgumentNotNull(query, "query");
            IRecord[] result = null;


            if (parameters != null)
            {
                foreach (System.Collections.Generic.KeyValuePair <string, object> parameter in parameters)
                {
                    if (parameter.Value != null)
                    {
                        query = query.Replace(":" + parameter.Key, parameter.Value.ToString());
                    }
                    else
                    if (query.IndexOf(":" + parameter.Key) >= 0)
                    {
                        // this means the query has the parameter, but no value was passed
                        // in that case best to not actually run the query since we don't have parameters filled in
                        return new IRecord[] {}
                    }
                    ;
                }
            }

            var auth = new SingleUserAuthorizer();

            auth.OAuthTwitter.OAuthConsumerKey    = OAuthConsumerKey;
            auth.OAuthTwitter.OAuthConsumerSecret = OAuthConsumerSecret;
            auth.OAuthTwitter.OAuthToken          = OAuthToken;
            auth.OAuthTwitter.OAuthTokenSecret    = OAuthTokenSecret;
            var twitterCtx = new TwitterContext(auth);

            twitterCtx.Timeout = DEFAULT_TIMEOUT;

            LinqCompiler lc = new LinqCompiler("");

            lc.Query = query;
            lc.AddSource("Status", twitterCtx.Status);
            lc.AddSource("User", twitterCtx.User);
            lc.AddSource("Search", twitterCtx.Search);

            object oresult = null;

            try
            {
                oresult = lc.Evaluate();

                if (oresult.GetType() == typeof(TwitterQueryable <LinqToTwitter.Status>))
                {
                    TwitterQueryable <LinqToTwitter.Status> q = (TwitterQueryable <LinqToTwitter.Status>)oresult;
                    //result = q.ToList().Cast<object>().SafeSelect((object item) => DefaultTwitterExecutor.CreateRecord(item, aliases)).ToArray<IRecord>();
                    result = q.ToList().SafeSelect(item => CreateStatusRecord(item)).ToArray <IRecord>();
                }
                else if (oresult.GetType() == typeof(TwitterQueryable <LinqToTwitter.User>))
                {
                    TwitterQueryable <LinqToTwitter.User> q = (TwitterQueryable <LinqToTwitter.User>)oresult;
                    result = q.ToList().Cast <object>().SafeSelect((object item) => DefaultTwitterExecutor.CreateRecord(item, aliases)).ToArray <IRecord>();
                }
                else if (oresult.GetType() == typeof(TwitterQueryable <LinqToTwitter.Search>))
                {
                    TwitterQueryable <LinqToTwitter.Search> q = (TwitterQueryable <LinqToTwitter.Search>)oresult;
                    // for the search operation, select the statuses as an array
                    result = q.ToList()[0].Statuses.SafeSelect(item => CreateStatusRecord(item)).ToArray <IRecord>();
                }
            }
            catch (TwitterQueryException ex)
            {
                WebException webEx = ex.InnerException as WebException;
                if (webEx != null)
                {
                    HttpWebResponse response = webEx.Response as HttpWebResponse;
                    if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                    {
                        // twitter returns a 404 if no matching user
                        return(new IRecord[] { });
                    }
                }
                throw new MashupException(string.Format("{0}{1}{2}", Saleslogix.Social.Mashups.Properties.Resources.DefaultQueryExecutor_UnableToExecuteQuery, System.Environment.NewLine, ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new MashupException(string.Format("{0}{1}{2}", Saleslogix.Social.Mashups.Properties.Resources.DefaultQueryExecutor_UnableToExecuteQuery, System.Environment.NewLine, ex.Message), ex);
            }

            if (result != null)
            {
                return(result);
            }

            throw new MashupException(Saleslogix.Social.Mashups.Properties.Resources.DefaultQueryExecutor_UnableToExecuteQuery);



            //using (SessionScopeWrapper sessionScopeWrapper = new SessionScopeWrapper())
            //{
            //    IQuery query2 = sessionScopeWrapper.CreateQuery(query);
            //    if (maximumResults.HasValue)
            //    {
            //        query2.SetMaxResults(maximumResults.Value);
            //    }
            //    if (parameters != null)
            //    {
            //        ISessionFactoryImplementor sessionFactoryImplementor = (ISessionFactoryImplementor)sessionScopeWrapper.SessionFactory;
            //        ParameterMetadata parameterMetadata = (sessionFactoryImplementor != null && sessionFactoryImplementor.QueryPlanCache != null) ? sessionFactoryImplementor.QueryPlanCache.GetHQLQueryPlan(query, false, null).ParameterMetadata : null;
            //        foreach (System.Collections.Generic.KeyValuePair<string, object> current in parameters)
            //        {
            //            if (parameterMetadata != null)
            //            {
            //                NamedParameterDescriptor namedParameterDescriptor = parameterMetadata.GetNamedParameterDescriptor(current.Key);
            //                PrimitiveType primitiveType = namedParameterDescriptor.ExpectedType as PrimitiveType;
            //                object val = (primitiveType != null) ? ConvertEx.ChangeType(current.Value, primitiveType.PrimitiveClass, null) : current.Value;
            //                query2.SetParameter(current.Key, val, namedParameterDescriptor.ExpectedType);
            //            }
            //            else
            //            {
            //                query2.SetParameter(current.Key, current.Value);
            //            }
            //        }
            //    }
            //    try
            //    {
            //        result = query2.List().Cast<object>().SafeSelect((object item) => DefaultQueryExecutor.CreateRecord(item, aliases)).ToArray<IRecord>();
            //    }
            //    catch (QueryException ex)
            //    {
            //        throw new MashupException(string.Format("{0}{1}{2}", Resources.DefaultQueryExecutor_UnableToExecuteQuery, System.Environment.NewLine, ex.Message), ex);
            //    }
            //}
        }