forked from chrisshiplet/btcbar
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// HuobiCNYFetcher.h | ||
// btcbar | ||
// | ||
// Created by lwei on 2/13/14. | ||
// Copyright (c) 2014 nearengine. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "Fetcher.h" | ||
|
||
@interface HaobtcInstantCNYFetcher : NSObject<Fetcher, NSURLConnectionDelegate> | ||
|
||
@property (nonatomic) NSString* ticker; | ||
@property (nonatomic) NSString* ticker_menu; | ||
@property (nonatomic) NSString* url; | ||
@property (nonatomic) NSError* error; | ||
@property (nonatomic) NSMutableData *responseData; | ||
|
||
@end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// | ||
// HuobiCNYFetcher.m | ||
// btcbar | ||
// | ||
// Created by lwei on 2/13/14. | ||
// Copyright (c) 2014 nearengine. All rights reserved. | ||
// | ||
|
||
#import "HaobtcInstantCNYFetcher.h" | ||
|
||
@implementation HaobtcInstantCNYFetcher | ||
|
||
- (id)init | ||
{ | ||
if (self = [super init]) | ||
{ | ||
// Menu Item Name | ||
self.ticker_menu = @"Haobtc Benchmark"; | ||
|
||
// Website location | ||
self.url = @"https://haobtc.com/?from=1NDnnWCUu926z4wxA3sNBGYWNQD3mKyes8"; | ||
|
||
// Immediately request first update | ||
[self requestUpdate]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
// Override Ticker setter to trigger status item update | ||
- (void)setTicker:(NSString *)tickerString | ||
{ | ||
// Update the ticker value | ||
_ticker = tickerString; | ||
|
||
// Trigger notification to update ticker | ||
[[NSNotificationCenter defaultCenter] postNotificationName:@"btcbar_ticker_update" object:self]; | ||
} | ||
|
||
// Initiates an asyncronous HTTP connection | ||
- (void)requestUpdate | ||
{ | ||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://haobtc.com/api/v1/price/cny"]]; | ||
|
||
// Set the request's user agent | ||
[request addValue:@"btcbar/2.0 (HaobtcCNYFetcher)" forHTTPHeaderField:@"User-Agent"]; | ||
|
||
// Initialize a connection from our request | ||
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; | ||
|
||
// Go go go | ||
[connection start]; | ||
} | ||
|
||
// Initializes data storage on request response | ||
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response | ||
{ | ||
self.responseData = [[NSMutableData alloc] init]; | ||
} | ||
|
||
// Appends response data | ||
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data | ||
{ | ||
[self.responseData appendData:data]; | ||
} | ||
|
||
// Indiciate no caching | ||
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse | ||
{ | ||
return nil; | ||
} | ||
|
||
// Parse data after load | ||
- (void)connectionDidFinishLoading:(NSURLConnection *)connection | ||
{ | ||
NSString *responseStr = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]; | ||
if (!responseStr) { | ||
return; | ||
} | ||
|
||
// Parse the JSON into results | ||
NSError *jsonParsingError = nil; | ||
id results = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&jsonParsingError]; | ||
|
||
// Results parsed successfully from JSON | ||
if (results) | ||
{ | ||
NSString *sell_price = [results objectForKey:@"buy"]; | ||
NSString *buy_price = [results objectForKey:@"sell"]; | ||
if (sell_price && buy_price) { | ||
// NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; | ||
// NSString *resultsStatus = [numberFormatter stringFromNumber:sell_price]; | ||
// resultsStatus = [NSString stringWithFormat:@"¥%@", resultsStatus]; | ||
// | ||
// self.error = nil; | ||
// self.ticker = resultsStatus; | ||
|
||
NSString *sell_price_str = [NSString stringWithFormat:@"/%@",sell_price]; | ||
NSString *buy_price_str = [NSString stringWithFormat:@"¥%@",buy_price]; | ||
|
||
NSString *resultsStatus = [buy_price_str stringByAppendingString:sell_price_str]; | ||
|
||
NSLog(resultsStatus,nil); | ||
|
||
self.ticker = resultsStatus; | ||
|
||
} | ||
// Otherwise log an error... | ||
else | ||
{ | ||
self.error = [NSError errorWithDomain:@"com.nearengine.btcbar" code:0 userInfo:[NSDictionary dictionaryWithObjectsAndKeys: @"API Error", NSLocalizedDescriptionKey, @"The JSON received did not contain a result or the API returned an error.", NSLocalizedFailureReasonErrorKey, nil]]; | ||
self.ticker = nil; | ||
} | ||
} | ||
// JSON parsing failed | ||
else | ||
{ | ||
self.error = [NSError errorWithDomain:@"com.nearengine.btcbar" code:0 userInfo:[NSDictionary dictionaryWithObjectsAndKeys: @"JSON Error", NSLocalizedDescriptionKey, @"Could not parse the JSON returned.", NSLocalizedFailureReasonErrorKey, nil]]; | ||
self.ticker = nil; | ||
} | ||
} | ||
|
||
// HTTP request failed | ||
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error | ||
{ | ||
self.error = [NSError errorWithDomain:@"com.nearengine.btcbar" code:0 userInfo:[NSDictionary dictionaryWithObjectsAndKeys: @"Connection Error", NSLocalizedDescriptionKey, @"Could not connect to Haobtc.", NSLocalizedFailureReasonErrorKey, nil]]; | ||
self.ticker = nil; | ||
} | ||
|
||
@end |
This file contains 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