Skip to content

Instantly share code, notes, and snippets.

@semuserable
Last active August 23, 2016 13:35
Show Gist options
  • Save semuserable/36c4024176a44c9c6a6b74cd4b0a9216 to your computer and use it in GitHub Desktop.
Save semuserable/36c4024176a44c9c6a6b74cd4b0a9216 to your computer and use it in GitHub Desktop.
module FinanceRevised.Core
open FinanceRevised.Types
open MaybeBuilder
open System
open System.IO
open FSharp.Data
open FSharp.Data.JsonExtensions
let private homeJson =
try
(__SOURCE_DIRECTORY__, "finance_ua.json" )
|> Path.Combine
|> File.ReadAllText
|> JsonValue.Parse
|> Some
with
| :? System.IO.FileNotFoundException -> None
let private financeUiJson =
try
"http://resources.finance.ua/ru/public/currency-cash.json"
|> JsonValue.AsyncLoad
|> Async.RunSynchronously
|> Some
with
| Failure _ -> None
| :? System.Net.WebException -> None
let private parseJson = function
| Home -> homeJson
| FinanceUa -> financeUiJson
let private tryGetCityId (name: string) json =
let byCityName jsonValue =
let byName (t:string * JsonValue) = (snd t).AsString().ToLowerInvariant() = name.ToLowerInvariant()
let takeFirst (t: string * JsonValue) = fst t
jsonValue?cities.Properties
|> Array.filter byName
|> Array.map takeFirst
let pickFirst (cities: 'a []) = if cities.Length = 0 then None else Some <| Array.head cities
json
|> byCityName
|> pickFirst
let private tryGetOrganizationsById id json =
let byCityId json = json?cityId.AsString() = id
let allOrganizations = json?organizations.AsArray()
allOrganizations |> Array.filter byCityId |> Some
let private getAverageByOrganizations currency operationType organizations =
let byCurrency t = fst t = currency
let forCurrencies v = v?currencies.Properties |> Array.filter byCurrency
let byOperationType (t: 'a * JsonValue) = (snd t).GetProperty(operationType).AsFloat()
organizations
|> Array.collect forCurrencies
|> Array.map byOperationType
|> Array.average
let private getAverageByCity path city currency byType =
let cityName = City.value city
let currency = Currency.value currency
let byType = CurrencyType.value byType
maybe {
let! json = parseJson path
let! cityId = json |> tryGetCityId cityName
let! organizations = json |> tryGetOrganizationsById cityId
return organizations |> getAverageByOrganizations currency byType
}
let public runFinanceCoreRevised () =
let kyivFromFinance = getAverageByCity FinanceUa City.Kyiv Currency.USD
let kyivUSDAsk = kyivFromFinance CurrencyType.Ask
let kyivUSDBid = kyivFromFinance CurrencyType.Bid
printfn "Ask: %A, Bid: %A" kyivUSDAsk kyivUSDBid
let path = FinanceUa
let cityName = City.value City.Kyiv
let currency = Currency.value Currency.USD
let byType = CurrencyType.value CurrencyType.Ask
let average = maybe {
let! json = parseJson path
let! cityId = json |> tryGetCityId cityName
let! organizations = json |> tryGetOrganizationsById cityId
return organizations |> getAverageByOrganizations currency byType
}
printfn "%A" average
// match jsonParsed with
// | Some json ->
// let cityId = json |> getCityId cityName
// match cityId with
// | Some id ->
// json
// |> getOrganizationsById id
// |> Some
// | None -> None
// | None -> None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment