Skip to content

Commit

Permalink
build(spm): add swift package manifest (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
sherwinski authored Feb 12, 2020
1 parent a9c43c2 commit 0150dc7
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 218 deletions.
4 changes: 2 additions & 2 deletions ImgixSwift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Pod::Spec.new do |s|

s.license = { :type => 'BSD 2-Clause', :file => 'LICENSE.md' }
s.homepage = "https://github.com/imgix/imgix-swift"
s.authors = { "Paul Straw" => "paulstraw@paulstraw.com" }
s.authors = { "Paul Straw" => "paulstraw@paulstraw.com", "Sherwin Heydarbeygi" => "sherwin@imgix.com" }
s.source = { :git => "https://github.com/imgix/imgix-swift.git", :tag => s.version }

s.xcconfig = {
"SWIFT_OBJC_INTERFACE_HEADER_NAME" => "ImgixSwift.h"
}

s.requires_arc = true
s.source_files = ["Sources/*.{h,m,swift}"]
s.source_files = ["Sources/ImgixSwift/*.swift"]

s.ios.deployment_target = "9.0"
s.osx.deployment_target = "10.10"
Expand Down
26 changes: 26 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swift-tools-version:5.1

import PackageDescription

let package = Package(
name: "ImgixSwift",
platforms: [
.macOS(.v10_10),
.iOS(.v9),
.tvOS(.v9)
],
products: [
.library(
name: "ImgixSwift",
targets: ["ImgixSwift"])
],
targets: [
.target(
name: "ImgixSwift"
),
.testTarget(
name: "ImgixSwiftTests",
dependencies: ["ImgixSwift"])
],
swiftLanguageVersions: [.v5]
)
22 changes: 11 additions & 11 deletions Sources/ImgixClient.swift → Sources/ImgixSwift/ImgixClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// ImgixClient.swift
// imgix-swift
//
// Created by Paul Straw on 6/30/16.
//
//

import Foundation

Expand All @@ -13,7 +10,7 @@ import Foundation

@objc public let host: String
@objc open var useHttps: Bool = true
@objc open var secureUrlToken: String? = nil
@objc open var secureUrlToken: String?
@objc open var includeLibraryParam: Bool = true

@objc public init(host: String) {
Expand Down Expand Up @@ -46,7 +43,10 @@ import Foundation
urlComponents.queryItems = buildParams(params)

if secureUrlToken != nil {
let signature = signatureForPathAndQueryString(path, queryString: encodeQueryItems(urlComponents.queryItems!))
let signature = signatureForPathAndQueryString(
path,
queryString: encodeQueryItems(urlComponents.queryItems!)
)
urlComponents.queryItems?.append(signature)
}

Expand All @@ -73,7 +73,7 @@ import Foundation
}
}

mergedParams.addEntries(from: params as! [AnyHashable : Any])
mergedParams.addEntries(from: params as! [AnyHashable: Any])

let generatedURL = buildUrl(originalURL.path, params: mergedParams)

Expand Down Expand Up @@ -120,26 +120,26 @@ import Foundation
var queryItems = [URLQueryItem]()
let queryParams: NSMutableDictionary = NSMutableDictionary.init(dictionary: params)

