adn 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ ### Changelog
2
+
3
+ * **Version 0.3.5** (23 October 2012)
4
+ * Added the unified stream
5
+ * **Version 0.3.1** (28 August 2012)
6
+ * Fixed naming conflict.
7
+ * **Version 0.3** (28 August 2012)
8
+ * Now includes a Post class that describes each post along with a bunch of new methods of accessing a post's replies and the post's original post.
9
+ * Users methods that return user details (follow, unfollow, following, followers, mute, unmute, mute_list) will now return either a User object back, or an array of User objects.
10
+ * Similarly, User methods that return post details (posts, mentions, stream) will return a Post object, or an array of Post objects.
11
+ * To accomplish all this, I've had to change the module structure which will break existing code if you've relied on the modules to access any data. Basically, all modules begin with ADN::API:: now (eg. ADN::API::Post, ADN::API::User).
12
+ * **Version 0.2** (27 August 2012)
13
+ * Changed all existing classes to modules and introduced the User class for easily accessing user details.
14
+ * **Version 0.1** (26 August 2012)
15
+ * Really basic functionality for all existing App.new API methods as per the spec.
data/Gemfile CHANGED
@@ -3,5 +3,5 @@ gemspec
3
3
 
4
4
  group :test do
5
5
  gem 'rake'
6
- gem 'minitest', '~> 3.3'
6
+ gem 'minitest', '~> 4.1'
7
7
  end
data/Gemfile.lock CHANGED
@@ -1,12 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- adn (0.3.4)
4
+ adn (0.3.5)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
- minitest (3.3.0)
9
+ minitest (4.1.0)
10
10
  rake (0.9.2.2)
11
11
 
12
12
  PLATFORMS
@@ -14,5 +14,5 @@ PLATFORMS
14
14
 
15
15
  DEPENDENCIES
16
16
  adn!
17
- minitest (~> 3.3)
17
+ minitest (~> 4.1)
18
18
  rake
data/README.md CHANGED
@@ -39,22 +39,6 @@ For API calls that accept parameters described in the App.net API Spec, simply p
39
39
 
40
40
  Complete documentation will be available soon, but in the meantime you can browse all API methods in `lib/adnruby.rb`.
41
41
 
42
- ### Changelog
43
-
44
- * **Version 0.3.1** (28 August 2012)
45
- * Fixed naming conflict
46
- * **Version 0.3** (28 August 2012)
47
- * Now includes a Post class that describes each post along with a bunch of new methods of accessing a post's replies and the post's original post.
48
- * Users methods that return user details (follow, unfollow, following, followers, mute, unmute, mute_list) will now return either a User object back, or an array of User objects.
49
- * Similarly, User methods that return post details (posts, mentions, stream) will return a Post object, or an array of Post objects.
50
- * To accomplish all this, I've had to change the module structure which will break existing code if you've relied on the modules to access any data. Basically, all modules begin with ADN::API:: now (eg. ADN::API::Post, ADN::API::User).
51
- * **Version 0.2** (27 August 2012)
52
- * Changed all existing classes to modules and introduced the User class for easily accessing user details
53
- * **Version 0.1** (26 August 2012)
54
- * Really basic functionality for all existing App.new API methods as per the spec.
55
-
56
- ---
57
-
58
42
  ### License
59
43
 
60
44
  **ADN** is licensed under the MIT License and is Copyright (c) 2012 Kishyr Ramdial.
data/lib/adn.rb CHANGED
@@ -30,14 +30,10 @@ require 'date'
30
30
  %w{constants api post user version}.each { |f| require_relative "adn/#{f}" }
31
31
 
32
32
  module ADN
33
- class Error < StandardError; end
33
+ Error = Class.new StandardError
34
34
 
35
- def self.token=(token)
36
- @token = token
37
- end
38
-
39
- def self.token
40
- @token
35
+ class << self
36
+ attr_accessor :token
41
37
  end
42
38
 
43
39
  def self.create_instance(data, type)
