hey 1.3.0 → 1.3.1

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MTU2MWYxNDU2NzFmYTY1MzFlZTcwZDY4ZjU4Y2ZkMzBjZWQ4Mjc5Yg==
4
+ YTExZTdlZDQzYTM3MzllMzNhZDI0YjU1MTljYmY0M2Q1MGMzZmY4MQ==
5
5
  data.tar.gz: !binary |-
6
- YTNiYTliNzc0ODEyZDMxMTdlNTdlYWIxN2FhMzUxOGEyNTE1ZjQ1MQ==
6
+ ZjFmZTY3Mzg5MDliOTQ1ZDdhMzViYzEyYmFkOTUzNjBmZDBjMzAyYQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjQ3ZjI4NDMxZjIyM2NhMWFkYzRmZmNmZTFmNGQ3MDhhYjJhYjBiYjcyNjQ2
10
- MjExNGJlNDFhMWVmYjdlYWFlYTExNjNhYzlkMzQ0YjM5ZTdmYjBmMDEwNWYy
11
- NzM1ZjUxMWQ5ZDQyYWI5ZmEyY2I3YmJhYTE3NjUzZWVjNmRjZjY=
9
+ NWFlNjJlNGVkYTA2NDIwZTc5MDdlMmNhZDE0NTFjMTE4ZjViNDYyMDY4OTMy
10
+ ZGI2MWZiNzkyZTJjYTU2OWZjMzM5NDU0NjBjNzM3NmFhZTY4YjI0MTJhNDRk
11
+ N2I5M2VlMmFhYWU1YjgzZTkwMmI2ZGZjZDJhMGEzZWI5YjQyODk=
12
12
  data.tar.gz: !binary |-
13
- NjkzYzQ2MjVmMjFkMmNkNzc0NWZlODMwOGZiYzMwNGM1ZDlhMGVmYzAyYWY3
14
- NzIxNDJjMDI3ODEzOGE3NzcxODIwNmI3NGIxM2M3MTFkZTUzNmI3ZTE0YWYx
15
- YWZkODNmMTU1ZThmNzk2OWQ5MjQ3Mjk4NGRlMzc2ZDM0MjZjZmI=
13
+ ZWZhNThhNTE2MTQzNDFjNjU5ZDdhYjEwOTUwYWY2Yzg2NWZkOTg2NzM2ZjY5
14
+ YjRmZDgzMDQ3MzkyMjI1ZDYyMjg2ZTdmMWE1MDBiNDMzNWQ3YjkyZDM3YWY4
15
+ NzY2NDI1MjAxMjg5MzliNjE4MzgyMmU5OTYzMDNkZWMwNzBjZmY=
data/README.md CHANGED
@@ -16,23 +16,62 @@ gem install hey
16
16
 
