Created
June 19, 2012 04:51
-
-
Save meboz/2952344 to your computer and use it in GitHub Desktop.
Open exchange rates c# language example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void can_deserialize_exchange_rate_data() | |
{ | |
var latest = "https://raw.github.com/currencybot/open-exchange-rates/master/latest.json"; | |
var client = new RestClient(latest); | |
var request = new RestRequest(Method.GET); | |
var response = client.Execute(request); | |
var exchangeRates = response.Content; | |
var exchangeRateData = JsonConvert.DeserializeObject<ExchangeRate>(exchangeRates); | |
exchangeRateData.Disclaimer.Length.ShouldBeGreaterThan(100); | |
exchangeRateData.Licence.Length.ShouldBeGreaterThan(100); | |
exchangeRateData.Timestamp.ShouldBeGreaterThan(1340080115); | |
exchangeRateData.Base.ShouldEqual("USD"); | |
exchangeRateData.Rates.Count.ShouldBeGreaterThan(20); | |
exchangeRateData.Rates["USD"].ShouldEqual(1.0); | |
var repo = new MongoRepository(); | |
repo.Save(exchangeRateData); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExchangeRate : MongoBase | |
{ | |
[JsonProperty("disclaimer")] | |
public string Disclaimer { get; set; } | |
[JsonProperty("license")] | |
public string Licence { get; set; } | |
[JsonProperty("timestamp")] | |
public long Timestamp { get; set; } | |
[JsonProperty("base")] | |
public string Base { get; set; } | |
[JsonProperty("rates")] | |
public Dictionary<string, decimal> Rates { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class OpenExchangeRatesTests | |
{ | |
[Test] | |
public void can_fetch_and_store_data() | |
{ | |
var latest = "https://raw.github.com/currencybot/open-exchange-rates/master/latest.json"; | |
var client = new RestClient(latest); | |
var request = new RestRequest(Method.GET); | |
var response = client.Execute(request); | |
var exchangeRates = response.Content; | |
var mongoServer = MongoServer.Create("mongodb://localhost:27017"); | |
var mongoDatabase = mongoServer.GetDatabase("openexchangerates"); | |
var mongoCollection = mongoDatabase.GetCollection("exchangerates"); | |
var document = new BsonDocument("rates", exchangeRates); | |
mongoCollection.Save(document); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is RestClient ?