Skip to content
This repository has been archived by the owner on Feb 4, 2020. It is now read-only.

Commit

Permalink
Added news loading
Browse files Browse the repository at this point in the history
Fixed UnixDateTimeConverter
  • Loading branch information
Constantine Colotiline committed Nov 7, 2014
1 parent 09ed59a commit fecf494
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@ $RECYCLE.BIN/

# Mac desktop service store files
.DS_Store

15 changes: 10 additions & 5 deletions Meduza.net/Api.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Meduza.net.Annotations;
using Meduza.net.Models.Api;
using Meduza.net.Models.Api.Enum;
using Meduza.net.Models.Api.Page;
using Meduza.net.Models.Api.Types;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Meduza.net {
public sealed class Api : INotifyPropertyChanged {
Expand Down Expand Up @@ -56,5 +56,10 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = null) {
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}

public async Task<News> LoadNewsAsync(string uri) {
var content = await _httpClient.GetStringAsync(uri);
return JObject.Parse(content).GetValue("root").ToObject<News>();
}
}
}
8 changes: 4 additions & 4 deletions Meduza.net/Helpers/UnixDateTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
var intValue = reader.Value as int?;
if (intValue.HasValue) {
return _unixStartDateTime.AddMilliseconds(intValue.Value);
return _unixStartDateTime.AddSeconds(intValue.Value);
}

var longValue = reader.Value as long?;
if (longValue.HasValue) {
return _unixStartDateTime.AddMilliseconds(longValue.Value);
return _unixStartDateTime.AddSeconds(longValue.Value);
}

var doubleValue = reader.Value as double?;
if (doubleValue.HasValue) {
return _unixStartDateTime.AddMilliseconds(doubleValue.Value);
return _unixStartDateTime.AddSeconds(doubleValue.Value);
}

var stringValue = reader.Value as string;
if (!string.IsNullOrWhiteSpace(stringValue)) {
return _unixStartDateTime.AddMilliseconds(Double.Parse(stringValue));
return _unixStartDateTime.AddSeconds(Double.Parse(stringValue));
}

throw new ArgumentException();
Expand Down
2 changes: 2 additions & 0 deletions Meduza.net/Meduza.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Compile Include="Helpers\UnixDateTimeConverter.cs" />
<Compile Include="Models\Api\Card\BackgroundImage.cs" />
<Compile Include="Models\Api\Card\Chapter.cs" />
<Compile Include="Models\Api\Content.cs" />
<Compile Include="Models\Api\Document.cs" />
<Compile Include="Models\Api\Enum\DocumentType.cs" />
<Compile Include="Models\Api\Enum\FunType.cs" />
Expand All @@ -48,6 +49,7 @@
<Compile Include="Models\Api\Page\Main.cs" />
<Compile Include="Models\Api\Root.cs" />
<Compile Include="Models\Api\Source.cs" />
<Compile Include="Models\Api\Types\News.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Uris.cs" />
Expand Down
26 changes: 26 additions & 0 deletions Meduza.net/Models/Api/Content.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Newtonsoft.Json;

namespace Meduza.net.Models.Api {
public sealed class Content {
private readonly string _body;
private readonly string _layoutUri;
private readonly string _description;
public Content(
string body,
[JsonProperty(PropertyName = "layout_url")] string layoutUri,
string description) {
_body = body;
_layoutUri = layoutUri;
_description = description;
}
public string Body {
get { return _body; }
}
public string LayoutUri {
get { return _layoutUri; }
}
public string Description {
get { return _description; }
}
}
}
4 changes: 2 additions & 2 deletions Meduza.net/Models/Api/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Newtonsoft.Json.Converters;

namespace Meduza.net.Models.Api {
public sealed class Document {
public class Document {
private readonly string _uri;
private readonly string _title;
private readonly string _secondTitle;
Expand Down Expand Up @@ -108,6 +108,6 @@ public Image Image {
public FunType FunType { get; set; }

//Topic properties
public IReadOnlyList<string> Content { get; set; }
public IReadOnlyList<string> Content { get; set; }
}
}
44 changes: 44 additions & 0 deletions Meduza.net/Models/Api/Types/News.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using Meduza.net.Helpers;
using Meduza.net.Models.Api.Enum;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Meduza.net.Models.Api.Types {
public sealed class News : Document {
public News(
[JsonProperty(PropertyName = "url")] string uri,
string title,
[JsonProperty(PropertyName = "second_title")] string secondTitle,
IReadOnlyList<IReadOnlyList<string>> authors,

[JsonProperty(PropertyName = "document_type")]
[JsonConverter(typeof(StringEnumConverter))]
DocumentType documentType,

int version,
[JsonProperty(PropertyName = "published_at")]
[JsonConverter(typeof(UnixDateTimeConverter))] DateTime publishedAt,

[JsonProperty(PropertyName = "updated_at")]
[JsonConverter(typeof(UnixDateTimeConverter))] DateTime updatedAt,

[JsonProperty(PropertyName = "full")] bool isFull,
Source source,
Image image)
: base(
uri,
title,
secondTitle,
authors,
documentType,
version,
publishedAt,
updatedAt,
isFull,
source,
image) { }
public new Content Content { get; set; }
}
}
12 changes: 11 additions & 1 deletion Tests/Loading.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Meduza.net;
using System.Linq;
using Meduza.net;
using NUnit.Framework;

namespace Tests {
Expand All @@ -18,5 +19,14 @@ public async void NonDefaultInitialization() {
await api.InitializeAsync();
Assert.NotNull(api.Main);
}
[Test]
public async void LoadNews() {
var api = new Api();

var document = api.Main.Documents.First();
var fullDocument = await api.LoadNewsAsync(document.Value.Uri);

Assert.NotNull(fullDocument);
}
}
}

0 comments on commit fecf494

Please sign in to comment.