Skip to content
This repository was archived by the owner on Apr 27, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.softwaremill.java_fp_example.contest.dorzepowski;

class DefaultImageUrl implements ImageUrl {

@Override
public String toString() {
return "https://softwaremill.com/images/logo-vertical.023d8496.png";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.softwaremill.java_fp_example.contest.dorzepowski;

import javaslang.control.Option;

class FacebookImage implements Image {

private final static String FACEBOOK_IMAGE_TAG = "og:image";

private final ParsedPage parsedPage;

private FacebookImage(ParsedPage parsedPage) {
this.parsedPage = parsedPage;
}

static FacebookImage from(ParsedPage parsedPage) {
return new FacebookImage(parsedPage);
}

@Override
public ImageUrl url() {
return facebookImageUrl().getOrElse(DefaultImageUrl::new);
}

private Option<ImageUrl> facebookImageUrl() {
return imageUrlOf(contentFrom(firstImageElement()));
}

private Option<ImageUrl> imageUrlOf(Option<String> url) {
return url.map(FacebookImageUrl::new);
}

private Option<String> contentFrom(Option<ParsedElement> parsedElements) {
return parsedElements.map(ParsedElement::content);
}

private Option<ParsedElement> firstImageElement() {
return parsedPage.elementsByProperty(FACEBOOK_IMAGE_TAG).headOption();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.softwaremill.java_fp_example.contest.dorzepowski;

class FacebookImageUrl implements ImageUrl {

private final String url;

FacebookImageUrl(String url) {
this.url = url;
}

@Override
public String toString() {
return url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.softwaremill.java_fp_example.contest.dorzepowski;

public interface Image {

ImageUrl url();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.softwaremill.java_fp_example.contest.dorzepowski;

public interface ImageUrl {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 dorzepowski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.softwaremill.java_fp_example.contest.dorzepowski;

import java.net.URL;


public class Page {

private final URL pageUrl;

public Page(URL pageUrl) {
this.pageUrl = pageUrl;
}

public Image facebookImage() {
return FacebookImage.from(new ParsedPage(pageUrl));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.softwaremill.java_fp_example.contest.dorzepowski;

import org.jsoup.nodes.Element;

class ParsedElement {

private final Element element;

ParsedElement(Element element) {
this.element = element;
}

String content() {
return element.attr("content");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.softwaremill.java_fp_example.contest.dorzepowski;

import java.io.IOException;
import java.net.URL;
import java.util.function.Function;

import javaslang.collection.Stream;
import javaslang.control.Try;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

@Slf4j
class ParsedPage {

private final static int TEN_SECONDS = 10_000;

private final URL pageUrl;

ParsedPage(URL pageUrl) {
this.pageUrl = pageUrl;
}

Stream<ParsedElement> elementsByProperty(String property) {
return makeParsedElementsWhen(onPage(foundElementsWithProperty(property)));
}

private Stream<ParsedElement> makeParsedElementsWhen(Stream<Element> foundElements) {
return foundElements.map(ParsedElement::new);
}

private Stream<Element> onPage(Function<? super Document, ? extends Elements> toFoundElements) {
return parsePage().flatMap(toFoundElements);
}

private Function<? super Document, ? extends Elements> foundElementsWithProperty(String property) {
return document -> document.head().getElementsByAttributeValue("property", property);
}

private Stream<Document> parsePage() {
return Try.of(this::parse).onFailure(this::logParsingProblem).toStream();
}

private Document parse() throws IOException {
return Jsoup.parse(pageUrl, TEN_SECONDS);
}

private void logParsingProblem(Throwable e) {
log.error("Unable to extract og:image from url {}. Problem: {}", pageUrl, e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.softwaremill.java_fp_example.contest.dorzepowski

import spock.lang.Specification

class FindFacebookImageUrlOnPageSpec extends Specification {

public static
final String DEFAULT_IMAGE = "https://softwaremill.com/images/logo-vertical.023d8496.png"


def "Find facebook image url for page that contain such url"() {
given:
def urlWithImage = new URL("https://softwaremill.com/the-wrong-abstraction-recap/")
def page = new Page(urlWithImage)
when:
def facebookImageUrl = page.facebookImage().url()
then:
'https://softwaremill.com/images/uploads/2017/02/street-shoe-chewing-gum.0526d557.jpg' == facebookImageUrl.toString()
}


def "Find default url when retrieving facebook image url from not existing page"() {
given:
def notExistingUrl = new URL("http://i-do-not-exist.pl")
def page = new Page(notExistingUrl)
when:
def facebookImageUrl = page.facebookImage().url()
then:
DEFAULT_IMAGE == facebookImageUrl.toString()
}

def "Find default url when retrieving facebook image url from page that doesn't contain facebook image"() {
given:
def urlWithoutImage = new URL("https://twitter.com/softwaremill")
def page = new Page(urlWithoutImage)
when:
def facebookImageUrl = page.facebookImage().url()
then:
DEFAULT_IMAGE == facebookImageUrl.toString()
}

def "Find default url when url is null"() {
given:
def page = new Page(null)
when:
def facebookImageUrl = page.facebookImage().url()
then:
DEFAULT_IMAGE == facebookImageUrl.toString()
}
}