the86-client 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -71,6 +71,26 @@ conversation.posts.create(
71
71
  content: "What are you guys talking about?",
72
72
  oauth_token: user.access_tokens.first.token
73
73
  )
74
+
75
+ # List conversations:
76
+ conversations = The86::Client.site("example").conversations
77
+ second_page = conversations.more
78
+ third_page = second_page.more
79
+ third_page.each { |conversation| p conversation }
80
+
81
+ # Check for updates:
82
+ site = The86::Client.site("example")
83
+ conversations = site.conversations.with_parameters(
84
+ posts_since: time.iso8601,
85
+ without_user: 64,
86
+ )
87
+
88
+ # Like!
89
+ post = site.conversations.first.posts.first
90
+ post.likes.create(oauth_token: oauth_token)
91
+ likes = post.load.likes
92
+ puts "Liked by #{likes.count} people"
93
+ likes.each { |like| puts "* #{like.user.name}" }
74
94
  ```
75
95
 
76
96
 
data/lib/the86-client.rb CHANGED
@@ -13,6 +13,7 @@
13
13
  site
14
14
  post
15
15
  conversation
16
+ like
16
17
  }.each { |r| require "the86-client/#{r}" }
17
18
 
18
19
  module The86
@@ -0,0 +1,11 @@
1
+ module The86::Client
2
+ class Like < Resource
3
+
4
+ attribute :id, Integer
5
+
6
+ path "likes"
7
+ belongs_to :post
8
+ has_one :user, ->{ User }
9
+
10
+ end
11
+ end
@@ -12,6 +12,7 @@ module The86::Client
12
12
  belongs_to :conversation
13
13
  has_one :user, ->{ User }
14
14
  has_one :in_reply_to, ->{ Post }
15
+ has_many :likes, ->{ Like }
15
16
 
16
17
  include CanBeHidden
17
18
 
@@ -27,7 +27,7 @@ module The86::Client
27
27
  build(attributes).tap(&:save)
28
28
  end
29
29
 
30
- attr_writer :parameters
30
+ attr_accessor :parameters
31
31
 
32
32
  def with_parameters(parameters)
33
33
  self.class.new(
@@ -63,12 +63,15 @@ module The86::Client
63
63
  # Link: <http://example.org/api/v1/sites/a/conversations?bumped_before=time>; rel="next"
64
64
  def more
65
65
  if more?
66
+ url = Addressable::URI.parse(http_response.links[:next])
66
67
  self.class.new(
67
68
  @connection,
68
- Addressable::URI.parse(http_response.links[:next]).request_uri,
69
+ url.path,
69
70
  @klass,
70
71
  @parent
71
- )
72
+ ).tap do |collection|
73
+ collection.parameters = url.query_values
74
+ end
72
75
  else
73
76
  raise PaginationError, %{Collection has no 'Link: <url>; rel="next"' header}
74
77
  end
@@ -1,5 +1,5 @@
1
1
  module The86
2
2
  module Client
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -58,6 +58,34 @@ module The86::Client
58
58
  page1.map(&:id).must_equal([1,2])
59
59
  page2.map(&:id).must_equal([3,4])
60
60
  end
61
+
62
+ it "allows pagination parameters to be fetched and set" do
63
+ url = "#{conversations_url}?limit=2"
64
+ next_url = "#{url}&bumped_before=timestamp"
65
+ expect_get_conversations(
66
+ url: basic_auth_url(url),
67
+ response_body: [{id: 1}, {id: 2}],
68
+ response_headers: {"Link" => %{<#{next_url}>; rel="next"}}
69
+ )
70
+
71
+ expect_get_conversations(
72
+ url: basic_auth_url(next_url),
73
+ response_body: [{id: 3}, {id: 4}],
74
+ )
75
+
76
+ # Emulate parameters being serialized & stored for a later request.
77
+ page1 = site.conversations.with_parameters(limit: 2)
78
+ parameters = JSON.parse(JSON.generate(page1.more.parameters))
79
+ page2 = site.conversations.with_parameters(parameters)
80
+
81
+ parameters.must_equal("limit" => "2", "bumped_before" => "timestamp")
82
+
83
+ page1.more?.must_equal true
84
+ page2.more?.must_equal false
85
+
86
+ page1.map(&:id).must_equal([1,2])
87
+ page2.map(&:id).must_equal([3,4])
88
+ end
61
89
  end
62
90
 
63
91
  describe "creating conversations" do
@@ -0,0 +1,42 @@
1
+ require_relative "spec_helper"
2
+
3
+ module The86::Client
4
+
5
+ describe Like do
6
+
7
+ let(:site) { The86::Client.site("test") }
8
+ let(:site_url) { "https://example.org/api/v1/sites/test" }
9
+ let(:post) { site.conversations.build(id: 1).posts.build(id: 2) }
10
+ let(:likes_url) { "#{site_url}/conversations/1/posts/2/likes" }
11
+
12
+ it "POSTs a new Like" do
13
+ expect_request(
14
+ url: likes_url,
15
+ method: :post,
16
+ status: 201,
17
+ request_body: {},
18
+ response_body: {user: {id: 1, name: "John Citizen"}},
19
+ request_headers: {"Authorization" => "Bearer secret"},
20
+ )
21
+
22
+ post.likes.create(oauth_token: "secret")
23
+ end
24
+
25
+ it "GETs Likes" do
26
+ expect_request(
27
+ url: likes_url.sub("//", "//user:pass@"),
28
+ method: :get,
29
+ status: 200,
30
+ response_body: [
31
+ {user: {name: "John"}},
32
+ {user: {name: "Alice"}},
33
+ ],
34
+ )
35
+
36
+ post.likes.map { |l| l.user.name }.must_equal %w{John Alice}
37
+ end
38
+
39
+ end
40
+
41
+
42
+ end
data/spec/post_spec.rb CHANGED
@@ -21,5 +21,19 @@ module The86::Client
21
21
  post.user.name.must_equal "John Citizen"
22
22
  end
23
23
  end
24
+
25
+ describe "#links" do
26
+ let(:post) do
27
+ Post.new(likes: [
28
+ {user: {id: 8, name: "John Citizen"}},
29
+ {user: {id: 2, name: "Sandman Slim"}}
30
+ ])
31
+ end
32
+ it "returns two The86::Client::Like instances" do
33
+ likes = post.likes
34
+ likes.count.must_equal 2
35
+ likes.first.user.name.must_equal "John Citizen"
36
+ end
37
+ end
24
38
  end
25
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: the86-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-28 00:00:00.000000000 Z
12
+ date: 2012-09-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -223,6 +223,7 @@ files:
223
223
  - lib/the86-client/connection.rb
224
224
  - lib/the86-client/conversation.rb
225
225
  - lib/the86-client/errors.rb
226
+ - lib/the86-client/like.rb
226
227
  - lib/the86-client/oauth_bearer_authorization.rb
227
228
  - lib/the86-client/post.rb
228
229
  - lib/the86-client/resource.rb
@@ -232,6 +233,7 @@ files:
232
233
  - lib/the86-client/user.rb
233
234
  - lib/the86-client/version.rb
234
235
  - spec/conversations_spec.rb
236
+ - spec/likes_spec.rb
235
237
  - spec/oauth_bearer_authorization_spec.rb
236
238
  - spec/post_spec.rb
237
239
  - spec/posts_spec.rb
@@ -268,6 +270,7 @@ specification_version: 3
268
270
  summary: Exposes The 86 conversation API server as an object model.
269
271
  test_files:
270
272
  - spec/conversations_spec.rb
273
+ - spec/likes_spec.rb
271
274
  - spec/oauth_bearer_authorization_spec.rb
272
275
  - spec/post_spec.rb
273
276
  - spec/posts_spec.rb