9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AFQiitaClient の使い方

Last updated at Posted at 2013-02-01

※ 元記事を誤って削除してしまったので、再投稿です。

AFQiitaClientQiita API を Cocoa で操るクライアントです

AFQiitaClientAFHTTPClient サブクラスで、AFNetworking のソースコードと共に、プロジェクトに追加して使用します。

CocoaPods からも追加できます。

pod 'AFQiitaClient'

使い方

初期化

AFQiitaClient *client = [[AFQiitaClient alloc] init];

認証

[client authenticateWithUsername:@"ngs"
                        password:@"qwerty1234!?"
                        success:^ {
                          NSLog(@"Success");
                        }
                        failure:^(NSError *error) {
                          NSLog(@"Error: %@", error);
                        }];

ページング

AFQiitaResponse には firstURL, prevURL, nextURL, prevURL という NSURL を返却するインスタンスメソッドがある。

[client tagsWithSuccess:^(AFQiitaResponse *response) {
                  if(response.nextURL) {
                    [client getURL:response.nextURL
                           success:^(AFQiitaResponse *response) { ... }
                           failure:^(NSError *error) { ... }];
                  } else {
                    NSLog(@"No more tags");
                  }
                }
                failure:^(NSError *error) {
                  NSLog(@"Error: %@", error);
                }];

残りリクエスト可能数とRate Limit取得

[client rateLimitWithSuccess:^(NSInteger remaining, NSInteger limit){
                        NSLog(@"Success: Limted to %d, %d remaining", limit, remaining);
                      }
                      failure:^(NSError *error) {
                        NSLog(@"Error: %@", error);
                      }];

リクエストユーザーの情報取得

[client currentUserWithSuccess:^(AFQiitaResponse *response) {
                         AFQiitaUser *me = [response first];
                         NSLog(@"Success: Hello, my name is %@", me.name);
                       }
                       failure:^(NSError *error) {
                         NSLog(@"Error: %@", error);
                       }];

特定ユーザーの情報取得

[client userWithUsername:@"yaotti@github"
                 success:^(AFQiitaResponse *response) {
                   AFQiitaUser *user = [response first];
                   NSLog(@"Success: %@ has %d followers", user.name, user.followers);
                 }
                 failure:^(NSError *error) {
                   NSLog(@"Error: %@", error);
                 }];

特定ユーザーの投稿取得

[client itemsWithUsername:@"yaotti@github"
                  success:^(AFQiitaResponse *response) {
                    AFQiitaItem *item = nil;
                    while(item = response.next)
                      NSLog(@"Post: %@", item.title);
                  }
                  failure:^(NSError *error) {
                    NSLog(@"Error: %@", error);
                  }];

特定ユーザーのストックした投稿取得

[client stockedItemsWithUsername:@"yaotti@github"
                         success:^(AFQiitaResponse *response) {
                           AFQiitaItem *item = nil;
                           while(item = response.next)
                             NSLog(@"Post: %@", item.title);
                         }
                         failure:^(NSError *error) {
                           NSLog(@"Error: %@", error);
                         }];

特定タグの投稿取得

[client itemsWithTag:@"Rails"
             success:^(AFQiitaResponse *response) {
               AFQiitaItem *item = nil;
               while(item = response.next)
                 NSLog(@"Post: %@", item.title);
             }
             failure:^(NSError *error) {
               NSLog(@"Error: %@", error);
             }];

タグ一覧取得

[client tagsWithSuccess:^(AFQiitaResponse *response) {
                  AFQiitaTag *tag = nil;
                  while(tag = response.next)
                    NSLog(@"Tag: %@", tag.name);
                }
                failure:^(NSError *error) {
                  NSLog(@"Error: %@", error);
                }];

検索結果取得

[client itemsWithSearchString:@"Hackathon"
                      stocked:NO
                      success:^(AFQiitaResponse *response) {
                        AFQiitaItem *item = nil;
                        while(item = response.next)
                          NSLog(@"Post: %@", item.title);
                      }
                      failure:^(NSError *error) {
                        NSLog(@"Error: %@", error);
                      }];

新着投稿の取得

[client recentItemsWithSuccess:^(AFQiitaResponse *response) {
                         AFQiitaItem *item = nil;
                         while(item = response.next)
                           NSLog(@"Post: %@", item.title);
                       }
                       failure:^(NSError *error) {
                         NSLog(@"Error: %@", error);
                       }];

自分のストックした投稿の取得

[client stockedItemsWithSuccess:^(AFQiitaResponse *response) {
                          AFQiitaItem *item = nil;
                          while(item = response.next)
                            NSLog(@"Post: %@", item.title);
                        }
                        failure:^(NSError *error) {
                          NSLog(@"Error: %@", error);
                        }];

投稿の実行

AFQiitaItem *item = [[AFQiitaItem alloc]
                      initWithTitle:@"テスト!"
                      body:@"[AFQiitaClient](http://github.com/ngs/AFQiitaClient) から投稿テスト"];

[item addTag:[AFQiitaTag tagWithName:@"iOS"
                            versions:@"5.1.1", @"6.0", nil]];

[item setTweet:YES]; // Posts URL to Twitter
[item setGist:YES];  // Share code blocks on Gist

[client createItem:item
           success:^(AFQiitaResponse *response) {
             AFQiitaItem *createdItem = [response first];
             NSLog(@"Success! UUID is %@", createdItem.uuid);
           }
           failure:^(NSError *error) {
             NSLog(@"Error: %@", error);
           }];

投稿の更新

AFQiitaItem *item = ...; // Received from API

[item setTitle:@"テスト!(追記アリ)"];
[item setBody:@"[AFQiitaClient](http://github.com/ngs/AFQiitaClient) から投稿テスト\n\n##追記 (2012/10/13)\n\n* 非公開にしてみた"];

[item removeTag:[item.tags objectAtIndex:2]];
[item addTag:[AFQiitaTag tagWithName:@"Rails"]];

[item publicize]; // 限定公開のものを public に変更のみ可能

[client updateItem:item
           success:^(AFQiitaResponse *response) {
             AFQiitaItem *updatedItem = [response first];
             NSLog(@"Success! Updated at %@", updatedItem.updatedAt);
           }
           failure:^(NSError *error) {
             NSLog(@"Error: %@", error);
           }];

投稿の削除

[client deleteItem:item // Alias for deleteItemWithUUID:(NSString *)uuid
           success:^(AFQiitaResponse *response) {
             NSLog(@"Successfully deleted");
           }
           failure:^(NSError *error) {
             NSLog(@"Error: %@", error);
           }];

特定の投稿取得

[client itemWithUUID:@"1a43e55e7209c8f3c565"
             success:^ {
             AFQiitaItem *item = [response first];
               NSLog(@"Success: Title is %@, posted by %@ at %@",
                 item.title, item.user.name, item.createdAt);
             }
             failure:^(NSError *error) {
               NSLog(@"Error: %@", error);
             }];

投稿のストック

[client stockItem:item // Alias for stockItemWithUUID:(NSString *)uuid
          success:^ {
            NSLog(@"Successfully stocked");
          }
          failure:^(NSError *error) {
            NSLog(@"Error: %@", error);
          }];

投稿のストック解除

[client unstockItem:item // Alias for unstockItemWithUUID:(NSString *)uuid
            success:^ {
              NSLog(@"Successfully unstocked");
            }
            failure:^(NSError *error) {
              NSLog(@"Error: %@", error);
            }];

連絡先

Atsushi Nagase

@ngs

ライセンス

AFQiitaClient は MIT ライセンスで配布しています。 詳しくは LICENSE ファイルをご覧ください。

9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?