hey 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmY2YmFlMzU2NzVlZjJmNTA0ODZmODFhMzg4YmE5MjNlMGJhZmZiYg==
4
+ NGVjMjMzNjEwMzU0Y2U1MTkyMzU0Nzk0MDQzYzQ2Y2E5ZDcwNmFmZA==
5
5
  data.tar.gz: !binary |-
6
- Yjk4MWVjYzJjZmUyZDIyOTMyNDhkMmIwZTNiNWFlNDQxNmQ1OWU2OQ==
6
+ YjQ0YmU3YzY0M2JiNjM2MWI1Zjg5NTNjNDAwNWYzNjU4MGQxY2FlZg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzA2NzQwNTRkZWY0M2FhNGJiODk1MjQzYTFjZWNmNmE4ZTE2ZWFiZjIxNzhk
10
- OGMzM2VkZmYzMmRhYWFlZDFiYjYzN2IxYTkyNmM0MjQ3MDIwMGI5MDE3YmVm
11
- OWRjMWI0NzBkNzZkNGE2YzdjOWI4YWY3YmQxNDkzMDQyZDhkY2Q=
9
+ OTY2NTJiMzYwOTM4ZWMwNDA0NjcwODdhNjA0Y2RhNzE3Y2YxNTM4NGYwMjdk
10
+ OWU1YWVjYWY5NzEzMzZjYjNmN2MyMmE2MTUxMzZkOGRlODBlNjc0ZTE5MzZk
11
+ ZTg0YTYwYmFkYzQxYzI0YWE0MDgxNzEyZGQ4MzQ3YzFjM2VhYWQ=
12
12
  data.tar.gz: !binary |-
13
- N2EyOTY0YjlkMGI5OWIzZjUwNjRmMmRmYWEwZTIzNWYyM2VjYjgzZDgzZWNl
14
- ZTk0NzYyM2M4NmQxMDYyMTliYWU1MGJjYjNmMjAyMWNkOWM5ZWQ4OWYwMjky
15
- MzA5YWQ0MWFhNmZkYmI0NTA1ZWQ1NDMxOTZiNGFkZmE3ODAxZGQ=
13
+ N2M4YTM5YWE2MGRiNjljNzEyNjBiYzU5YzU2ZTAyNGQ1MDk0NGI2YmI0Zjg2
14
+ NmIzNGRhZjYxNzBmYjk1MGQzZjQxN2ZkODJlZTdiYzNkM2NmOGFkYjA3MmI1
15
+ NzU0MmE5MWIzMjc5YjlkMjFmM2ZjYTU5NzA5NTY0YmJlM2Q1NGI=
data/README.md CHANGED
@@ -20,16 +20,16 @@ Then:
20
20
 
21
21
  ```ruby
22
22
  Hey.api_token = '3858f62230ac3c915f300c664312c63f'
23
- Hey::Yo.all
23
+ Hey::Yo.all # or yo a specific user...
24
+ Hey::Yo.user "WORLDCUP"
25
+ Hey::Subscriber.count
24
26
 
25
27
  # or set api_token on a Yo instance:
26
28
 
27
- Hey::Yo.new(api_token: '3858f62230ac3c915f300c664312c63f').all
28
-
29
- # or Yo a specific user:
30
-
31
- Hey::Yo.user "WORLDCUP" # or...
32
- Hey::Yo.new(api_token: '3858f62230ac3c915f300c664312c63f').user "WORLDCUP"
29
+ yo = Hey::Yo.new(api_token: '3858f62230ac3c915f300c664312c63f')
30
+ yo.all
31
+ yo.user "WORLDCUP"
32
+ yo.subscribers.count
33
33
  ```
34
34
 
35
35
  That's it!
data/lib/hey.rb CHANGED
@@ -1,6 +1,5 @@
1
- require 'net/http'
2
- require 'json'
3
1
  require 'hey/yo'
2
+ require 'hey/subscriber'
4
3
 
5
4
  module Hey
6
5
 
