Skip to content

Commit

Permalink
Separated YQL into his own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermechapiewski committed Oct 19, 2012
1 parent 0ae349d commit 2a963d4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 30 deletions.
8 changes: 8 additions & 0 deletions yql-ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
BB1775851631B65C00659E8A /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BB1775601631B65C00659E8A /* Foundation.framework */; };
BB17758D1631B65C00659E8A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = BB17758B1631B65C00659E8A /* InfoPlist.strings */; };
BB1775901631B65C00659E8A /* yql_iosTests.m in Sources */ = {isa = PBXBuildFile; fileRef = BB17758F1631B65C00659E8A /* yql_iosTests.m */; };
BB17759B1631DC0E00659E8A /* YQL.m in Sources */ = {isa = PBXBuildFile; fileRef = BB17759A1631DC0D00659E8A /* YQL.m */; };
BB17759C1631DC0E00659E8A /* YQL.m in Sources */ = {isa = PBXBuildFile; fileRef = BB17759A1631DC0D00659E8A /* YQL.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -58,6 +60,8 @@
BB17758C1631B65C00659E8A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
BB17758E1631B65C00659E8A /* yql_iosTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = yql_iosTests.h; sourceTree = "<group>"; };
BB17758F1631B65C00659E8A /* yql_iosTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = yql_iosTests.m; sourceTree = "<group>"; };
BB1775991631DC0D00659E8A /* YQL.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YQL.h; sourceTree = "<group>"; };
BB17759A1631DC0D00659E8A /* YQL.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YQL.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -123,6 +127,8 @@
BB1775791631B65C00659E8A /* ViewController.h */,
BB17757A1631B65C00659E8A /* ViewController.m */,
BB1775651631B65C00659E8A /* Supporting Files */,
BB1775991631DC0D00659E8A /* YQL.h */,
BB17759A1631DC0D00659E8A /* YQL.m */,
);
path = "yql-ios";
sourceTree = "<group>";
Expand Down Expand Up @@ -273,6 +279,7 @@
BB17756B1631B65C00659E8A /* main.m in Sources */,
BB17756F1631B65C00659E8A /* AppDelegate.m in Sources */,
BB17757B1631B65C00659E8A /* ViewController.m in Sources */,
BB17759B1631DC0E00659E8A /* YQL.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -281,6 +288,7 @@
buildActionMask = 2147483647;
files = (
BB1775901631B65C00659E8A /* yql_iosTests.m in Sources */,
BB17759C1631DC0E00659E8A /* YQL.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
6 changes: 5 additions & 1 deletion yql-ios/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
//

#import <UIKit/UIKit.h>
#import "YQL.h"

@interface ViewController : UIViewController
@interface ViewController : UIViewController {
YQL *yql;
}

@property (nonatomic, retain) IBOutlet UITextView *QueryTextView;
@property (nonatomic, retain) IBOutlet UITextView *ResultsTextView;
@property (strong, nonatomic) YQL *yql;

-(IBAction)Run_OnClick:(id)sender;

Expand Down
38 changes: 9 additions & 29 deletions yql-ios/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
//

#import "ViewController.h"

#define QUERY_PREFIX @"http://query.yahooapis.com/v1/public/yql?q="
#define QUERY_SUFFIX @"&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="
#import "YQL.h"

@interface ViewController ()

Expand All @@ -18,40 +16,22 @@ @interface ViewController ()
@implementation ViewController
@synthesize QueryTextView;
@synthesize ResultsTextView;
@synthesize yql;

- (void)viewDidLoad
{
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// Init YQL
yql = [[YQL alloc] init];
}

- (void)didReceiveMemoryWarning
{
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(IBAction)backgroundTouched:(id)sender {
[QueryTextView resignFirstResponder];
[ResultsTextView resignFirstResponder];
}

-(IBAction)Run_OnClick:(id)sender {
[sender resignFirstResponder];

NSString *query = [NSString stringWithFormat:@"%@%@%@", QUERY_PREFIX, [QueryTextView.text stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], QUERY_SUFFIX];

NSLog(@"Request URL: %@", query);

NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;
if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);

NSLog(@"Results: %@", [results valueForKeyPath:@"query.results"]);

// lame :(
ResultsTextView.text = [NSString stringWithFormat:@"%@", [results valueForKeyPath:@"query.results"]];
NSString *results = [yql query:QueryTextView.text];
ResultsTextView.text = results;
}

@end
15 changes: 15 additions & 0 deletions yql-ios/YQL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// YQL.h
// yql-ios
//
// Created by Guilherme Chapiewski on 10/19/12.
// Copyright (c) 2012 Guilherme Chapiewski. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface YQL : NSObject

- (NSString *)query:(NSString *)statement;

@end
36 changes: 36 additions & 0 deletions yql-ios/YQL.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// YQL.m
// yql-ios
//
// Created by Guilherme Chapiewski on 10/19/12.
// Copyright (c) 2012 Guilherme Chapiewski. All rights reserved.
//

#import "YQL.h"

#define QUERY_PREFIX @"http://query.yahooapis.com/v1/public/yql?q="
#define QUERY_SUFFIX @"&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="

@implementation YQL

- (NSString *) query: (NSString *)statement {
NSLog(@"Start yql call");

NSString *query = [NSString stringWithFormat:@"%@%@%@", QUERY_PREFIX, [statement stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], QUERY_SUFFIX];

NSLog(@"Request URL: %@", query);

NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;

if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);

NSLog(@"Results: %@", [results valueForKeyPath:@"query.results"]);

NSLog(@"Done yql call");

return [NSString stringWithFormat:@"%@", [results valueForKeyPath:@"query.results"]];
}

@end

0 comments on commit 2a963d4

Please sign in to comment.