pupil 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
1
+ class Pupil
2
+ # @return [Array] Timeline
3
+ # @param [Hash] param
4
+ # @option param [Fixnum] :count Number of tweets
5
+ # @option param [Fixnum] :since_id
6
+ # @option param [Fixnum] :max_id
7
+ # @option param [Fixnum] :page
8
+ # @option param [Symbol] :trim_user
9
+ # @option param [Symbol] :include #=> [:rts]
10
+ # @option param [Symbol] :exclude #=> [:replies]
11
+ # @option param [Symbol] :contributor_details
12
+ def home_timeline param={}
13
+ response = self.get("/statuses/home_timeline.json", param)
14
+ statuses = Array.new
15
+ response.each do |element|
16
+ status = Status.new element
17
+ statuses << status
18
+ end
19
+ return statuses
20
+ end
21
+
22
+ # @return [Hash] mention
23
+ # @param [Hash] param
24
+ def mentions param={}
25
+ response = self.get("/statuses/mentions.json", param)
26
+ statuses = Array.new
27
+ response.each do |element|
28
+ status = Status.new element
29
+ statuses << status
30
+ end
31
+ return statuses
32
+ end
33
+
34
+ # Returning user timeline
35
+ # @return [Hash] timeline
36
+ # @param [Hash] param
37
+ # @option param [Fixnum] :user_id The ID of user
38
+ # @option param [String] :screen_name The Screen name of user
39
+ # @option param [Fixnum] :since_id
40
+ # @option param [Fixnum] :max_id
41
+ # @option param [Fixnum] :count
42
+ # @option param [Fixnum] :page Specifies
43
+ # @option param [Symbol] :trim_user
44
+ # @option param [Symbol] :include #=> [:rts]
45
+ # @option param [Symbol] :exclude #=> [:replies]
46
+ # @option param [Symbol] :contributor_details
47
+ # @example
48
+ # twitter = Pupil.new PUPIL_KEY
49
+ # twitter.user_timeline(:screen_name => 'o_ame', :exclude => :replies).each do |status|
50
+ # puts "#{status.user.screen_name}: #{status.text}"
51
+ # end
52
+ def user_timeline param={}
53
+ response = self.get("/statuses/user_timeline.json", param)
54
+ statuses = Array.new
55
+ response.each do |element|
56
+ status = Status.new element
57
+ statuses << status
58
+ end
59
+ return statuses
60
+ end
61
+
62
+ def show_status status_id
63
+ response = @access_token.get("/statuses/show/#{status_id}.json").body
64
+ return response
65
+ status = Status.new response
66
+ return status
67
+ end
68
+
69
+ def update(status, irt='')
70
+ response = self.post(
71
+ "/statuses/update.json",
72
+ "status"=> status,
73
+ "in_reply_to_status_id" => irt
74
+ )
75
+ return response
76
+ end
77
+
78
+ alias_method :tweet, :update
79
+
80
+ def destroy status_id
81
+ response = self.post("/statuses/destroy/#{status_id}.json")
82
+ return response
83
+ end
84
+ end
@@ -0,0 +1,94 @@
1
+ class Pupil
2
+ class Stream
3
+ class StreamError < StandardError ; end
4
+ STREAM_APIS = {
5
+ :userstream => "https://userstream.twitter.com/2/user.json",
6
+ :filter => "https://stream.twitter.com/1/statuses/filter.json%s"
7
+ }
8
+
9
+ def initialize key
10
+ @screen_name = key[:screen_name]
11
+ @client = nil
12
+ @config = nil
13
+
14
+ @consumer = OAuth::Consumer.new(
15
+ key[:consumer_key],
16
+ key[:consumer_secret],
17
+ :site => TWITTER_API_URL
18
+ )
19
+ @access_token = OAuth::AccessToken.new(
20
+ @consumer,
21
+ key[:access_token],
22
+ key[:access_token_secret]
23
+ )
24
+ end
25
+
26
+ def start(type, param=nil, &block)
27
+ raise ArgumentError unless block_given?
28
+
29
+ run_get_stream type, param, &block
30
+ end
31
+
32
+ def run_get_stream(type, param=nil, &block)
33
+ uri = URI.parse(STREAM_APIS[type] % Pupil.param_serializer(param))
34
+ https = Net::HTTP.new(uri.host, uri.port)
35
+ https.use_ssl = true
36
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
37
+ https.verify_depth = 5
38
+
39
+ while true do
40
+ begin
41
+ https.start do |https|
42
+ request = Net::HTTP::Get.new(uri.request_uri)
43
+ request["User-Agent"] = "Ruby/#{RUBY_VERSION} Pupil::Stream"
44
+ request.oauth!(https, @consumer, @access_token)
45
+ buf = ""
46
+ https.request(request) do |response|
47
+ response.read_body do |chunk|
48
+ buf << chunk
49
+ while (line = buf[/.+?(\r\n)+/m]) != nil
50
+ begin
51
+ buf.sub!(line,"")
52
+ line.strip!
53
+ status = JSON.parse(line)
54
+ rescue
55
+ break
56
+ end
57
+
58
+ block.call StreamEvent.new(status)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ rescue => vars
64
+ raise StreamError, "StreamError: #{vars}"
65
+ end
66
+ end
67
+ end
68
+
69
+ class StreamEvent
70
+ attr_reader :type
71
+ attr_reader :content
72
+
73
+ def initialize status
74
+ @type, @content = self.guess_event status
75
+ end
76
+
77
+ def guess_event status
78
+ if status["delete"]
79
+ return :delete, status["delete"]["status"]
80
+ elsif status["friends"]
81
+ return :friends, status["friends"]
82
+ elsif status["event"] == "favorite"
83
+ return :favorite, status
84
+ elsif status["retweeted_status"]
85
+ return :retweet, status
86
+ elsif status["text"]
87
+ return :status, status#Pupil::Status.new(status)
88
+ else
89
+ return :unknown, status
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,13 @@
1
+ class Pupil
2
+ def lookup param={}
3
+ response = self.get("/users/lookup.json", param)
4
+ users = Array.new
5
+ response.each do |element|
6
+ user = User.new element
7
+ users << user
8
+ end
9
+ return users
10
+ end
11
+
12
+
13
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "pupil"
8
- s.version = "0.0.6"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Oame"]
12
- s.date = "2011-10-22"
12
+ s.date = "2011-12-15"
13
13
  s.description = "The \"Lazy\" Twitter API Wrapper for Ruby 1.9.2"
