Giter Site home page Giter Site logo

nicholastd07 / swiftdailyapi Goto Github PK

View Code? Open in Web Editor NEW
196.0 6.0 28.0 203 KB

A Swift API framework for ZhiHu's Daily News.

License: MIT License

Swift 94.02% Ruby 2.69% Shell 1.99% Objective-C 1.29%
swift api rest-api zhihu daily cocoapods carthage

swiftdailyapi's Introduction

SwiftDailyAPI

Build Status codecov.io CocoaPods Compatible Carthage Compatible

An API framework for ZhiHu's Daily News, built in Swift. It is powering my open source App Swift-ZHI which is a newsreader App built for ZhiHu's Daily News.

Requirement

  • iOS 8.0+ / Mac OS X 10.9+
  • Xcode 7.2 +

Installation

Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks (10.9).

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 0.39.0+ is required to build SwiftDailyAPI 2.0.0+.

To integrate SwiftDailyAPI into your Xcode project using CocoaPods, specify it in your Podfile:

platform :ios, '8.0'
use_frameworks!

pod 'SwiftDailyAPI', '~> 2.0'

Then, run the following command:

$ pod install

Add the following to your Cartfile:

github "NicholasTD07/SwiftDailyAPI" ~> 2.0.0 # Swift 2.1
github "NicholasTD07/SwiftDailyAPI" ~> 1.0.0 # Swift 1.2

Then run carthage update.

Follow the current instructions in Carthage's README for up to date installation instructions.

Usage

import SwiftDailyAPI

// Setup
var latestDaily: LatestDaily?
var daily: Daily?
var news: News?
var newsExtra: NewsExtra?
var shortComments, longComments: Comments?

// Given
let newsId = 4772308
let date = NSDate.dateFromString("20150525", format: DailyConstants.dateFormat)!
let api = DailyAPI(userAgent: "SwiftDailyAPI_ReadMe")

// When
api.latestDaily { latestDailyFromAPI in
  latestDaily = latestDailyFromAPI
  print(latestDaily?.news)
  print(latestDaily?.topNews)
}

api.daily(forDate: date) { dailyFromAPI in
  daily = dailyFromAPI
  print(daily?.news)
}

api.news(newsId) { newsFromAPI in
  news = newsFromAPI
  print(news?.newsId)
  print(news?.title)
}

api.newsExtra(newsId) { newsExtraFromAPI in
  newsExtra = newsExtraFromAPI
  print(newsExtra?.popularity)
  print(newsExtra?.comments)
}

api.comments(newsId, shortCommentsHandler: { comments in
  shortComments = comments
  print(shortComments?.comments)
  }, longCommentsHandler: { comments in
    longComments = comments
    print(longComments?.comments)
})

// Then
// expect all vars in `Setup` section to not be nil
// code in `ReadmeCodeSpecs.swift`

This code is tested in ReadmeCodeSpecs.swift. There's also a Then part in the spec which expects all the vars in the Setup to not be nil.

Frameworks

Appendix

How does SwiftDailyAPI support both Carthage and CocoaPods while having 3rd parity dependencies?

Well, it works like this. Before 2.0.1, I was using Carthage to manage dependencies so Carthage works for version 2.0.0. However, I switched to CocoaPods since 2.0.1 because it's easier to setup, more stable from my experience, also takes less time to install dependencies (Carthage has one extra build step) and Travis has caching for CocoaPods.

swiftdailyapi's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

swiftdailyapi's Issues

Looks like this Argo is not as easy as ObjectMappper

JUST FYI,

https://github.com/Hearst-DD/ObjectMapper

`class User: Mappable {
var username: String?
var age: Int?
var weight: Double!
var array: [AnyObject]?
var dictionary: [String : AnyObject] = [:]
var bestFriend: User? // Nested User object
var friends: [User]? // Array of Users
var birthday: NSDate?

required init?(_ map: Map) {

}

// Mappable
func mapping(map: Map) {
    username    <- map["username"]
    age         <- map["age"]
    weight      <- map["weight"]
    array       <- map["arr"]
    dictionary  <- map["dict"]
    bestFriend  <- map["best_friend"]
    friends     <- map["friends"]
    birthday    <- (map["birthday"], DateTransform())
}

}`

Log failed decoding of JSON

type: 'news' or 'daily'
id-string: 'id: xxx' or 'date: yyyyMMdd'
json: ...
version: x.y.z
Argo version: x.y.z

Get `DailyNews` for a date

Given a user wants DailyNews for a specific date
When the user pass in the date and a handler
When the request and parsing finishes successfully
Then the handler will be called with the DailyNews

Support Daily Themes

struct BasicNewsMeta { // ThemeNewsMeta
  let newsId: Int
  let title: String
  let imageURLs: [NSURL]?
}

struct Theme { }

enum DailyRouter {
  case themes
  case themeDaily(themeId:)
}

Split `ModelDecodingSpecs` into smaller files

It is getting larger and larger with more models added in.

Some ideas:

  • Split by data
    • RealDataModelDecoding
    • TestDataModelDecoding
  • Or split by model
    • NewsDecoding
    • DailyDecoding
    • etc.

Highlight Commits

This is a note where I keep all the note worth a second look. Maybe I had an elegant way of solving a problem, maybe I was impressed by Swfit.

Isolated spec for `News` with real data

Although DailyAPISpecs test model decoding with real data. However, when the server changes its API, those specs will fail. It would be hard to tell which part of the process failed. Maybe it's network, maybe it's the JSON parsing.

With spec testing on real data, it would be easier and quicker to find out which part fails during a integration test like the ones in DailyAPISpecs.

api.request THROWS

enum SwiftDailyError {
  // case NoNetworkError
  case RequestFailedError
  case DecodingError
}

Update dependencies

  • carthage update
  • xcodebuild clean test
  • carthage build ...
  • put it into a bin file

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.