@@ -0,0 +1,58 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ module Hey
5
+ class Dispatcher
6
+
7
+ # (Optional) Should be set if an API token hasn't be set
8
+ # on the Hey module.
9
+ attr_accessor :api_token
10
+
11
+ # Optionally accepts a hash containing an API token.
12
+ # Defaults to the API token set on the Hey module.
13
+ #
14
+ # Hey::Yo.new api_token: '3858f62230ac3c915f300c664312c63f'
15
+ def initialize options = {}
16
+ @api_token = options[:api_token] || Hey.api_token
17
+ end
18
+
19
+ private
20
+
21
+ def uri route, data = nil #:nodoc:
22
+ URI("http://api.justyo.co/#{route}/#{data}")
23
+ end
24
+
25
+ def get route #:nodoc:
26
+ response = Net::HTTP.get_response uri(route, "?api_token=#{api_token}")
27
+ raise_for_invalid_code! response
28
+ response
29
+ end
30
+
31
+ def post route, params = {} #:nodoc:
32
+ params = params.merge api_token: api_token
33
+ response = Net::HTTP.post_form uri(route), params
34
+ raise_for_invalid_code! response
35
+ response
36
+ end
37
+
38
+ def raise_for_missing_api_token! #:nodoc:
39
+ raise MissingAPITokenError unless api_token
40
+ end
41
+
42
+ def raise_for_invalid_code! response #:nodoc:
43
+ body = get_response_body(response)
44
+ return unless body && body["code"] == 141
45
+ case body["error"]
46
+ when "NO SUCH USER"
47
+ raise NoSuchUserError
48
+ else
49
+ raise InvalidAPITokenError
50
+ end
51
+ end
52
+
53
+ def get_response_body request #:nodoc:
54
+ request && request.body ? JSON.parse(request.body) : nil
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,24 @@
1
+ require 'hey/dispatcher'
2
+
3
+ module Hey
4
+ # Sends requests to the Yo API subscriber endpoint.
5
+ class Subscriber < Dispatcher
6
+
7
+ # Sends a request to the +subscribers_count+ endpoint.
8
+ # Raises a +MissingAPITokenError+ error if an API token
9
+ # hasn't been set on the Hey module or Yo instance.
10
+ def count
11
+ raise_for_missing_api_token!
12
+ response = get 'subscribers_count'
13
+ body = get_response_body response
14
+ body['count']
15
+ end
16
+
17
+ # Sends a request to the +subscribers_count+ endpoint using the
18
+ # API token set on the Hey module.
19
+ def self.count
20
+ new.count
21
+ end
22
+
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module Hey
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -1,25 +1,16 @@
1
+ require 'hey/dispatcher'
2
+ require 'hey/subscriber'
3
+
1
4
  module Hey
2
- # Sends requests to the Yo API endpoint.
3
- class Yo
4
-
5
- # (Optional) Should be set if an API token hasn't be set
6
- # on the Hey module.
7
- attr_accessor :api_token
8
-
9
- # Optionally accepts a hash containing an API token.
10
- # Defaults to the API token set on the Hey module.
11
- #
12
- # Hey::Yo.new api_token: '3858f62230ac3c915f300c664312c63f'
13
- def initialize options = {}
14
- @api_token = options[:api_token] || Hey.api_token
15
- end
5
+ # Sends requests to the Yo API yo endpoints.
6
+ class Yo < Dispatcher
16
7
 
17
8
  # Sends a request to the +yoall+ endpoint.
18
9
  # Raises a +MissingAPITokenError+ error if an API token
19
10
  # hasn't been set on the Hey module or Yo instance.
20
11
  def all
21
- raise MissingAPITokenError unless api_token
22
- post_request 'yoall'
12
+ raise_for_missing_api_token!
13
+ post 'yoall'
23
14
  end
24
15
 
25
16
  # Sends a request to a user using the +yo+ endpoint.
@@ -28,8 +19,17 @@ module Hey
28
19
  #
29
20
  # Hey::Yo.new.user "worldcup"
30
21
  def user name
31
- raise MissingAPITokenError unless api_token
32
- post_request 'yo', username: name
22
+ raise_for_missing_api_token!
23
+ post 'yo', username: name
24
+ end
25
+
26
+ # Accesses subscriber endpoints using the same API key
27
+ #
28
+ # yo = Hey::Yo.new api_token: '3858f62230ac3c915f300c664312c63f'
29
+ # yo.subscribers.count
30
+ # # => 2
31
+ def subscribers
32
+ Hey::Subscriber.new api_token: api_token
33
33
  end
