Skip to content

Commit

Permalink
Merge pull request #25 from Priyansh-Kedia/issue#22-23-24
Browse files Browse the repository at this point in the history
Changes for builder pattern
  • Loading branch information
pknotfound authored Sep 8, 2023
2 parents 9368445 + f6364d9 commit d050cd5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
2 changes: 1 addition & 1 deletion OGParser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ afterEvaluate {

groupId = 'com.github.Priyansh-Kedia'
artifactId = 'OpenGraphParser'
version = '2.5.5'
version = '2.5.6'
}
}
}
Expand Down
28 changes: 23 additions & 5 deletions OGParser/src/main/java/com/kedia/ogparser/JsoupNetworkCall.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package com.kedia.ogparser

import android.util.Log
import org.jsoup.Jsoup

class JsoupNetworkCall {
/**
* @param timeout - Timeout for requests, specified in milliseconds (default - 60000)
* @param jsoupProxy - Specify proxy for requests (host, port)
* @param maxBodySize - The maximum size to fetch for body
*/

class JsoupNetworkCall(
private val timeout: Int? = DEFAULT_TIMEOUT,
private val jsoupProxy: JsoupProxy? = null,
private val maxBodySize: Int? = null
) {

fun callUrl(url: String, agent: String): OpenGraphResult? {
val openGraphResult = OpenGraphResult()
try {
val response = Jsoup.connect(url)
val connection = Jsoup.connect(url)
.ignoreContentType(true)
.userAgent(agent)
.referrer(REFERRER)
.timeout(TIMEOUT)
.timeout(timeout ?: DEFAULT_TIMEOUT)
.followRedirects(true)
.execute()

jsoupProxy?.let { connection.proxy(it.host, it.port) }
maxBodySize?.let { connection.maxBodySize(it) }

val response = connection.execute()

val doc = response.parse()
val ogTags = doc.select(DOC_SELECT_OGTAGS)
Expand Down Expand Up @@ -63,11 +78,14 @@ class JsoupNetworkCall {

companion object {
private const val REFERRER = "http://www.google.com"
private const val TIMEOUT = 100000
private const val DEFAULT_TIMEOUT = 60000

private const val DOC_SELECT_OGTAGS = "meta[property^=og:]"
private const val DOC_SELECT_DESCRIPTION = "meta[name=description]"

private const val OPEN_GRAPH_KEY = "content"
private const val PROPERTY = "property"

private const val OG_IMAGE = "og:image"
private const val OG_DESCRIPTION = "og:description"
private const val OG_URL = "og:url"
Expand Down
6 changes: 6 additions & 0 deletions OGParser/src/main/java/com/kedia/ogparser/JsoupProxy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kedia.ogparser

data class JsoupProxy(
val host: String,
val port: Int
)
48 changes: 44 additions & 4 deletions OGParser/src/main/java/com/kedia/ogparser/OpenGraphParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,54 @@ package com.kedia.ogparser
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext

class OpenGraphParser(
class OpenGraphParser @JvmOverloads constructor(
private val listener: OpenGraphCallback,
private var showNullOnEmpty: Boolean = false,
private val cacheProvider: CacheProvider? = null
) {
private val cacheProvider: CacheProvider? = null,
timeout: Int? = null,
jsoupProxy: JsoupProxy? = null,
maxBodySize: Int? = null) {

private val jsoupNetworkCall = JsoupNetworkCall()
private val jsoupNetworkCall = JsoupNetworkCall(
timeout, jsoupProxy, maxBodySize
)

private constructor(builder: Builder): this(
builder.listener,
builder.showNullOnEmpty,
builder.cacheProvider,
builder.timeout,
builder.jsoupProxy,
builder.maxBodySize
)

class Builder(
val listener: OpenGraphCallback
) {
var showNullOnEmpty: Boolean = false
private set

var cacheProvider: CacheProvider? = null
private set

var timeout: Int? = null
private set

var jsoupProxy: JsoupProxy? = null
private set

var maxBodySize: Int? = null
private set

// Setter methods for the vars
fun showNullOnEmpty(showNullOnEmpty: Boolean) = apply { this.showNullOnEmpty = showNullOnEmpty }
fun cacheProvider(cacheProvider: CacheProvider) = apply { this.cacheProvider = cacheProvider }
fun timeout(timeout: Int) = apply { this.timeout = timeout }
fun jsoupProxy(jsoupProxy: JsoupProxy) = apply { this.jsoupProxy = jsoupProxy }
fun maxBodySize(maxBodySize: Int) = apply { this.maxBodySize = maxBodySize }

fun build() = OpenGraphParser(this)
}

fun parse(url: String) {
ParseLink(url).parse()
Expand Down

0 comments on commit d050cd5

Please sign in to comment.