hunting_season 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzU2NmRjYzJmYzY5NzhjZDMzMmEyMjFlYTEzMTdiYjVlYzcwZWZjYw==
5
+ data.tar.gz: !binary |-
6
+ ZmNkYjBjNWUyMTk4ODQ5NjA1YmE0ZTNmNWY2MjYzMGY3YTk0ZWJkNg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YjUyMmMwNDU1OTEzMGYzNWYwNzM4MGY2ZDQ1ZjlhMzBlYzRiMmI2MTRlOTVm
10
+ Y2MxYzc4YjBmMDEzMTJmOTViN2IwMTZlYmRlOTAyNjg5M2M2YjJlNTZiN2E4
11
+ NTBmMzc5M2NmYjQwNTYwNWNkNjE4YjhkNzUzZGUwMGViNWJkMGU=
12
+ data.tar.gz: !binary |-
13
+ OWZjY2FlNzQxNDMzNjA5ZjEyMWFhZWE4ODVjZjg1MmYwODQxNjlmZjMzMmQz
14
+ YjU2YjYxN2MzZWY4YzA0NzNjMGVhMzJkNGQwNjNkZGQ4MWQ2OTA5MTM1ODIz
15
+ YzhlNDgwZTUwNDQwMTA2ZGJkZjAwNmVkMGQ5YTIzOGQxMDVhZjI=
@@ -0,0 +1 @@
1
+ require 'product_hunt/api'
@@ -0,0 +1,77 @@
1
+ require 'httparty'
2
+ require 'uri'
3
+
4
+ module ProductHunt
5
+ class API
6
+ include HTTParty
7
+ base_uri 'https://api.producthunt.com/v1/'
8
+ format :json
9
+
10
+ def initialize(token)
11
+ @config = { headers: { "Authorization" => "Bearer #{token}" } }
12
+ end
13
+
14
+ def config
15
+ @config
16
+ end
17
+
18
+ def posts(options_or_id_or_nil = nil)
19
+ if options_or_id_or_nil.is_a?(Fixnum)
20
+ post = Post.new(self, options_or_id_or_nil)
21
+ else
22
+ raise InvalidArgumentError.new("#{options_or_id_or_nil} is not supported by ProductHunt::API#posts")
23
+ end
24
+ end
25
+
26
+ class Post
27
+ def initialize(api, id)
28
+ @api = api
29
+ @id = id
30
+ @attributes = nil
31
+ self
32
+ end
33
+
34
+ def [](key)
35
+ if @attributes.nil?
36
+ fetch_self
37
+ end
38
+ @attributes[key]
39
+ end
40
+
41
+ def votes(options = {})
42
+ if @id
43
+ fetch_votes(options).map{|i| Vote.new(@api, i)}
44
+ else
45
+ raise InvalidCallError.new("Cannot call ProductHunt::Post#votes on an unsaved or uninitialized Post")
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def fetch_self
52
+ @attributes = @api.class.get("/posts/#{@id}", @api.config)["post"]
53
+ end
54
+
55
+ def fetch_votes(params)
56
+ path = "/posts/#{@id}/votes"
57
+ if params.is_a?(Enumerable)
58
+ path += "?" + URI.encode_www_form(params)
59
+ end
60
+ @api.class.get(path, @api.config)["votes"]
61
+ end
62
+ end
63
+
64
+ class Vote
65
+ def initialize(api, attributes)
66
+ @attributes = attributes
67
+ end
68
+
69
+ def [](key)
70
+ @attributes[key]
71
+ end
72
+ end
73
+
74
+ class InvalidArgumentError < ArgumentError; end
75
+ class InvalidCallError < RuntimeError; end
76
+ end
77
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe ProductHunt do
4
+
5
+ before(:each) do
6
+ @api = ProductHunt::API.new(ENV['TOKEN'] || 'my-token')
7
+ end
8
+
9
+ describe 'API::Post' do
10
+
11
+ before(:each) do
12
+ @post = @api.posts(3372)
13
+ end
14
+
15
+ it 'implements posts#show and yields the name of the post' do
16
+ @post['name'].should == 'namevine'
17
+ end
18
+
19
+ it 'implements votes#index and yields the first voter' do
20
+ vote = @post.votes.first
21
+
22
+ vote.should be_a(ProductHunt::API::Vote)
23
+ vote['user']['username'].should == '1korda'
24
+ end
25
+
26
+ it 'implements votes#index with pagination' do
27
+ votes = @post.votes(per_page: 1)
28
+ votes.size.should be(1)
29
+
30
+ votes = @post.votes(per_page: 1, older: votes.first['id'])
31
+ votes.size.should be(1)
32
+ votes.first['user']['username'].should == 'mikejarema'
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'hunting_season' # and any other gems you need
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hunting_season
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Jarema
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ description: This gem is a work-in-progress which allows for calls to both the posts#show
56
+ and votes#index endpoints.
57
+ email: mike@jarema.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/hunting_season.rb
63
+ - lib/product_hunt/api.rb
64
+ - spec/product_hunt_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: http://rubygems.org/gems/hunting_season
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.2.2
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Ruby gem which interfaces with Product Hunt's official REST API.
90
+ test_files: []