@@ -45,6 +41,6 @@ module ADN
45
41
  end
46
42
 
47
43
  def self.create_collection(data, type)
48
- data.collect { |t| type.new(t) }
44
+ data.map { |t| type.new(t) }
49
45
  end
50
46
  end
data/lib/adn/api.rb CHANGED
@@ -6,7 +6,7 @@ end
6
6
 
7
7
  module ADN
8
8
  module API
9
- class Error < StandardError; end
9
+ Error = Class.new StandardError
10
10
 
11
11
  class << self
12
12
  def perform(request)
@@ -18,26 +18,36 @@ module ADN
18
18
  }
19
19
  end
20
20
 
21
+ def construct_request(verb, url)
22
+ http_method = case verb
23
+ when :post then Net::HTTP::Post
24
+ when :put then Net::HTTP::Put
25
+ when :delete then Net::HTTP::Delete
26
+ else Net::HTTP::Get
27
+ end
28
+ http_method.new(url)
29
+ end
30
+
21
31
  def get(url, params = nil)
22
32
  url = params.nil? ? url : [url, URI.encode_www_form(params)].join("?")
23
- request = Net::HTTP::Get.new(url)
33
+ request = construct_request(:get, url)
24
34
  perform(request)
25
35
  end
26
36
 
27
37
  def post(url, params = nil)
28
- request = Net::HTTP::Post.new(url)
38
+ request = construct_request(:post, url)
29
39
  request.set_form_data(params) if params
30
40
  perform(request)
31
41
  end
32
42
 
33
43
  def put(url, params = nil)
34
- request = Net::HTTP::Put.new(url)
44
+ request = construct_request(:put, url)
35
45
  request.set_form_data(params) if params
36
46
  perform(request)
37
47
  end
38
48
 
39
49
  def delete(url)
40
- request = Net::HTTP::Delete.new(url)
50
+ request = construct_request(:delete, url)
41
51
  perform(request)
42
52
  end
43
53
  end
data/lib/adn/api/post.rb CHANGED
@@ -39,9 +39,13 @@ module ADN
39
39
  ADN::API.get("#{ADN::API_ENDPOINT_POSTS}/stream/global", params)
40
40
  end
41
41
 
42
+ def self.unified_stream(params = nil)
43
+ ADN::API.get("#{ADN::API_ENDPOINT_POSTS}/stream/unified", params)
44
+ end
45
+
42
46
  def self.by_hashtag(hashtag, params = nil)
43
47
  ADN::API.get("#{ADN::API_ENDPOINT_POSTS}/tag/#{hashtag}", params)
44
48
  end
45
49
  end
46
50
  end
47
- end
51
+ end
data/lib/adn/api/user.rb CHANGED
@@ -7,10 +7,6 @@ module ADN
7
7
  ADN::API.get("#{ADN::API_ENDPOINT_USERS}/#{user_id}")
8
8
  end
9
9
 
10
- def self.by_id(user_id)
11
- self.retrieve(user_id)
12
- end
13
-
14
10
  def self.following(user_id)
15
11
  ADN::API.get("#{ADN::API_ENDPOINT_USERS}/#{user_id}/following")
16
12
  end
data/lib/adn/post.rb CHANGED
@@ -2,9 +2,15 @@
2
2
 
3
3
  module ADN
4
4
  class Post
5
- attr_accessor :post_id, :created_at, :entities,
6
- :html, :id, :num_replies, :reply_to,
7
- :source, :text, :thread_id, :user
5
+ attr_accessor(
6
+ :id, :post_id, :text, :html, :source, :machine_only,
7
+ :reply_to, :thread_id, :canonical_url,
8
+ :num_replies, :num_reposts, :num_stars,
9
+ :annotations, :entities, :you_reposted,
10
+ :you_starred, :reposters, :starred_by
11
+ )
12
+
13
+ attr_writer :user, :created_at
8
14
 
9
15
  def self.send_post(params)
10
16
  result = ADN::API::Post.create(params)
@@ -26,7 +32,10 @@ module ADN
26
32
 
27
33
  def details
28
34
  if id
29
- Hash[self.instance_variables.map { |i| [i.to_s.slice(1..-1), self.instance_variable_get(i)]}]
35
+ value = self.instance_variables.map do |i|
36
+ [i.to_s.slice(1..-1), self.instance_variable_get(i)]
37
+ end
38
+ Hash[value]
30
39
  else
31
40
  ADN::API::Post.by_id(post_id)
32
41
  end
data/lib/adn/user.rb CHANGED
@@ -2,36 +2,25 @@
2
2
 
3
3
  module ADN
4
4
  class User
5
- attr_accessor :user_id
6
- attr_accessor :avatar_image, :counts, :cover_image,
7
- :created_at, :description, :follows_you,
8
- :id, :is_follower, :is_following, :is_muted,
9
- :locale, :name, :timezone, :type, :username,
10
- :you_follow, :you_muted
5
+ attr_accessor(
6
+ :id, :user_id, :username, :name, :description,
7
+ :timezone, :locale, :avatar_image,
8
+ :cover_image, :type, :counts, :app_data,
9
+ :follows_you, :you_follow, :you_muted
10
+ )
11
+
12
+ attr_writer :created_at
11
13
 
12
14
  def self.me
13
- new ADN::API::Token.current['user']
15
+ new ADN::API::Token.current["user"]
14
16
  end
15
17
 
16
- def initialize(user)
17
- if user.respond_to?(:each_pair)
18
- set_values(user)
19
- self.user_id = id.to_s
20
- else
21
- self.user_id = user.to_s
22
- user_details = details
23
- if details.has_key? "data"
24
- set_values(user_details["data"])
25
- end
26
- end
18
+ def self.find(user_id)
19
+ new ADN::API::User.retrieve(user_id)
27
20
  end
28
21
 
29
- def details
30
- if id
31
- Hash[self.instance_variables.map { |i| [i.to_s.slice(1..-1), self.instance_variable_get(i)]}]
32
- else
33
- ADN::API::User.retrieve(user_id)
34
- end
22
+ def initialize(user_data = {})
23
+ set_values(user_data)
35
24
  end
36
25
 
37
26
  def created_at
@@ -40,20 +29,16 @@ module ADN
40
29
 
41
30
  # Followers/Users
42
31
 
43
- def get_user(user)
44
- user.is_a?(ADN::User) ? user.id : user
45
- end
46
-
47
32
  def follow(user)
48
- user_id = get_user(user)
49
- result = ADN.post("/stream/0/users/#{user_id}/follow")
33
+ result = ADN.post("#{ADN::API_ENDPOINT_USERS}/#{user.user_id}/follow")
50
34
  ADN.create_instance(result["data"], User)
51
35
  end
52
36
 
53
37
  def unfollow(user)
54
- user_id = get_user(user)
55
- result = ADN.delete("/stream/0/users/#{user_id}/follow")
56
- ADN.create_instance(result["data"], User)
38
+ if user.valid_user?
39
+ result = ADN.delete("#{ADN::API_ENDPOINT_USERS}/#{user.user_id}/follow")
40
+ ADN.create_instance(result["data"], User)
41
+ end
57
42
  end
58
43
 
59
44
  def followers
@@ -69,15 +54,17 @@ module ADN
69
54
  # Mute
70
55
 
71
56
  def mute(user)
72
- user_id = get_user(user)
73
- result = ADN.post("#{ADN::API_ENDPOINT_USERS}/#{user_id}/mute")
74
- ADN.create_instance(result["data"], User)
57
+ if user.valid_user?
58
+ result = ADN.post("#{ADN::API_ENDPOINT_USERS}/#{user.user_id}/mute")
59
+ ADN.create_instance(result["data"], User)
60
+ end
75
61
  end
76
62
 
77
63
  def unmute(user)