17
17
  First [get an API token](http://yoapi.justyo.co).
18
18
 
19
- Then:
19
+ There are two ways of setting you API token, depending on your requirements. If your program is only working with a single API token, you can set it in an initializer or at the start of your script.
20
20
 
21
21
  ```ruby
22
22
  Hey.api_token = "3858f62230ac3c915f300c664312c63f"
23
- Hey::Yo.all # or yo a specific user...
24
- Hey::Yo.user "WORLDCUP"
25
- Hey::Subscriber.count
23
+ ```
24
+
25
+ If you need to use multiple API tokens, any `hey` class will accept an `api_token` on initialization.
26
26
 
27
- # Additional parameters can be sent to Yo endpoints.
28
- Hey::Yo.all link: "http://justyo.co"
29
- Hey::Yo.user "WORLDCUP", link: "http://justyo.co"
27
+ ```ruby
28
+ Hey::Yo.new api_token: "3858f62230ac3c915f300c664312c63f"
29
+ ```
30
30
 
31
- # or set api_token on a Yo instance:
31
+ An API token set when initializing an object takes precidence over the API token set on the `Hey` module.
32
32
 
33
- yo = Hey::Yo.new api_token: "3858f62230ac3c915f300c664312c63f"
33
+ ## Yo All
34
+
35
+ Sends a yo to all of the account's subscribers.
36
+
37
+ ```ruby
38
+ Hey::Yo.all
39
+ # or
40
+ yo = Hey::Yo.new api_token: "..."
34
41
  yo.all
42
+ ```
43
+
44
+ Accepts either `link` or `location` in a hash of parameters (optional).
45
+
46
+ ```ruby
47
+ Hey::Yo.all link: "https://github.com/jackjennings/hey"
48
+ ```
49
+
50
+ ## Yo User
51
+
52
+ Sends a yo to a specific user.
53
+
54
+ ```ruby
55
+ Hey::Yo.user "WORLDCUP"
56
+ # or
57
+ yo = Hey::Yo.new api_token: "..."
35
58
  yo.user "WORLDCUP"
59
+ ```
60
+
61
+ Accepts either `link` or `location` in a hash of parameters (optional).
62
+
63
+ ```ruby
64
+ Hey::Yo.user "WORLDCUP" link: "https://github.com/jackjennings/hey"
65
+ ```
66
+
67
+ ## Subscriber Count
68
+
69
+ Returns the number of subscribers.
70
+
71
+ ```ruby
72
+ Hey::Subscriber.count
73
+ # or
74
+ yo = Hey::Yo.new api_token: "3858f62230ac3c915f300c664312c63f"
36
75
  yo.subscribers.count
37
76
  ```
38
77
 
@@ -1,5 +1,4 @@
1
- require 'json'
2
- require 'net/http'
1
+ require 'hey/request'
3
2
 
4
3
  module Hey
5
4
  class Dispatcher
@@ -13,45 +12,38 @@ module Hey
13
12
  #
14
13
  # Hey::Yo.new api_token: '3858f62230ac3c915f300c664312c63f'
15
14
  def initialize options = {}
16
- @api_token = options[:api_token] || Hey.api_token
15
+ @api_token = options[:api_token]
17
16
  end
18
17
 
19
- private
20
-
21
- def uri route, data = nil #:nodoc:
22
- URI("http://api.justyo.co/#{route}/#{data}")
18
+ # Returns the API token to be used when making requests. This will either
19
+ # be the api_token set on initialization or the token set on the Hey
20
+ # module itself.
21
+ def api_token
22
+ @api_token || Hey.api_token
23
23
  end
24
24
 
25
+ private
26
+
25
27
  def get route #:nodoc:
26
- response = Net::HTTP.get_response uri(route, "?api_token=#{api_token}")
27
- raise_for_invalid_code! response
28
- response
28
+ request :get, route
29
29
  end
30
30
 
31
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
32
+ request :post, route, params
36
33
  end
37
34
 
38
- def raise_for_missing_api_token! #:nodoc:
39
- raise MissingAPITokenError unless api_token
35
+ def request method, route, params = {} #:nodoc:
36
+ merge_api_token! params
37
+ Request.of_type(method).new route, params
40
38
  end
41
39
 
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
40
+ def merge_api_token! params #:nodoc:
41
+ raise_for_missing_api_token!
42
+ params.merge! api_token: api_token
51
43
  end
52
44
 
53
- def get_response_body request #:nodoc:
54
- request && request.body ? JSON.parse(request.body) : nil
45
+ def raise_for_missing_api_token! #:nodoc:
46
+ raise MissingAPITokenError unless api_token
55
47
  end
56
48
 
57
49
  end
@@ -0,0 +1,12 @@
1
+ require 'hey/request/get'
2
+ require 'hey/request/post'
3
+
4
+ module Hey
5
+ module Request
6
+
7
+ def self.of_type method
8
+ self.const_get method.to_s.capitalize
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,50 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ module Hey
5
+ module Request
6
+ class Base
7
+
8
+ URL = "http://api.justyo.co"
9
+ ERROR_CODE = 141
10
+
11
+ attr_accessor :response
12
+
13
+ def initialize route, params = {}
14
+ @response = send_request route, params
15
+ raise_for_invalid_code!
16
+ end
17
+
18
+ def uri route, data = nil #:nodoc:
19
+ URI([URL, route, data].join("/"))
20
+ end
21
+
22
+ def raise_for_invalid_code! #:nodoc:
23
+ return unless has_error?
24
+ case error_message
25
+ when "NO SUCH USER"
26
+ raise NoSuchUserError
27
+ else
28
+ raise InvalidAPITokenError
29
+ end
30
+ end
31
+
32
+ def has_error?
33
+ json && json["code"] == ERROR_CODE
34
+ end
35
+
36
+ def error_message
37
+ json["error"]
38
+ end
39
+
40
+ def get_response_body request #:nodoc:
41
+ request && request.body ? JSON.parse(request.body) : nil
42
+ end
43
+
44
+ def json
45
+ @json ||= get_response_body @response
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,20 @@
1
+ require 'hey/request/base'
2
+
3
+ module Hey
4
+ module Request
5
+ class Get < Base
6
+
7
+ def send_request route, params
8
+ Net::HTTP.get_response uri(route, url_params(params))
9
+ end
10
+
11
+ def url_params params
12
+ parts = params.reduce([]) do |arr, param|
13
+ arr << "#{param[0]}=#{param[1]}"
14
+ end
15
+ "?" << parts.join("&") if parts.any?
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ require 'hey/request/base'
2
+
3
+ module Hey
4
+ module Request
5
+ class Post < Base
6
+
7
+ def send_request route, params
8
+ Net::HTTP.post_form uri(route), params
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -8,10 +8,8 @@ module Hey
8
8
  # Raises a +MissingAPITokenError+ error if an API token
9
9
  # hasn't been set on the Hey module or Yo instance.
10
10
  def count
11
- raise_for_missing_api_token!
12
- response = get 'subscribers_count'
13
- body = get_response_body response
14
- body['count']
11
+ request = get 'subscribers_count'
12
+ request.json['count']
15
13
  end
16
14
 
17
15
  # Sends a request to the +subscribers_count+ endpoint using the
@@ -1,3 +1,3 @@
1
1
  module Hey
2
- VERSION = "1.3.0"
3
- end
2
+ VERSION = "1.3.1"
3
+ end
@@ -11,7 +11,6 @@ module Hey
11
11
  # Accepts an optional hash of additional parameters to
12
12
  # send with the request.
13
13
  def all params = {}
14
- raise_for_missing_api_token!
15
14
  post 'yoall', params
16
15
  end
17
16
 
@@ -24,7 +23,6 @@ module Hey
24
23
  # Hey::Yo.new.user "worldcup"
25
24
  # Hey::Yo.new.user "worldcup", link: "http://example.com"
26
25
  def user name, params = {}
27
- raise_for_missing_api_token!
28
26
  params.merge! username: name
29
27
  post 'yo', params
30
28
  end
@@ -4,7 +4,6 @@ require 'hey/dispatcher'
4
4
  class DispatcherTest < Minitest::Test
5
5
 
6
6
  def teardown
7
- WebMock.reset!
8
7
  Hey.api_token = nil
9
8
  end
10
9
 
@@ -19,19 +18,25 @@ class DispatcherTest < Minitest::Test
19
18
  assert_equal 'bar', dispatcher.api_token
20
19
  end
21
20
 
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
21
+ def test_raises_for_absent_api_token
22
+ assert_raises Hey::MissingAPITokenError do
23
+ dispatcher = Hey::Dispatcher.new
24
+ dispatcher.merge_api_token! {}
25
+ end
27
26
  end
28
27
 
29
- def test_post_request
30
- stub_post = stub_request(:post, "http://api.justyo.co/bar/")
31
- .with(:body => {"api_token"=>"foo"})
28
+ def test_merges_api_token
32
29
  dispatcher = Hey::Dispatcher.new api_token: 'foo'
33
- dispatcher.send :post, 'bar'
34
- assert_requested stub_post
30
+ params = {}
31
+ dispatcher.send(:merge_api_token!, params)
32
+ assert 'foo', params[:api_token]
33
+ end
34
+
35
+ def test_raises_for_absent_api_token
36
+ assert_raises Hey::MissingAPITokenError do
37
+ dispatcher = Hey::Dispatcher.new
38
+ dispatcher.send(:merge_api_token!, {})
39
+ end
35
40
  end
36
41
 
37
- end
42
+ end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+ require 'hey/request'
3
+
4
+ class RequestTest < Minitest::Test
5
+
6
+ def teardown
7
+ WebMock.reset!
8
+ Hey.api_token = nil
9
+ end
10
+
11
+ def test_get_request
12
+ stub_get = stub_request(:get, "http://api.justyo.co/bar/")
13
+ request = Hey::Request::Get.new 'bar'
14
+ assert_requested stub_get
15
+ end
16
+
17
+ def test_get_request_with_params
18
+ stub_get = stub_request(:get, "http://api.justyo.co/bar/?foo=baz")
19
+ request = Hey::Request::Get.new 'bar', foo: 'baz'
20
+ assert_requested stub_get
21
+ end
22
+
23
+ def test_post_request
24
+ stub_post = stub_request(:post, "http://api.justyo.co/bar/")
25
+ dispatcher = Hey::Request::Post.new 'bar'
26
+ assert_requested stub_post
27
+ end
28
+
29
+ def test_post_request_with_params
30
+ stub_post = stub_request(:post, "http://api.justyo.co/bar/")
31
+ .with(:body => {"foo"=>"baz"})
32
+ dispatcher = Hey::Request::Post.new 'bar', foo: 'baz'
33
+ assert_requested stub_post
34
+ end
35
+
36
+ def test_of_type_get
37
+ assert_equal Hey::Request::Get, Hey::Request.of_type(:get)
38
+ end
39
+
40
+ def test_of_type_post
41
+ assert_equal Hey::Request::Post, Hey::Request.of_type(:post)
42
+ end
43
+
44
+ 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.3.0
4
+ version: 1.3.1
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-10-01 00:00:00.000000000 Z
11
+ date: 2014-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -76,11 +76,16 @@ files:
76
76
  - Rakefile
77
77
  - lib/hey.rb
78
78
  - lib/hey/dispatcher.rb
79
+ - lib/hey/request.rb
80
+ - lib/hey/request/base.rb
81
+ - lib/hey/request/get.rb
82
+ - lib/hey/request/post.rb
79
83
  - lib/hey/subscriber.rb
80
84
  - lib/hey/version.rb
81
85
  - lib/hey/yo.rb
82
86
  - test/dispatcher_test.rb
83
87
  - test/hey_test.rb
88
+ - test/request_test.rb
84
89
  - test/subscriber_test.rb
85
90
  - test/test_helper.rb
86
91
  - test/yo_test.rb
@@ -111,6 +116,7 @@ summary: Sends a yo
111
116
  test_files:
112
117
  - test/dispatcher_test.rb
113
118
  - test/hey_test.rb
119
+ - test/request_test.rb
114
120
  - test/subscriber_test.rb
115
121
  - test/test_helper.rb
116
122
  - test/yo_test.rb