14
14
  s.email = "oame@oameya.com"
15
15
  s.extra_rdoc_files = [
@@ -26,9 +26,18 @@ Gem::Specification.new do |s|
26
26
  "Rakefile",
27
27
  "VERSION",
28
28
  "lib/pupil.rb",
29
- "lib/pupil/keygen.rb",
29
+ "lib/pupil/account.rb",
30
+ "lib/pupil/base.rb",
31
+ "lib/pupil/blocks.rb",
32
+ "lib/pupil/direct_messages.rb",
33
+ "lib/pupil/friendships.rb",
34
+ "lib/pupil/keygen/keygen.rb",
35
+ "lib/pupil/lists.rb",
36
+ "lib/pupil/schemes.rb",
37
+ "lib/pupil/statuses.rb",
38
+ "lib/pupil/stream.rb",
39
+ "lib/pupil/users.rb",
30
40
  "pupil.gemspec",
31
- "samples/ff-dumper.rb",
32
41
  "samples/generate-key.rb",
33
42
  "spec/pupil_spec.rb",
34
43
  "spec/spec_helper.rb"
@@ -44,6 +53,7 @@ Gem::Specification.new do |s|
44
53
 
45
54
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
55
  s.add_runtime_dependency(%q<oauth>, [">= 0"])
56
+ s.add_runtime_dependency(%q<json>, [">= 0"])
47
57
  s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
48
58
  s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
49
59
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -51,6 +61,7 @@ Gem::Specification.new do |s|
51
61
  s.add_development_dependency(%q<rcov>, [">= 0"])
52
62
  else
53
63
  s.add_dependency(%q<oauth>, [">= 0"])
64
+ s.add_dependency(%q<json>, [">= 0"])
54
65
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
55
66
  s.add_dependency(%q<yard>, ["~> 0.6.0"])
56
67
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -59,6 +70,7 @@ Gem::Specification.new do |s|
59
70
  end
60
71
  else
61
72
  s.add_dependency(%q<oauth>, [">= 0"])
73
+ s.add_dependency(%q<json>, [">= 0"])
62
74
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
63
75
  s.add_dependency(%q<yard>, ["~> 0.6.0"])
64
76
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -2,8 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  require "rubygems" if RUBY_VERSION < "1.9.0"
5
- $LOAD_PATH << File.join(File.dirname(File.expand_path(__FILE__)), "../")
6
- require "lib/pupil"
5
+ require "pupil"
7
6
 
8
7
  keygen = Pupil::Keygen.new
9
8
  keygen.interactive
@@ -9,21 +9,142 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
9
9
  describe Pupil, "が #home_timeline を呼ぶ時は" do
10
10
  before do
11
11
  pupil = Pupil.new PUPIL_TESTKEY
12
- @home_timeline = pupil.home_timeline
12
+ @home_timeline = pupil.home_timeline :count => 50
13
13
  end
14
14
 
15
15
  it "Array型を返すこと" do
16
16
  @home_timeline.class.should == Array
17
17
  end
18
+
19
+ it "sizeが50であること" do
20
+ @home_timeline.size.should == 50
21
+ end
18
22
  end
19
23
 
20
24
  describe Pupil, "が #mentions を呼ぶ時は" do
21
25
  before do
22
26
  pupil = Pupil.new PUPIL_TESTKEY
23
- @mentions = pupil.mentions
27
+ @mentions = pupil.mentions :count => 10
24
28
  end
25
29
 
26
30
  it "Array型を返すこと" do
27
31
  @mentions.class.should == Array
28
32
  end
33
+
34
+ it "sizeが50であること" do
35
+ @mentions.size.should == 10
36
+ end
37
+ end
38
+
39
+ describe Pupil, "が #verify_credentials を呼ぶ時は" do
40
+ before do
41
+ pupil = Pupil.new PUPIL_TESTKEY
42
+ @vc = pupil.verify_credentials
43
+ end
44
+
45
+ it "Pupil::User型を返すこと" do
46
+ @vc.class.should == Pupil::User
47
+ end
48
+ end
49
+
50
+ describe Pupil, "が #user_timeline を呼ぶ時は" do
51
+ before do
52
+ pupil = Pupil.new PUPIL_TESTKEY
53
+ @user_timeline = pupil.user_timeline :screen_name => pupil.screen_name
54
+ end
55
+
56
+ it "Array型を返すこと" do
57
+ @user_timeline.class.should == Array
58
+ end
59
+ end
60
+
61
+ describe Pupil, "が #friendship? を呼ぶ時は" do
62
+ before do
63
+ pupil = Pupil.new PUPIL_TESTKEY
64
+ @nonfriendship = pupil.friendship? pupil.screen_name, KNOWN_NONFOLLOWED_USER
65
+ @friendship = pupil.friendship? pupil.screen_name, KNOWN_USER
66
+ end
67
+
68
+ it "フォロー関係に無い場合はFalseClass型を返すこと" do
69
+ @nonfriendship.class.should == FalseClass
70
+ end
71
+
72
+ it "フォロー関係にある場合はTrueClass型を返すこと" do
73
+ @friendship.class.should == TrueClass
74
+ end
29
75
  end
76
+
77
+ describe Pupil, "が #follow を呼ぶ時は" do
78
+ before do
79
+ pupil = Pupil.new PUPIL_TESTKEY
80
+ @follow = pupil.follow :screen_name => KNOWN_NONFOLLOWED_USER
81
+ @follow_fail = pupil.follow :screen_name => UNKNOWN_USER
82
+ end
83
+
84
+ it "フォローに成功した場合はPupil::User型を返すこと" do
85
+ @follow.class.should == Pupil::User
86
+ end
87
+
88
+ it "フォローに失敗した場合はFalseClass型を返すこと" do
89
+ @follow_fail.class.should == FalseClass
90
+ end
91
+ end
92
+
93
+ describe Pupil, "が #unfollow を呼ぶ時は" do
94
+ before do
95
+ pupil = Pupil.new PUPIL_TESTKEY
96
+ @unfollow = pupil.unfollow :screen_name => KNOWN_NONFOLLOWED_USER
97
+ @unfollow_fail = pupil.unfollow :screen_name => UNKNOWN_USER
98
+ end
99
+
100
+ it "成功した場合はPupil::User型を返すこと" do
101
+ @unfollow.class.should == Pupil::User
102
+ end
103
+
104
+ it "失敗した場合にはFalseClassを返すこと" do
105
+ @unfollow_fail.class.should == FalseClass
106
+ end
107
+ end
108
+
109
+ describe Pupil, "が #block を呼ぶ時は" do
110
+ before do
111
+ pupil = Pupil.new PUPIL_TESTKEY
112
+ @block = pupil.block :screen_name => KNOWN_NONFOLLOWED_USER
113
+ @block_fail = pupil.block :screen_name => UNKNOWN_USER
114
+ end
115
+
116
+ it "成功した場合はPupil::User型を返すこと" do
117
+ @block.class.should == Pupil::User
118
+ end
119
+
120
+ it "失敗した場合にはFalseClassを返すこと" do
121
+ @block_fail.class.should == FalseClass
122
+ end
123
+ end
124
+
125
+ describe Pupil, "が #unblock を呼ぶ時は" do
126
+ before do
127
+ pupil = Pupil.new PUPIL_TESTKEY
128
+ @unblock = pupil.unblock :screen_name => KNOWN_NONFOLLOWED_USER
129
+ @unblock_fail = pupil.unblock :screen_name => UNKNOWN_USER
130
+ end
131
+
132
+ it "成功した場合はPupil::User型を返すこと" do
133
+ @unblock.class.should == Pupil::User
134
+ end
135
+
136
+ it "失敗した場合にはFalseClassを返すこと" do
137
+ @unblock_fail.class.should == FalseClass
138
+ end
139
+ end
140
+
141
+ describe Pupil, "が #blocking を呼ぶ時は" do
142
+ before do
143
+ pupil = Pupil.new PUPIL_TESTKEY
144
+ @blocking = pupil.blocking
145
+ end
146
+
147
+ it "Array型を返すこと" do
148
+ @blocking.class.should == Array
149
+ end
150
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pupil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-22 00:00:00.000000000Z
12
+ date: 2011-12-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth
16
- requirement: &70230268043280 !ruby/object:Gem::Requirement
16
+ requirement: &70343823054920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70230268043280
24
+ version_requirements: *70343823054920
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &70343823033660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70343823033660
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rspec
27
- requirement: &70230268042620 !ruby/object:Gem::Requirement
38
+ requirement: &70343823032620 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ~>
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: 2.3.0
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *70230268042620
46
+ version_requirements: *70343823032620
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: yard
38
- requirement: &70230268042080 !ruby/object:Gem::Requirement
49
+ requirement: &70343823031540 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ~>
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: 0.6.0
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *70230268042080
57
+ version_requirements: *70343823031540
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: bundler
49
- requirement: &70230268041480 !ruby/object:Gem::Requirement
60
+ requirement: &70343823029980 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ~>
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: 1.0.0
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *70230268041480
68
+ version_requirements: *70343823029980
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: jeweler
60
- requirement: &70230268040900 !ruby/object:Gem::Requirement
71
+ requirement: &70343823027780 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ~>
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: 1.6.4
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *70230268040900
79
+ version_requirements: *70343823027780
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: rcov
71
- requirement: &70230268040240 !ruby/object:Gem::Requirement
82
+ requirement: &70343823026600 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,7 +87,7 @@ dependencies:
76
87
  version: '0'
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *70230268040240
90
+ version_requirements: *70343823026600
80
91
  description: The "Lazy" Twitter API Wrapper for Ruby 1.9.2
81
92
  email: oame@oameya.com
82
93
  executables: []
@@ -94,9 +105,18 @@ files:
94
105
  - Rakefile
95
106
  - VERSION
96
107
  - lib/pupil.rb
97
- - lib/pupil/keygen.rb
108
+ - lib/pupil/account.rb
109
+ - lib/pupil/base.rb
110
+ - lib/pupil/blocks.rb
111
+ - lib/pupil/direct_messages.rb
112
+ - lib/pupil/friendships.rb
113
+ - lib/pupil/keygen/keygen.rb
114
+ - lib/pupil/lists.rb
115
+ - lib/pupil/schemes.rb
116
+ - lib/pupil/statuses.rb
117
+ - lib/pupil/stream.rb
118
+ - lib/pupil/users.rb
98
119
  - pupil.gemspec
99
- - samples/ff-dumper.rb
100
120
  - samples/generate-key.rb
101
121
  - spec/pupil_spec.rb
102
122
  - spec/spec_helper.rb
@@ -115,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
135
  version: '0'
116
136
  segments:
117
137
  - 0
118
- hash: -2476195476726471591
138
+ hash: 4408204829077515658
119
139
  required_rubygems_version: !ruby/object:Gem::Requirement
120
140
  none: false
121
141
  requirements: