totter 0.2.14 → 0.3.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.
@@ -10,7 +10,7 @@ module Totter
10
10
  # @example
11
11
  # Totter.avatar(1, 1)
12
12
  def avatar(user_id, avatar_id)
13
- get "users/#{user_id}/avatars/#{avatar_id}"
13
+ get("users/#{user_id}/avatars/#{avatar_id}").body
14
14
  end
15
15
 
16
16
  # Get all known avatars for a given user
@@ -20,7 +20,7 @@ module Totter
20
20
  # @example
21
21
  # Totter.avatar(1)
22
22
  def avatars(user_id)
23
- get "users/#{user_id}/avatars"
23
+ get("users/#{user_id}/avatars").body
24
24
  end
25
25
 
26
26
  # Creates a new avatar on the server, allowing for a signed S3 upload
@@ -33,7 +33,7 @@ module Totter
33
33
  def create_avatar(user_id, redirect_url = nil)
34
34
  data = {}
35
35
  data[:redirect_url] = redirect_url if redirect_url
36
- post "users/#{user_id}/avatars", data
36
+ post("users/#{user_id}/avatars", data).body
37
37
  end
38
38
 
39
39
  # Destroy an avatar
@@ -44,7 +44,7 @@ module Totter
44
44
  # @example
45
45
  # Totter.destroy_avatar(1, 1)
46
46
  def destroy_avatar(user_id, avatar_id)
47
- boolean_from_response :delete, "users/#{user_id}/avatars/#{avatar_id}"
47
+ boolean_from_response(:delete, "users/#{user_id}/avatars/#{avatar_id}")
48
48
  end
49
49
  end
50
50
  end
@@ -11,7 +11,7 @@ module Totter
11
11
  # @example
12
12
  # Totter.choice(1, 1, 1)
13
13
  def choice(user_id, decision_id, choice_id)
14
- get "users/#{user_id}/decisions/#{decision_id}/choices/#{choice_id}"
14
+ get("users/#{user_id}/decisions/#{decision_id}/choices/#{choice_id}").body
15
15
  end
16
16
 
17
17
 
@@ -43,7 +43,7 @@ module Totter
43
43
  }
44
44
  }
45
45
 
46
- post "users/#{user_id}/decisions/#{decision_id}/choices", data
46
+ post("users/#{user_id}/decisions/#{decision_id}/choices", data).body
47
47
  end
48
48
 
49
49
  # Create a new choice upload. Resulting hash contains an #upload parameter
@@ -55,7 +55,7 @@ module Totter
55
55
  # @example
56
56
  # Totter.create_choice_upload(1, 1)
57
57
  def create_choice_upload(user_id, decision_id)
58
- post "users/#{user_id}/decisions/#{decision_id}/choices"
58
+ post("users/#{user_id}/decisions/#{decision_id}/choices").body
59
59
  end
60
60
 
61
61
  # Destroy a choice
@@ -67,7 +67,7 @@ module Totter
67
67
  # @example
68
68
  # Totter.destroy_choice(1, 1)
69
69
  def destroy_choice(user_id, decision_id, choice_id)
70
- boolean_from_response :delete, "users/#{user_id}/decisions/#{decision_id}/choices/#{choice_id}"
70
+ boolean_from_response(:delete, "users/#{user_id}/decisions/#{decision_id}/choices/#{choice_id}")
71
71
  end
72
72
  end
73
73
  end
@@ -10,7 +10,7 @@ module Totter
10
10
  # @example
11
11
  # Totter.comments(1, 1)
12
12
  def comments(user_id, decision_id)
13
- get "users/#{user_id}/decisions/#{decision_id}/comments"
13
+ get("users/#{user_id}/decisions/#{decision_id}/comments").body
14
14
  end
15
15
 
16
16
  # Creates a new comment
@@ -27,7 +27,7 @@ module Totter
27
27
  message: message
28
28
  }
29
29
  }
30
- post "users/#{user_id}/decisions/#{decision_id}/comments", options
30
+ post("users/#{user_id}/decisions/#{decision_id}/comments", options).body
31
31
  end
32
32
 
33
33
  # Destroys a comment
@@ -39,7 +39,7 @@ module Totter
39
39
  # @example
40
40
  # Totter.destroy_comment(1, 1, 15)
41
41
  def destroy_comment(user_id, decision_id, comment_id)
42
- boolean_from_response :delete, "users/#{user_id}/decisions/#{decision_id}/comments/#{comment_id}"
42
+ boolean_from_response(:delete, "users/#{user_id}/decisions/#{decision_id}/comments/#{comment_id}")
43
43
  end
44
44
 
45
45
  end
@@ -8,7 +8,7 @@ module Totter
8
8
  # @example
9
9
  # Totter.configuration
10
10
  def configuration
11
- get 'configuration'
11
+ get('configuration').body
12
12
  end
13
13
 
14
14
  # Get the featured timelines and users for Explore.
@@ -17,7 +17,7 @@ module Totter
17
17
  # @example
18
18
  # Totter.explore
19
19
  def explore
20
- get 'explore'
20
+ get('explore').body
21
21
  end
22
22
  end
23
23
  end
@@ -10,7 +10,7 @@ module Totter
10
10
  # @example
11
11
  # Totter.decision(1, 1)
12
12
  def decision(user_id, decision_id)
13
- get "users/#{user_id}/decisions/#{decision_id}"
13
+ get("users/#{user_id}/decisions/#{decision_id}").body
14
14
  end
15
15
 
16
16
  # Create a decision
@@ -20,7 +20,7 @@ module Totter
20
20
  # @example
21
21
  # Totter.create_decision(1)
22
22
  def create_decision(user_id)
23
- post "users/#{user_id}/decisions"
23
+ post("users/#{user_id}/decisions").body
24
24
  end
25
25
 
26
26
  # Publish a decision
@@ -40,7 +40,7 @@ module Totter
40
40
  decision: options
41
41
  }
42
42
 
43
- post "users/#{user_id}/decisions/#{decision_id}/publish", decision_options
43
+ post("users/#{user_id}/decisions/#{decision_id}/publish", decision_options).body
44
44
  end
45
45
 
46
46
  # Destroy a decision
@@ -51,7 +51,7 @@ module Totter
51
51
  # @example
52
52
  # Totter.destroy_decision(1, 1)
53
53
  def destroy_decision(user_id, decision_id)
54
- boolean_from_response :delete, "users/#{user_id}/decisions/#{decision_id}"
54
+ boolean_from_response(:delete, "users/#{user_id}/decisions/#{decision_id}")
55
55
  end
56
56
 
57
57
  # Get decision analytics
@@ -62,7 +62,7 @@ module Totter
62
62
  # @example
63
63
  # Totter.decision_analytics(1, 1)
64
64
  def decision_analytics(user_id, decision_id)
65
- get "users/#{user_id}/decisions/#{decision_id}/analytics"
65
+ get("users/#{user_id}/decisions/#{decision_id}/analytics").body
66
66
  end
67
67
 
68
68
  # Flag a decision for content review
@@ -73,7 +73,7 @@ module Totter
73
73
  # @example
74
74
  # Totter.flag_decision(1, 1)
75
75
  def flag_decision(user_id, decision_id)
76
- boolean_from_response :post, "users/#{user_id}/decisions/#{decision_id}/flag"
76
+ boolean_from_response(:post, "users/#{user_id}/decisions/#{decision_id}/flag")
77
77
  end
78
78
 
79
79
  # Unflag a decision for content review
@@ -84,7 +84,7 @@ module Totter
84
84
  # @example
85
85
  # Totter.unflag_decision(1, 1)
86
86
  def unflag_decision(user_id, decision_id)
87
- boolean_from_response :post, "users/#{user_id}/decisions/#{decision_id}/unflag"
87
+ boolean_from_response(:post, "users/#{user_id}/decisions/#{decision_id}/unflag")
88
88
  end
89
89
  end
90
90
  end
@@ -12,7 +12,7 @@ module Totter
12
12
  def slug(id, unique_id = nil)
13
13
  path = "slugs/#{id}"
14
14
  path += "?unique_id=#{unique_id}" if unique_id
15
- get path
15
+ get(path).body
16
16
  end
17
17
  end
18
18
  end
@@ -9,7 +9,7 @@ module Totter
9
9
  # @example
10
10
  # Totter.sticker('staff')
11
11
  def sticker(sticker_name)
12
- get "stickers/#{sticker_name}"
12
+ get("stickers/#{sticker_name}").body
13
13
  end
14
14
  end
15
15
  end
@@ -17,7 +17,7 @@ module Totter
17
17
  # @example
18
18
  # Totter.global_timeline(limit: 20, since: '1,15')
19
19
  def global_timeline(options = DEFAULT_TIMELINE_OPTIONS)
20
- get 'timelines/global', options
20
+ get('timelines/global', options)
21
21
  end
22
22
 
23
23
  # Get recent decisions from a hashtag timeline
@@ -31,7 +31,7 @@ module Totter
31
31
  # @example
32
32
  # Totter.global_timeline(limit: 20, since: '1,15')
33
33
  def hashtag_timeline(hashtag, options = DEFAULT_TIMELINE_OPTIONS)
34
- get 'timelines/global', options.merge({hashtag: hashtag})
34
+ get('timelines/global', options.merge({hashtag: hashtag}))
35
35
  end
36
36
 
37
37
  # Get recent decisions from a sticker timeline
@@ -45,7 +45,7 @@ module Totter
45
45
  # @example
46
46
  # Totter.global_timeline(limit: 20, since: '1,15')
47
47
  def sticker_timeline(sticker, options = DEFAULT_TIMELINE_OPTIONS)
48
- get 'timelines/global', options.merge({sticker: sticker})
48
+ get('timelines/global', options.merge({sticker: sticker}))
49
49
  end
50
50
 
51
51
  # Search for published items
@@ -59,7 +59,7 @@ module Totter
59
59
  # @example
60
60
  # Totter.global_timeline(limit: 20, since: '1,15')
61
61
  def search_timeline(query, options = DEFAULT_TIMELINE_OPTIONS)
62
- get 'timelines/search', options.merge({query: query})
62
+ get('timelines/search', options.merge({query: query}))
63
63
  end
64
64
 
65
65
  # Get recent decisions from the flagged-for-review timeline
@@ -72,7 +72,7 @@ module Totter
72
72
  # @example
73
73
  # Totter.global_timeline(limit: 20, since: '1,15')
74
74
  def flagged_timeline(options = DEFAULT_TIMELINE_OPTIONS)
75
- get 'timelines/flagged', options
75
+ get('timelines/flagged', options)
76
76
  end
77
77
 
78
78
  # Get recent decisions from a given user.
@@ -86,7 +86,7 @@ module Totter
86
86
  # @example
87
87
  # Totter.user_timeline(4, limit: 20, since: '1,15')
88
88
  def user_timeline(user_id, options = DEFAULT_TIMELINE_OPTIONS)
89
- get "users/#{user_id}/timelines/user", options
89
+ get("users/#{user_id}/timelines/user", options)
90
90
  end
91
91
 
92
92
  # Get the friends timeline (also known as "feed" or "home") for a given user.
@@ -103,7 +103,7 @@ module Totter
103
103
  # @example
104
104
  # Totter.friends_timeline(4, limit: 20, since: '1,15')
105
105
  def friends_timeline(user_id, options = DEFAULT_TIMELINE_OPTIONS)
106
- get "users/#{user_id}/timelines/friends", options
106
+ get("users/#{user_id}/timelines/friends", options)
107
107
  end
108
108
  end
109
109
  end
@@ -11,7 +11,7 @@ module Totter
11
11
  # @example
12
12
  # client.me
13
13
  def me
14
- get 'me'
14
+ get('me').body
15
15
  end
16
16
 
17
17
  # Get a single user
@@ -21,7 +21,7 @@ module Totter
21
21
  # @example
22
22
  # Totter.user('soffes')
23
23
  def user(user)
24
- get "users/#{user}"
24
+ get("users/#{user}").body
25
25
  end
26
26
 
27
27
  # Updates the authenticating user
@@ -43,7 +43,7 @@ module Totter
43
43
  data = {
44
44
  user: options.merge({preferences: preferences})
45
45
  }
46
- put "me", data
46
+ put("me", data).body
47
47
  end
48
48
 
49
49
  # Follow a user.
@@ -56,7 +56,7 @@ module Totter
56
56
  # @example
57
57
  # client.follow('gotwalt')
58
58
  def follow(user)
59
- boolean_from_response :post, "users/#{user}/follow"
59
+ boolean_from_response(:post, "users/#{user}/follow")
60
60
  end
61
61
 
62
62
  # Unfollow a user.
@@ -69,7 +69,7 @@ module Totter
69
69
  # @example
70
70
  # client.unfollow('kyle')
71
71
  def unfollow(user)
72
- boolean_from_response :post, "users/#{user}/unfollow"
72
+ boolean_from_response(:post, "users/#{user}/unfollow")
73
73
  end
74
74
 
75
75
  # @return Array of [Hashie::Mash]
@@ -77,7 +77,7 @@ module Totter
77
77
  # @example
78
78
  # client.followers('5')
79
79
  def followers(user_id)
80
- get "users/#{user_id}/followers"
80
+ get("users/#{user_id}/followers").body
81
81
  end
82
82
 
83
83
  # @return Array of [Hashie::Mash]
@@ -85,7 +85,7 @@ module Totter
85
85
  # @example
86
86
  # client.following('5')
87
87
  def following(user_id)
88
- get "users/#{user_id}/following"
88
+ get("users/#{user_id}/following").body
89
89
  end
90
90
 
91
91
  # @return Array of [Hashie::Mash]
@@ -93,7 +93,7 @@ module Totter
93
93
  # @example
94
94
  # client.user_votes('5')
95
95
  def votes(user_id)
96
- get "users/#{user_id}/votes"
96
+ get("users/#{user_id}/votes").body
97
97
  end
98
98
 
99
99
  end
@@ -18,7 +18,7 @@ module Totter
18
18
  slug = options[:invitation_slug]
19
19
  params[:invitation] = { slug: slug } if slug
20
20
 
21
- post path, params
21
+ post(path, params).body
22
22
  end
23
23
  end
24
24
  end
@@ -0,0 +1,3 @@
1
+ module Totter
2
+
3
+ end
@@ -78,25 +78,10 @@ module Totter
78
78
  end
79
79
 
80
80
  def json_request(*args)
81
- return Hashie::Mash.new({}) if Totter::Client.stubbed?
81
+ return Response.new(Hashie::Mash.new(body: {}.to_json, headers: [])) if Totter::Client.stubbed?
82
82
 
83
83
  # Perform request
84
- response = request(*args)
85
-
86
- # Parse JSON
87
- object = MultiJson.load(response.body)
88
-
89
- # Hash
90
- return Hashie::Mash.new(object) if object.is_a? Hash
91
-
92
- # Array
93
- begin
94
- return object.map { |h| Hashie::Mash.new(h) } if object.is_a? Array
95
- rescue
96
- # sometimes, for things like string arrays, we'll end up with an error
97
- end
98
- # Fallback incase it's not a hash or array
99
- object
84
+ Response.new(request(*args))
100
85
  end
101
86
 
102
87
  def boolean_from_response(*args)
@@ -113,6 +98,42 @@ module Totter
113
98
  json_request(method, *args)
114
99
  end
115
100
  end
101
+
102
+ class Response
103
+ attr_accessor :body, :headers
104
+
105
+ def initialize(http_response)
106
+ @headers = parse_headers(http_response.to_hash)
107
+ @body = parse_body(http_response.body)
108
+ end
109
+
110
+ private
111
+
112
+ def parse_headers(raw_headers)
113
+ raw_headers.inject({}) do |remainder, (k, v)|
114
+ remainder[k] = [v].flatten.first
115
+ remainder
116
+ end
117
+ end
118
+
119
+ def parse_body(body)
120
+ # Parse JSON
121
+ object = MultiJson.load(body)
122
+
123
+ # Hash
124
+ return Hashie::Mash.new(object) if object.is_a? Hash
125
+
126
+ # Array
127
+ begin
128
+ return object.map { |h| Hashie::Mash.new(h) } if object.is_a? Array
129
+ rescue
130
+ # sometimes, for things like string arrays, we'll end up with an error
131
+ end
132
+ # Fallback incase it's not a hash or array
133
+ object
134
+ end
135
+ end
136
+
116
137
  end
117
138
  end
118
139
  end
@@ -1,4 +1,4 @@
1
1
  module Totter
2
2
  # Verion of the Totter gem
3
- VERSION = '0.2.14'
3
+ VERSION = '0.3.0'
4
4
  end
@@ -21,7 +21,7 @@ http_interactions:
21
21
  message: OK
22
22
  headers:
23
23
  X-Pusher-Channel:
24
- - timeline-hashtag-test
24
+ - timeline-sticker-test
25
25
  Content-Type:
26
26
  - application/json; charset=utf-8
27
27
  X-Ua-Compatible:
@@ -3,42 +3,53 @@ require 'test_helper'
3
3
  class TimelinesTest < Totter::TestCase
4
4
  def test_global
5
5
  VCR.use_cassette 'timelines/global' do
6
- assert_equal 15, Totter.global_timeline(limit: 15).length
6
+ response = Totter.global_timeline(limit: 15)
7
+ assert_equal 15, response.body.length
8
+ assert_equal 'timeline-global', response.headers['x-pusher-channel']
7
9
  end
8
10
  end
9
11
 
10
12
  def test_hashtags
11
13
  VCR.use_cassette 'timelines/hashtags' do
12
14
  client = local_client
13
- assert_equal 1, local_client.hashtag_timeline('test').length
15
+ response = local_client.hashtag_timeline('test')
16
+ assert_equal 1, response.body.length
17
+ assert_equal 'timeline-hashtag-test', response.headers['x-pusher-channel']
14
18
  end
15
19
  end
16
20
 
17
21
  def test_stickers
18
22
  VCR.use_cassette 'timelines/stickers' do
19
23
  client = local_client
20
- assert_equal 1, local_client.sticker_timeline('test').length
24
+ response = local_client.sticker_timeline('test')
25
+ assert_equal 1, response.body.length
26
+ assert_equal 'timeline-sticker-test', response.headers['x-pusher-channel']
21
27
  end
22
28
  end
23
29
 
24
30
  def test_search
25
31
  VCR.use_cassette 'timelines/search' do
26
32
  client = local_client
27
- assert_equal 2, local_client.search_timeline('bacon').length
33
+ response = local_client.search_timeline('bacon')
34
+ assert_equal 2, response.body.length
28
35
  end
29
36
  end
30
37
 
31
38
  def test_flagged
32
39
  VCR.use_cassette 'timelines/flagged' do
33
40
  client = local_client
34
- assert_equal 1, local_client.flagged_timeline.length
41
+ response = local_client.flagged_timeline
42
+ assert_equal 1, response.body.length
43
+ assert_equal 'timeline-flagged', response.headers['x-pusher-channel']
35
44
  end
36
45
  end
37
46
 
38
47
  def test_user
39
48
  VCR.use_cassette 'timelines/user' do
40
49
  client = local_client
41
- assert_equal 0, local_client.user_timeline(4).length
50
+ response = local_client.user_timeline(4)
51
+ assert_equal 0, response.body.length
52
+ assert_equal 'timeline-user-4', response.headers['x-pusher-channel']
42
53
  end
43
54
  end
44
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: totter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.14
4
+ version: 0.3.0
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: 2013-02-19 00:00:00.000000000 Z
14
+ date: 2013-02-20 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: multi_json
@@ -74,6 +74,7 @@ files:
74
74
  - lib/totter/client/users.rb
75
75
  - lib/totter/client/votes.rb
76
76
  - lib/totter/error.rb
77
+ - lib/totter/response.rb
77
78
  - lib/totter/transport.rb
78
79
  - lib/totter/transport/http.rb
79
80
  - lib/totter/version.rb