34
34
 
35
35
  # Sends a request to the +yoall+ endpoint using the
@@ -47,26 +47,5 @@ module Hey
47
47
  new.user name
48
48
  end
49
49
 
50
- private
51
-
52
- def post_request method, params = {} #:nodoc:
53
- params = params.merge api_token: api_token
54
- uri = URI("http://api.justyo.co/#{method}/")
55
- request = Net::HTTP.post_form uri, params
56
- raise_for_invalid_code! request
57
- request
58
- end
59
-
60
- def raise_for_invalid_code! request #:nodoc:
61
- response = JSON.parse(request.body)
62
- return unless response["code"] && response["code"] == 141
63
- case response["error"]
64
- when "NO SUCH USER"
65
- raise NoSuchUserError
66
- else
67
- raise InvalidAPITokenError
68
- end
69
- end
70
-
71
50
  end
72
51
  end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+ require 'hey/dispatcher'
3
+
4
+ class DispatcherTest < Minitest::Test
5
+
6
+ def teardown
7
+ WebMock.reset!
8
+ Hey.api_token = nil
9
+ end
10
+
11
+ def test_sets_api_token_on_instance
12
+ dispatcher = Hey::Dispatcher.new api_token: 'foo'
13
+ assert_equal 'foo', dispatcher.api_token
14
+ end
15
+
16
+ def test_defaults_to_hey_api_token
17
+ Hey.api_token = 'bar'
18
+ dispatcher = Hey::Dispatcher.new
19
+ assert_equal 'bar', dispatcher.api_token
20
+ end
21
+
22
+ def test_get_request
23
+ stub_get = stub_request(:get, "http://api.justyo.co/bar/?api_token=foo")
24
+ dispatcher = Hey::Dispatcher.new api_token: 'foo'
25
+ dispatcher.send :get, 'bar'
26
+ assert_requested stub_get
27
+ end
28
+
29
+ def test_post_request
30
+ stub_post = stub_request(:post, "http://api.justyo.co/bar/")
31
+ .with(:body => {"api_token"=>"foo"})
32
+ dispatcher = Hey::Dispatcher.new api_token: 'foo'
33
+ dispatcher.send :post, 'bar'
34
+ assert_requested stub_post
35
+ end
36
+
37
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+ require 'hey/subscriber'
3
+
4
+ class SubscriberTest < Minitest::Test
5
+
6
+ def teardown
7
+ WebMock.reset!
8
+ Hey.api_token = nil
9
+ end
10
+
11
+ def test_count_sends_api_request
12
+ stub_get = stub_request(:get, "http://api.justyo.co/subscribers_count/?api_token=foo")
13
+ .to_return(body: '{"count": 2}')
14
+ count = Hey::Subscriber.new(api_token: 'foo').count
15
+ assert_requested stub_get
16
+ assert_equal count, 2
17
+ end
18
+
19
+ def test_class_method_all_sends_api_request
20
+ stub_get = stub_request(:get, "http://api.justyo.co/subscribers_count/?api_token=foo")
21
+ .to_return(body: '{"count": 2}')
22
+ Hey.api_token = 'foo'
23
+ count = Hey::Subscriber.count
24
+ assert_requested stub_get
25
+ assert_equal count, 2
26
+ end
27
+
28
+ end
@@ -1,4 +1,3 @@
1
1
  require 'minitest'
2
2
  require 'minitest/autorun'
3
- require 'minitest/mock'
4
- require 'minitest/stub_const'
3
+ require 'webmock/minitest'
@@ -4,38 +4,38 @@ require 'hey/yo'
4
4
  class YoTest < Minitest::Test
5
5
 
6
6
  def teardown
7
+ WebMock.reset!
7
8
  Hey.api_token = nil
8
9
  end
9
10
 
10
- def test_sets_api_token_on_instance
11
- yo = Hey::Yo.new(api_token: 'foo')
12
- assert_equal 'foo', yo.api_token
13
- end
14
-
15
- def test_defaults_to_hey_api_token
16
- Hey.api_token = 'bar'
17
- yo = Hey::Yo.new
18
- assert_equal 'bar', yo.api_token
19
- end
20
-
21
11
  def test_all_sends_api_request
