示例#1
0
        public Client(Credentials credentials, IEnvironment env = null)
        {
            if (credentials == null)
            {
                throw new ArgumentNullException(nameof(credentials));
            }

            environment = env ?? Environments.Production;
            requester   = new Requester(credentials, environment, null, this.apiVersion);

            Account      = new AccountResource(requester);
            Balance      = new BalanceResource(requester);
            Charges      = new ChargeResource(requester);
            Customers    = new CustomerResource(requester);
            Disputes     = new DisputeResource(requester);
            Events       = new EventResource(requester);
            Forex        = new ForexResource(requester);
            Links        = new LinkResource(requester);
            Occurrences  = new OccurrenceResource(requester);
            Sources      = new PaymentSourceResource(requester);
            Receipts     = new ReceiptResource(requester);
            Recipients   = new RecipientResource(requester);
            Refunds      = new RefundResource(requester);
            Schedules    = new ScheduleResource(requester);
            Tokens       = new TokenResource(requester);
            Transactions = new TransactionResource(requester);
            Transfers    = new TransferResource(requester);
        }
示例#2
0
        public async Task <ActionResult> Create([Bind("Name, SelectedTag, TagResources")] LinkResource model)
        {
            if (!String.IsNullOrEmpty(model.Name))
            {
                if (ModelState.IsValid)
                {
                    if (String.IsNullOrEmpty(model.SelectedTag) || model.SelectedTag == "Add other" || model.SelectedTag == "Select tag category")
                    {
                        ViewBag.ErrorMessage = errorMsgGen;
                        return(View(model));
                    }

                    //check for tag occurance
                    // var selectedTagExists = await _tagService.CheckTagForOccuranceAsync(model.Name, model.SelectedTag);
                    model.Name = model.Name.ToLower();
                    var response = await _linkService.CreateLink(model);

                    if (!response)
                    {
                        ViewBag.ErrorMessage = errorMsg;
                        return(View(model));
                    }
                    return(RedirectToAction("Home"));
                }
                else
                {
                    model.TagResources.Clear();
                    ViewBag.ErrorMessage = errorMsg;
                }
            }

            return(View(model));
        }