78
- user_id = get_user(user)
79
- result = ADN.delete("#{ADN::API_ENDPOINT_USERS}/#{user_id}/mute")
80
- ADN.create_instance(result["data"], User)
64
+ if user.valid_user?
65
+ result = ADN.delete("#{ADN::API_ENDPOINT_USERS}/#{user.user_id}/mute")
66
+ ADN.create_instance(result["data"], User)
67
+ end
81
68
  end
82
69
 
83
70
  def mute_list
@@ -102,12 +89,15 @@ module ADN
102
89
  ADN.create_collection(result["data"], Post)
103
90
  end
104
91
 
105
- def set_values(values)
106
- values.each_pair { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
92
+ def valid_user?
93
+ !!user_id.match(/^\d+$/)
107
94
  end
108
95
 
109
- def has_error?
110
- self.id.nil?
96
+ def set_values(values)
97
+ if values.respond_to? :each_pair
98
+ values.each_pair { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
99
+ self.user_id = id.to_s
100
+ end
111
101
  end
112
102
  end
113
103
  end
data/lib/adn/version.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module ADN
4
4
  MAJOR = 0
5
5
  MINOR = 3
6
- TINY = 4
6
+ TINY = 5
7
7
  VERSION = [MAJOR, MINOR, TINY].join('.')
8
8
  end
9
9
 
@@ -90,6 +90,17 @@ describe ADN::API::Post do
90
90
  end
91
91
  end
92
92
 
93
+ describe "unified_stream" do
94
+ it "retrieves the unified stream" do
95
+ args(:get) {
96
+ path, params = subject.unified_stream('baz')
97
+
98
+ path.must_equal base_path + "/stream/unified"
99
+ params.must_equal 'baz'
100
+ }
101
+ end
102
+ end
103
+
93
104
  describe "by_hashtag" do
94
105
  it "retrieves posts by hashtag" do
95
106
  args(:get) {
@@ -7,13 +7,15 @@ describe ADN::API::Token do
7
7
 
8
8
  describe "current" do
9
9
  it "does not return the current token if it has an error" do
10
- ADN::API.stub(:get, ADN::API::Response.new({ "error" => "error message" })) do
10
+ ADN::API.stub(
11
+ :get, ADN::API::Response.new("error" => "error message")) do
11
12
  subject.current.must_equal nil
12
13
  end
13
14
  end
14
15
 
15
16
  it "retrieves the current token" do
16
- ADN::API.stub(:get, ADN::API::Response.new({ "data" => "example_token" })) do
17
+ ADN::API.stub(
18
+ :get, ADN::API::Response.new("data" => "example_token")) do
17
19
  subject.current.must_equal "example_token"
18
20
  end
19
21
  end
@@ -13,14 +13,6 @@ describe ADN::API::User do
13
13
  end
14
14
  end
15
15
 
16
- describe "by_id" do
17
- it "is just an alias for retrieve" do
18
- subject.stub(:retrieve, 'foo') do
19
- subject.by_id(123).must_equal 'foo'
20
- end
21
- end
22
- end
23
-
24
16
  describe "following" do
25
17
  it "retrieves the following list" do
26
18
  arg(:get) { subject.following(44).must_equal base_path + "44/following" }
@@ -95,7 +95,8 @@ describe ADN::Post do
95
95
 
96
96
  describe "replies" do
97
97
  it "returns a list of posts" do
98
- response = ADN::API::Response.new("data" => [{ text: "foo"}, {text: "bar"}])
98
+ response = ADN::API::Response.new(
99
+ "data" => [{ text: "foo"}, {text: "bar"}])
99
100
 
100
101
  ADN::API::Post.stub(:replies, ->(*a){ response }) do
101
102
  r = post.replies
@@ -5,9 +5,9 @@ require_relative '../spec_helper'
5
5
  describe ADN::User do
6
6
  subject { ADN::User }
7
7
 
8
- let(:empty_user) { subject.new({}) }
9
- let(:user) { subject.new(user_data) }
10
- let(:user_data) { fixture('user.json') }
8
+ let(:empty_user) { subject.new({}) }
9
+ let(:user) { subject.new(user_data) }
10
+ let(:user_data) { fixture('user.json') }
11
11
 
12
12
  describe "me" do
13
13
  it "retrieves the user based on the current token" do
@@ -19,24 +19,21 @@ describe ADN::User do
19
19
  end
20
20
  end
21
21
 
22
- describe "initialize" do
23
- it "populates the accessors based on the raw user data passed in" do
24
- u = subject.new(user_data)
25
- u.user_id.must_equal "4821"
26
- end
27
-
28
- # TODO: Remove this behavior, wrong level of abstraction
29
- it "populates the accessors based on the user id passed in" do
30
- ADN::API::User.stub(:retrieve, { "data" => user_data }) do
31
- u = subject.new(4821)
22
+ describe "find" do
23
+ it "retrieves the user data from the API and returns a User object" do
24
+ ADN::API::User.stub(:retrieve, user_data) do
25
+ u = subject.find("4821")
32
26
  u.name.must_equal "Peter Hellberg"
33
27
  u.user_id.must_equal "4821"
34
28
  end
35
29
  end
36
30
  end
37
31
 
38
- describe "details" do
39
- it "spec_name" do
32
+ describe "initialize" do
33
+ it "populates the accessors based on the raw user data passed in" do
34
+ u = subject.new(user_data)
35
+ u.name.must_equal "Peter Hellberg"
36
+ u.user_id.must_equal "4821"
40
37
  end
41
38
  end
42
39
 
@@ -46,16 +43,6 @@ describe ADN::User do
46
43
  end
47
44
  end
48
45
 
49
- # TODO: Change the name to describe what
50
- # it actually returns (user_id) or
51
- # remove it completely
52
- describe "get_user" do
53
- it "returns a user id for some reason" do
54
- empty_user.get_user(user).must_equal "4821"
55
- empty_user.get_user(123).must_equal 123
56
- end
57
- end
58
-
59
46
  describe "follow" do
60
47
  it "" do
61
48
  end
@@ -115,13 +102,13 @@ describe ADN::User do
115
102
  end
116
103
  end
117
104
 
118
- describe "has_error?" do
119
- it "returns true if no id" do
120
- empty_user.has_error?.must_equal true
105
+ describe "valid_user?" do
106
+ it "returns false if no id" do
107
+ empty_user.valid_user?.must_equal false
121
108
  end
122
109
 
123
- it "returns false if the user has an id" do
124
- user.has_error?.must_equal false
110
+ it "returns true if the user has an id" do
111
+ user.valid_user?.must_equal true
125
112
  end
126
113
  end
127
114
  end
data/spec/adn_spec.rb CHANGED
@@ -23,7 +23,7 @@ describe ADN do
23
23
  ADN::API_ENDPOINT_POSTS.must_equal '/stream/0/posts'
24
24
  ADN::API_ENDPOINT_USERS.must_equal '/stream/0/users'
25
25
  end
26
-
26
+
27
27
  it "has constants containing the API endpoints for tokens" do
28
28
  ADN::API_ENDPOINT_TOKEN.must_equal '/stream/0/token'
29
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-09-10 00:00:00.000000000 Z
14
+ date: 2012-10-23 00:00:00.000000000 Z
15
15
  dependencies: []
16
16
  description: A simple and easy to use library to interact with App.net's API
17
17
  email:
@@ -24,6 +24,7 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - .gitignore
26
26
  - .travis.yml
27
+ - CHANGELOG.md
27
28
  - Gemfile
28
29
  - Gemfile.lock
29
30
  - README.md
@@ -75,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
76
  version: '0'
76
77
  segments:
77
78
  - 0
78
- hash: -2288184448617261034
79
+ hash: 2181708405046728371
79
80
  requirements: []
80
81
  rubyforge_project:
81
82
  rubygems_version: 1.8.24