22
- yo = Hey::Yo.new(api_token: 'foo')
23
- assert_sends_yo {yo.all}
12
+ stub_post = stub_request(:post, "http://api.justyo.co/yoall/")
13
+ .with(:body => {"api_token"=>"foo"})
14
+ Hey::Yo.new(api_token: 'foo').all
15
+ assert_requested stub_post
24
16
  end
25
17
 
26
18
  def test_class_method_all_sends_api_request
19
+ stub_post = stub_request(:post, "http://api.justyo.co/yoall/")
20
+ .with(:body => {"api_token"=>"foo"})
27
21
  Hey.api_token = 'foo'
28
- assert_sends_yo {Hey::Yo.all}
22
+ Hey::Yo.all
23
+ assert_requested stub_post
29
24
  end
30
25
 
31
26
  def test_user_sends_api_request
32
- yo = Hey::Yo.new(api_token: 'foo')
33
- assert_sends_yo {yo.user('YOJOBS')}
27
+ stub_post = stub_request(:post, "http://api.justyo.co/yo/")
28
+ .with(:body => {"api_token"=>"foo", "username"=>"YOJOBS"})
29
+ Hey::Yo.new(api_token: 'foo').user('YOJOBS')
30
+ assert_requested stub_post
34
31
  end
35
32
 
36
33
  def test_class_method_user_sends_api_request
34
+ stub_post = stub_request(:post, "http://api.justyo.co/yo/")
35
+ .with(:body => {"api_token"=>"foo", "username"=>"YOJOBS"})
37
36
  Hey.api_token = 'foo'
38
- assert_sends_yo {Hey::Yo.user('YOJOBS')}
37
+ Hey::Yo.user('YOJOBS')
38
+ assert_requested stub_post
39
39
  end
40
40
 
41
41
  def test_raises_no_api_token_error
@@ -46,14 +46,11 @@ class YoTest < Minitest::Test
46
46
  end
47
47
  end
48
48
 
49
- def assert_sends_yo(&block)
50
- mock_response = Net::HTTPResponse.new('1.1', '201', 'CREATED')
51
- mock_response.stub(:read_body, "{\"code\": 201}") do
52
- mock = MiniTest::Mock.new
53
- mock.expect(:post_form, mock_response, [URI, Hash])
54
- Net.stub_const(:HTTP, mock, &block)
55
- assert mock.verify
56
- end
49
+ def test_subscribers_returns_subscriber
50
+ yo = Hey::Yo.new api_token: 'foo'
51
+ subscriber = yo.subscribers
52
+ assert subscriber.is_a?(Hey::Subscriber)
53
+ assert_equal subscriber.api_token, yo.api_token
57
54
  end
58
55
 
59
- end
56
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hey
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Jennings
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-17 00:00:00.000000000 Z
11
+ date: 2014-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '5.3'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest-stub-const
42
+ name: webmock
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ~>
46
46
  - !ruby/object:Gem::Version
47
- version: '0.2'
47
+ version: '1.18'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: '0.2'
54
+ version: '1.18'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rdoc
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -75,9 +75,13 @@ files:
75
75
  - README.md
76
76
  - Rakefile
77
77
  - lib/hey.rb
78
+ - lib/hey/dispatcher.rb
79
+ - lib/hey/subscriber.rb
78
80
  - lib/hey/version.rb
79
81
  - lib/hey/yo.rb
82
+ - test/dispatcher_test.rb
80
83
  - test/hey_test.rb
84
+ - test/subscriber_test.rb
81
85
  - test/test_helper.rb
82
86
  - test/yo_test.rb
83
87
  homepage: https://github.com/jackjennings/hey
@@ -105,6 +109,8 @@ signing_key:
105
109
  specification_version: 4
106
110
  summary: Sends a yo
107
111
  test_files:
112
+ - test/dispatcher_test.rb
108
113
  - test/hey_test.rb
114
+ - test/subscriber_test.rb
109
115
  - test/test_helper.rb
110
116
  - test/yo_test.rb