示例#3
0
        public async Task <bool> CreateLink(LinkResource link)
        {
            try
            {
                link.UserId = userId;
                var obj           = JsonConvert.SerializeObject(link);
                var stringContent = new StringContent(obj, UnicodeEncoding.UTF8, MediaTypeNames.Application.Json);

                var response = await _httpClient.PostAsync(linksUrl, stringContent);

                //if (response.StatusCode == HttpStatusCode.Conflict)
                //{
                //    var errmsg = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);
                //    var id = Regex.Match(Convert.ToString(errmsg), @"'([^']*)").Groups[1].Value;
                //    return new LinkResource { ID = Convert.ToInt32(id), Name = link.Name };
                //}

                //string apiResponse = await response.Content.ReadAsStringAsync();
                //var createdLink = JsonConvert.DeserializeObject<LinkResource>(apiResponse);

                //return createdLink;

                return(response.IsSuccessStatusCode);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
示例#4
0
 private static Links MapLink(LinkResource arg)
 {
     return(new Links
     {
         Url = arg.Target,
         Name = arg.Type
     });
 }
示例#5
0
        public async Task <ActionResult> ClearTags([Bind("Name,SelectedTag, TagResources")] LinkResource model)
        {
            if (model.TagResources != null)
            {
                model.TagResources.Clear();
            }

            return(View("TagRow", model));
        }
示例#6
0
        public ActionResult <LinkResource> PostLink([FromBody] LinkResource longUrl)
        {
            var urlModel = new AddShortLink(_repository, longUrl).Execute();

            if (urlModel == null)
            {
                return(NotFound());
            }

            return(urlModel);
        }
示例#7
0
        public Client(Credentials credentials)
        {
            if (credentials == null)
            {
                throw new ArgumentNullException(nameof(credentials));
            }
            requester = new Requester(credentials);

            Account      = new AccountResource(requester);
            Balance      = new BalanceResource(requester);
            Cards        = new CardResourceShim(requester);
            Charges      = new ChargeResource(requester);
            Customers    = new CustomerResource(requester);
            Disputes     = new DisputeResource(requester);
            Events       = new EventResource(requester);
            Links        = new LinkResource(requester);
            Recipients   = new RecipientResource(requester);
            Refunds      = new RefundResourceShim(requester);
            Tokens       = new TokenResource(requester);
            Transactions = new TransactionResource(requester);
            Transfers    = new TransferResource(requester);
        }
示例#8
0
        public async Task <ActionResult> AddTag([Bind("Name, SelectedTag, TagResources")] LinkResource model)
        {
            //   model.TagResources.Add(new TagResource());

            return(PartialView("TagRow", model));
        }
示例#9
0
        public async Task <ActionResult> InitializeTags([Bind("Name, SelectedTag, TagResources")] LinkResource model)
        {
            model.NewTagResources.Clear();
            model.TagResources.Clear();
            //suggest tags from other users
            var linkOccurance = await _linkService.CheckLinkForOccurance(model.Name.ToLower()); //check if user adds same link twice firstly

            if (linkOccurance != null)
            {
                var tagsWithOccurances = await _tagService.GetTagsByOccurancesAndLinkIdAsync(linkOccurance.ID);

                var usertags = await _tagService.GetTagsByLinkId(linkOccurance.ID);

                if (tagsWithOccurances != null && tagsWithOccurances.Any())
                {
                    //model.TagResources = new List<TagResource>();
                    foreach (var item in tagsWithOccurances)
                    {
                        bool exists = false;
                        // model.TagResources.Add(new SelectListItem() { Disabled=true, Text = item.Item2, Value = item.Item1.ToString()});

                        foreach (var ex in usertags)
                        {
                            if (item.Item1 == ex.ID)
                            {
                                model.TagResources.Add(new SelectListItem()
                                {
                                    Disabled = true, Text = item.Item2, Value = item.Item2
                                });
                                exists = true;
                                break;
                            }
                        }
                        if (!exists)
                        {
                            model.TagResources.Add(new SelectListItem()
                            {
                                Text = item.Item2, Value = item.Item2
                            });
                        }
                        //if (item.Item2 == 1)
                        //    model.TagResources.Add(new TagResource { Name = item.Item1, NumberOfOccurances = "Occured once.", LinkId = model.ID });
                        //else
                        //    model.TagResources.Add(new TagResource { Name = item.Item1, NumberOfOccurances = "Occured " + item.Item2 + " times.", LinkId = model.ID });
                    }
                }
            }
            model.TagResources.Insert(0, new SelectListItem {
                Text = "Select tag category", Value = "0"
            });
            model.TagResources.Insert(model.TagResources.Count, new SelectListItem {
                Text = "Add other", Value = "Add other"
            });
            model.SelectedTag = "0";

            // suggest for new tags from content
            var contentTags = StripHtml(model);

            if (contentTags != null && contentTags.Any())
            {
                var grouped = contentTags.GroupBy(x => x.Name)
                              .OrderBy(group => group.Key)
                              .Select(group => Tuple.Create(group.Key, group.Count()))
                              .ToList();

                var sorted = grouped.OrderByDescending(x => x.Item2).ThenBy(X => X.Item1).ToList();

                int i = 0;
                foreach (var item in sorted)
                {
                    i++;
                    //item.LinkId = model.ID;
                    // model.TagResources.Add(new TagResource { Name = item.Item1, NumberOfOccurances = "Occured " + item.Item2 + " times.", LinkId = model.ID });
                    model.NewTagResources.Add(new SelectListItem()
                    {
                        Text = item.Item1, Value = i + ". "
                    });                                                                                      //name - both times?
                }
            }

            // model.TagResources.Add(new TagResource());

            return(PartialView("TagRow", model));
        }
示例#10
0
        protected List <TagResource> StripHtml(LinkResource link)
        {
            string siteContent = string.Empty;

            try
            {
                List <TagResource> tagResourcesForSuggestion = new List <TagResource>();

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link.Name);
                request.AutomaticDecompression = DecompressionMethods.GZip;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    using (Stream responseStream = response.GetResponseStream())
                        using (StreamReader streamReader = new StreamReader(responseStream))
                        {
                            siteContent = streamReader.ReadToEnd();
                        }

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.OptionWriteEmptyNodes = true;
                doc.LoadHtml(siteContent);
                if (doc == null)
                {
                    return(null);
                }
                string output = "";
                foreach (var node in doc.DocumentNode.ChildNodes)
                {
                    output += node.InnerText;
                }
                var result = output.RemoveStopWords("en");
                result = result.RemoveSpecialCharacters();
                string[] splitkeywords = Regex.Split(result, "(?<=[a-z])(?=[A-Z])|(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])|(?<=\\W)(?=\\W)");
                splitkeywords = (from x in splitkeywords select x.Trim()).ToArray();

                var longwords      = String.Join(" ", splitkeywords.Where(x => x.Length > 3).ToArray());
                var splitlongwords = longwords.Split(" ");

                var groupedkeywords = splitlongwords.GroupBy(split => split)
                                      .Select(g => new { Keyword = g.Key, Count = g.Count() });

                var sortedkeywords = groupedkeywords.OrderByDescending(x => x.Count).ThenBy(X => X.Keyword).ToList();

                var count = sortedkeywords.Count() > 10 ? 10 : sortedkeywords.Count() - 1;

                foreach (var item in sortedkeywords.GetRange(0, count)) //temporary
                {
                    if (item.Count > 2)
                    {
                        for (int i = 0; i < item.Count; i++)
                        {
                            tagResourcesForSuggestion.Add(new TagResource {
                                Name = item.Keyword, LinkId = link.ID
                            });
                        }
                        //tagResourcesForSuggestion.Add(new TagResource { Name = item.Keyword, NumberOfOccurances = "Occured " + item.Count + " times.", LinkId = link.ID });
                    }
                }

                return(tagResourcesForSuggestion);
            }
            catch (Exception)
            {
                return(null); //not possible to parse the provided URL
            }
        }