me2day-ruby 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown CHANGED
@@ -1,22 +1,47 @@
1
- An API client for me2day(http://me2day.net), a Twitter-like Korean popular social networking service.
1
+ An API client for me2day(http://me2day.net), a twitter-like popular Korean social networking service.
2
2
 
3
- Install
3
+ 설치(Installation)
4
4
  =====
5
5
 
6
6
  $ (sudo) gem install me2day-ruby
7
7
 
8
-
9
- me2day "Easy Authentication (Web-based)"
8
+ 기본 사용법(Basic Usage)
10
9
  =====
11
10
 
11
+ 우선 클라이언트를 생성합니다. 이 때 app_key, user_id, user_key가 필요합니다.
12
+ app_key는 미투데이로부터 부여받은 APPLICATION_KEY이며, user_id와 user_key는 인증을 통해 획득할 수 있습니다.
13
+
12
14
  require 'me2day'
13
-
14
- @auth_url = Me2day::Client.get_auth_url(:app_key => "YOUR_ME2DAY_APPLICATION_KEY")
15
+
16
+ @client = Me2day::Client.new(
17
+ :user_id => user_id,
18
+ :user_key => user_key,
19
+ :app_key => "YOUR_ME2DAY_APPLICATION_KEY"
20
+ )
21
+
22
+ 생성된 클라이언트에 대해 메서드를 호출합니다.
23
+ 미투데이 사용설명서에 있는 모든 메서드를 호출할 수 있으며, 모든 파라미터를 이름 그래도 사용하면 됩니다.
24
+ 자세한 사용법은 미투데이 사용설명서를 참고하시면 됩니다.
25
+
26
+ @client.noop
27
+ @client.create_post "me2_id", 'post[body]' => "안녕하세요, 미투데이!"
28
+ @client.get_comments :post_id => '123456'
29
+ @client.get_person "me2_id"
30
+ @client.get_friends "me2_id"
31
+
32
+ 웹 기반 쉬운 인증
33
+ =====
34
+
35
+ 미투데이는 웹 기반 쉬운 인증이라는 자체 인증방식을 제공하며, 이 방법을 사용하면 사용자의 user_id와 user_key를 쉽게 얻을 수 있습니다.
36
+
37
+ 1. 인증 URL을 얻습니다.
38
+
39
+ auth_url = Me2day::Client.get_auth_url(:app_key => "YOUR_ME2DAY_APPLICATION_KEY")
15
40
  => "http://me2day.net/api/start_auth?token=XXXXXXXXXXXXXX"
16
41
 
17
- .. then redirection to the auth_url
18
- .. and then user accept the auth
19
- .. and then in callback
42
+ 2. auth_url로 리다이렉션(redirect) 합니다.
43
+
44
+ 3. 설정한 콜백(callback)에서 다음과 같이 처리 합니다.
20
45
 
21
46
  token, user_id, user_key, result = request.params["token"], request.params["user_id"], request.params["user_key"], request.params["result"]
22
47
 
@@ -28,23 +53,9 @@ me2day "Easy Authentication (Web-based)"
28
53
  :app_key => "YOUR_ME2DAY_APPLICATION_KEY"
29
54
  )
30
55
 
31
- @client.get("/noop")
32
- => {"error"=>{"code"=>"0", "description"=>nil, "message"=>"\354\204\261\352\263\265\355\226\210\354\212\265\353\213\210\353\213\244."}}
33
-
34
- OR
35
-
36
56
  @client.noop
37
57
  => {"error"=>{"code"=>"0", "description"=>nil, "message"=>"\354\204\261\352\263\265\355\226\210\354\212\265\353\213\210\353\213\244."}}
38
58
 
39
- OR
40
-
41
- client.noop(:format => 'json')
42
- => {"code"=>1007, "description"=>"\354\225\224\355\230\270\352\260\200 \354\235\274\354\271\230\355\225\230\354\247\200 \354\225\212\354\212\265\353\213\210\353\213\244.", "message"=>"\354\235\270\354\246\235 \354\213\244\355\214\250"}
43
-
44
- @client.post("/create_post/[me2_user_id]", :query => { 'post[body]' => "Hello! me2!!" })
45
-
46
- OR
47
-
48
59
  @client.create_post('me2_user_id', "Hi!, How are you!!")
49
60
 
50
61
  end
@@ -53,8 +64,7 @@ me2day "Easy Authentication (Web-based)"
53
64
  Todos
54
65
  =====
55
66
 
56
- * Add more methods
57
- * Error Handling
67
+ * Enhancing error handling
58
68
  * DRY cleaning!
59
69
 
60
70
 
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ Rake::TestTask.new("test:units") { |t|
12
12
  t.warning = true
13
13
  }
14
14
 
15
- Echoe.new('me2day-ruby', '0.1.1') do |p|
15
+ Echoe.new('me2day-ruby', '0.2.0') do |p|
16
16
  p.description = "a me2day API client"
17
17
  p.url = "https://github.com/sjoonk/me2day-ruby"
18
18
  p.author = "Sukjoon Kim"
data/lib/me2day/client.rb CHANGED
@@ -2,78 +2,76 @@ module Me2day
2
2
  class Client
3
3
  include HTTParty
4
4
  base_uri "http://me2day.net/api"
5
- DEFAULT_FORMAT = 'xml'
5
+ DEFAULT_FORMAT = 'json'
6
+ @@apis = %w(
7
+ accept_friendship_request
8
+ create_comment
9
+ create_post
10
+ delete_comment
11
+ delete_post
12
+ friendship
13
+ get_bands
14
+ get_comments
15
+ get_friends
16
+ get_friendship_requests
17
+ get_latests
18
+ get_metoos
19
+ get_person
20
+ get_posts
21
+ get_settings
22
+ metoo
23
+ noop
24
+ track_comments
25
+ )
26
+
27
+ class UnauthenticatedError < RuntimeError; end
28
+ class InternalServerError < RuntimeError; end
29
+
30
+ class << self
31
+ # attr_accessor :app_key
32
+
33
+ def get_auth_url(options={})
34
+ raise "app_key is required" unless options[:app_key]
35
+ self.headers 'me2_application_key' => options[:app_key]
36
+ @auth_url ||= get("/get_auth_url.json")["url"]
37
+ end
38
+ end
6
39
 
7
40
  def initialize(options={})
8
41
  @app_key = options[:app_key]
9
42
  @user_id = options[:user_id]
10
43
  @user_key = options[:user_key]
44
+ @auth = { :uid => @user_id, :ukey => u_key(@user_key) }
11
45
  self.class.headers 'me2_application_key' => @app_key
12
- self.class.default_params.merge!(:uid => @user_id, :ukey => u_key(@user_key))
13
46
  end
14
47
 
15
- def self.get_auth_url(options={})
16
- raise "app_key is required" unless options[:app_key]
17
- self.headers 'me2_application_key' => options[:app_key]
18
- get("/get_auth_url.json")["url"]
48
+ def method_missing(method_sym, *args, &block)
49
+ if @@apis.include?(method_sym.to_s)
50
+ id_part = args.first.is_a?(String) ? "/#{args.first}" : ''
51
+ options = args.last.is_a?(Hash) ? args.last : {}
52
+ format = options ? (options.delete(:format) || DEFAULT_FORMAT) : DEFAULT_FORMAT
53
+ options.merge!(@auth)
54
+ resp = self.class.get("/#{method_sym}#{id_part}.#{format}", :query => options)
55
+ case resp.response.code.to_s
56
+ when "200"
57
+ resp
58
+ when "401"
59
+ raise UnauthenticatedError, resp.parsed_response.to_s
60
+ when "500"
61
+ raise InternalServerError, resp.parsed_response.to_s
62
+ else
63
+ raise "Unknown Error", resp.inspect
64
+ end
65
+ else
66
+ super
67
+ end
19
68
  end
20
-
21
- # General helper methods
69
+
70
+ # General helper methods (deprecated)
22
71
 
23
72
  def get(path, options={}); self.class.get(path, options).parsed_response end
24
73
  def post(path, options={}); self.class.post(path, options).parsed_response end
25
74
 
26
- # API wrappers
27
-
28
- def accept_friendship_request(friendship_request_id, message)
29
- format = options.delete(:format) || DEFAULT_FORMAT
30
- post("/accept_friendship_request.#{format}",
31
- :query => {:friendship_request_id => friendship_request_id, :message => message})
32
- end
33
-
34
- def create_comment(post_id, body)
35
- format = options.delete(:format) || DEFAULT_FORMAT
36
- post("/create_comment.#{format}", :query => {:post_id => post_id, :body => body})
37
- end
38
-
39
- def create_post(user_id, post_body, options={})
40
- format = options.delete(:format) || DEFAULT_FORMAT
41
- post("/create_post/#{user_id}.#{format}", :query => options.merge('post[body]' => post_body))
42
- end
43
-
44
- def delete_comment(comment_id)
45
- format = options.delete(:format) || DEFAULT_FORMAT
46
- post("/delete_comment.#{format}", :query => {:comment_id => comment_id})
47
- end
48
-
49
- def delete_post(post_id)
50
- format = options.delete(:format) || DEFAULT_FORMAT
51
- post("/delete_post.#{format}", :query => {:post_id => post_id})
52
- end
53
-
54
- def friendship(user_id, options={})
55
- format = options.delete(:format) || DEFAULT_FORMAT
56
- scope = options.delete(:scope)
57
- value = options.delete(:value)
58
- post("/delete_post.#{format}", :query => {:user_id => user_id, :scope => scope, :value => value})
59
- end
60
-
61
- def get_bands(band_id, include_members=false)
62
- format = options.delete(:format) || DEFAULT_FORMAT
63
- get("/get_bands.#{format}", :query => {:band_id => band_id, :include_members => include_members.to_s})
64
- end
65
-
66
- def noop(options={})
67
- format = options.delete(:format) || DEFAULT_FORMAT
68
- get("/noop.#{format}")
69
- end
70
-
71
- def get_person(user_id)
72
- format = options.delete(:format) || DEFAULT_FORMAT
73
- get("/get_person/#{user_id}.#{format}")
74
- end
75
-
76
-
77
75
  private
78
76
 
79
77
  def u_key(user_key)
data/lib/me2day.rb CHANGED
@@ -1,8 +1,8 @@
1
- require 'digest/md5'
2
1
  require 'httparty'
2
+ require 'digest/md5'
3
3
  require 'me2day/client'
4
4
 
5
5
  module Me2day
6
- VERSION = '0.1.1'
6
+ VERSION = '0.2.0'
7
7
  end
8
8
 
data/me2day-ruby.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{me2day-ruby}
5
- s.version = "0.1.1"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Sukjoon Kim"]
9
- s.date = %q{2011-01-12}
9
+ s.date = %q{2011-01-22}
10
10
  s.description = %q{a me2day API client}
11
11
  s.email = %q{sjoonk@gmail.com}
12
12
  s.extra_rdoc_files = ["LICENSE", "README.markdown", "lib/me2day.rb", "lib/me2day/client.rb"]
@@ -15,11 +15,12 @@ Gem::Specification.new do |s|
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Me2day-ruby", "--main", "README.markdown"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{me2day-ruby}
18
- s.rubygems_version = %q{1.4.2}
18
+ s.rubygems_version = %q{1.3.7}
19
19
  s.summary = %q{a me2day API client}
20
20
  s.test_files = ["test/client_test.rb", "test/test_helper.rb"]
21
21
 
22
22
  if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
24
  s.specification_version = 3
24
25
 
25
26
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
data/test/client_test.rb CHANGED
@@ -2,17 +2,54 @@ require File.join(File.dirname(__FILE__), "/test_helper")
2
2
 
3
3
  class ClientTest < Test::Unit::TestCase
4
4
  include Me2day
5
+
6
+ context "get_auth_url" do
7
+ setup do
8
+ FakeWeb.register_uri(:get,
9
+ 'http://me2day.net/api/get_auth_url.json',
10
+ :body => "#{FIXTURES_DIR}/get_auth_url.json",
11
+ :content_type => "text/json")
12
+ end
13
+
14
+ should "require a app_key" do
15
+ lambda { Client.get_auth_url }.should raise_error
16
+ Client.get_auth_url(:app_key => 'YOUR_ME2DAY_APPLICATION_KEY').should == "http://me2day.net/api/start_auth?token=2d76abbbca0db990d17fd5e84227041e"
17
+ end
18
+ end
5
19
 
6
- context "A client instance" do
20
+ context "Calling api" do
21
+ setup do
22
+ @client = Client.new(
23
+ :user_id => "tester",
24
+ :user_key => "nonce_plus_md5_hash",
25
+ :app_key => "YOUR_ME2DAY_APPLICATION_KEY"
26
+ )
27
+ end
7
28
 
8
- setup do
9
- @client = Client.new
29
+ context "noop" do
30
+ setup do
31
+ FakeWeb.register_uri(:get, %r|http://me2day\.net/api/noop.json|,
32
+ :body => "#{FIXTURES_DIR}/noop.json", :content_type => "text/json")
33
+ end
34
+
35
+ should "return valid response code" do
36
+ @client.noop["code"].should == 0
37
+ end
10
38
  end
11
39
 
12
- should "be kind of Me2day client" do
13
- assert_kind_of Me2day::Client, @client
40
+ context "create_post" do
41
+ setup do
42
+ FakeWeb.register_uri(:get, %r|http://me2day\.net/api/create_post/sjoonk.json|,
43
+ :body => "#{FIXTURES_DIR}/create_post.json", :content_type => "text/json")
44
+ end
45
+
46
+ should "return valid response" do
47
+ post = @client.create_post("sjoonk", :'post[body]' => 'blablabla')
48
+ post['body'].should == 'blablabla'
49
+ end
14
50
  end
15
-
16
- end
51
+
52
+
53
+ end
17
54
 
18
55
  end
data/test/test_helper.rb CHANGED
@@ -1,10 +1,17 @@
1
1
  require 'rubygems'
2
+ require 'test/unit'
2
3
  require 'shoulda'
3
4
  require 'redgreen'
4
- require 'mocha'
5
+ require 'matchy'
6
+ require 'fakeweb'
5
7
 
6
- lib_files = File.join(File.dirname(__FILE__), "..", "lib")
8
+ FakeWeb.allow_net_connect = false # blocking all real requests
7
9
 
8
- Dir.glob(File.join(lib_files, "**")).each do |file|
9
- require file
10
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
11
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
+ FIXTURES_DIR = File.join(File.dirname(__FILE__), 'fixtures')
13
+
14
+ require 'me2day'
15
+
16
+ class Test::Unit::TestCase
10
17
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: me2day-ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease:
4
+ hash: 23
5
+ prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sukjoon Kim
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-12 00:00:00 +09:00
18
+ date: 2011-01-22 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements: []
90
90
 
91
91
  rubyforge_project: me2day-ruby
92
- rubygems_version: 1.4.2
92
+ rubygems_version: 1.3.7
93
93
  signing_key:
94
94
  specification_version: 3
95
95
  summary: a me2day API client