if (includeLibraryParam) {
if includeLibraryParam {
queryParams.setValue("swift-" + ImgixClient.VERSION, forKey: "ixlib")
}

let keys = queryParams.allKeys.map { String(describing: $0) }

for key in keys.sorted(by: {$0 < $1}) {
if let val = queryParams[key] {
var stringVal = String(describing: val)

if key.hasSuffix("64") {
stringVal = stringVal.ixEncode64()
}

let queryItem = URLQueryItem.init(name: key, value: stringVal)

queryItems.append(queryItem)
}
}

return queryItems
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// String+ImgixSwift.swift
// imgix-swift
//
// Created by Paul Straw on 7/7/16.
//
//

import Foundation
import var CommonCrypto.CC_MD5_DIGEST_LENGTH
Expand All @@ -13,25 +10,25 @@ import typealias CommonCrypto.CC_LONG

extension String {
static var ixEncodeUriComponentCharSet: CharacterSet = {
var cs = CharacterSet.alphanumerics
cs.insert(charactersIn: "-_.!~*'()")
return cs
var charSet = CharacterSet.alphanumerics
charSet.insert(charactersIn: "-_.!~*'()")
return charSet
}()

func ixEncode64() -> String {
let strData = self.data(using: String.Encoding.utf8)

guard var str64 = strData?.base64EncodedString(options: Data.Base64EncodingOptions()) else {
return ""
}

str64 = str64.replacingOccurrences(of: "=", with: "")
str64 = str64.replacingOccurrences(of: "/", with: "_")
str64 = str64.replacingOccurrences(of: "+", with: "-")

return str64
}

func ixEncodeUriComponent() -> String {
return self.addingPercentEncoding(withAllowedCharacters: String.ixEncodeUriComponentCharSet)!
}
Expand All @@ -42,12 +39,13 @@ extension String {

func MD5(string: String) -> String {
let length = Int(CC_MD5_DIGEST_LENGTH)
let messageData = string.data(using:.utf8)!
let messageData = string.data(using: .utf8)!
var digestData = Data(count: length)

_ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
messageData.withUnsafeBytes { messageBytes -> UInt8 in
if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
if let messageBytesBaseAddress = messageBytes.baseAddress,
let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
let messageLength = CC_LONG(messageData.count)
CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// BuildUrlTests.swift
// imgix-swift
//
// Created by Paul Straw on 7/4/16.
//
// ImgixSwiftTests
//

import XCTest
Expand All @@ -14,95 +11,95 @@ class BuildUrlTests: XCTestCase {

override func setUp() {
super.setUp()

client = ImgixClient.init(host: "paulstraw.imgix.net")
client.includeLibraryParam = false
}

override func tearDown() {
super.tearDown()
}

func testBuildUrlWithoutParams() {
let generatedUrl = client.buildUrl("1.jpg")
let expectedUrl = "https://paulstraw.imgix.net/1.jpg"

XCTAssert(generatedUrl.absoluteString == expectedUrl)
}

func testBuildUrlWithoutParamsAndIncludeLibraryParam() {
client.includeLibraryParam = true

let generatedUrl = client.buildUrl("1.jpg")
let expectedUrl = "https://paulstraw.imgix.net/1.jpg?ixlib=swift-\(ImgixClient.VERSION)"

XCTAssert(generatedUrl.absoluteString == expectedUrl)
}

func testBuildUrlEmptyParams() {
let generatedUrl = client.buildUrl("1.jpg", params: [:])
let expectedUrl = "https://paulstraw.imgix.net/1.jpg"

XCTAssert(generatedUrl.absoluteString == expectedUrl)
}

func testBuildUrlWithOneParam() {
let generatedUrl = client.buildUrl("1.jpg", params: ["w": 400])
let expectedUrl = "https://paulstraw.imgix.net/1.jpg?w=400"

XCTAssert(generatedUrl.absoluteString == expectedUrl)
}

func testBuildUrlWithOneParamAndIncludeLibraryParam() {
client.includeLibraryParam = true

let generatedUrl = client.buildUrl("1.jpg", params: ["w": 400])
let expectedUrl = "https://paulstraw.imgix.net/1.jpg?ixlib=swift-\(ImgixClient.VERSION)&w=400"

XCTAssert(generatedUrl.absoluteString == expectedUrl)
}

func testBuildUrlWithMultipleParams() {
let generatedUrl = client.buildUrl("1.jpg", params: ["w": 400, "flip": "v"])
let expectedUrl = "https://paulstraw.imgix.net/1.jpg?flip=v&w=400"

XCTAssert(generatedUrl.absoluteString == expectedUrl)
}

func testBuildUrlWithMultipleParamsSorted() {
let generatedUrl = client.buildUrl("1.jpg", params: ["w": 900, "h": 300, "fit": "crop", "crop": "entropy"])
let expectedUrl = "https://paulstraw.imgix.net/1.jpg?crop=entropy&fit=crop&h=300&w=900"

XCTAssert(generatedUrl.absoluteString == expectedUrl)
}

func testBuildUrlWithMultipleParamsAndIncludeLibraryParamSorted() {
client.includeLibraryParam = true
let generatedUrl = client.buildUrl("1.jpg", params: ["w": 900, "h": 300, "fit": "crop", "crop": "entropy"])
let expectedUrl = "https://paulstraw.imgix.net/1.jpg?crop=entropy&fit=crop&h=300&ixlib=swift-\(ImgixClient.VERSION)&w=900"

let expectedUrl = "https://paulstraw.imgix.net/1.jpg?" +
"crop=entropy&fit=crop&h=300&ixlib=swift-\(ImgixClient.VERSION)&w=900"

XCTAssert(generatedUrl.absoluteString == expectedUrl)
}

func testQueryStringKeyEscaping() {
let generatedUrl = client.buildUrl("1.jpg", params: ["hello world": "interesting"])
let expectedQuery = "hello%20world=interesting"

XCTAssert(generatedUrl.query == expectedQuery)
}

func testQueryStringValueEscaping() {
let generatedUrl = client.buildUrl("1.jpg", params: ["txt": "/foo'> <script>alert('hacked')</script><"])
let expectedQuery = "txt=%2Ffoo'%3E%20%3Cscript%3Ealert('hacked')%3C%2Fscript%3E%3C"

XCTAssert(generatedUrl.query == expectedQuery)
}

func testBase64ParamVariantsAreBase64Encoded() {
let generatedUrl = client.buildUrl("~text", params: ["txt64": "I cannøt belîév∑ it wors! 😱"])
let expectedQuery = "txt64=SSBjYW5uw7h0IGJlbMOuw6l24oiRIGl0IHdvcu-jv3MhIPCfmLE"

XCTAssert(generatedUrl.query == expectedQuery)
}

}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,43 @@
// InitializationTests.swift
// ImgixSwiftTests
//
// Created by Paul Straw on 7/4/16.
//
//

import XCTest
import ImgixSwift

class InitializationTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testHost() {
let client = ImgixClient.init(host: "paulstraw.imgix.net")

XCTAssert(client.host == "paulstraw.imgix.net")
}

func testDefaultsToHttpsUrls() {
let client = ImgixClient.init(host: "paulstraw.imgix.net")

XCTAssert(client.useHttps == true)
}

func testSettingHttpUrls() {
let client = ImgixClient.init(host: "paulstraw.imgix.net", useHttps: false)

XCTAssert(client.useHttps == false)
}

func testSettingSecureUrlToken() {
let client = ImgixClient.init(host: "paulstraw.imgix.net", secureUrlToken: "loreM")

XCTAssert(client.secureUrlToken == "loreM")
}
}
Loading

0 comments on commit 0150dc7

Please sign in to comment.