strawpoll 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21ed499f4a57c52be893a7929b54b2f0085a60e3
4
+ data.tar.gz: 475d628f4e84bf5f43531b1969e42e0a0d45fbc8
5
+ SHA512:
6
+ metadata.gz: 3ed1c04aa5b619fecbbde838a2cd792fd28aed51a988aa9194e78c5b84d2cc8e8ba6bf4fb1e59e5e967f5b1f474191696db37a34b10290f2cf54a73864c317ec
7
+ data.tar.gz: 600fc022729eaf6abb05a3e036d628ec8bf0d87757c4831c73a130d983657dae29a92b2f5accf826c241171e59dc178b032c77b37c066360dcc7c58a2c92dae9
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport', require: false
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ gem 'webmock'
8
+ end
9
+ # Specify your gem's dependencies in strawpoll.gemspec
10
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Pavel Astraukh
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # Strawpoll
2
+
3
+ With the help of this gem you can manipulate with polls of strawpoll.me service. You can get, create polls and also vote.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'strawpoll'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install strawpoll
20
+
21
+ ## Usage
22
+
23
+ You can create poll this way:
24
+
25
+ ```ruby
26
+ Strawpoll::Poll.create(title: 'Do you like this gem?', options: ['Yes', 'No'])
27
+ => #<Strawpoll::Poll:0x007fb8e4162f70 @id=3057139, @title="Do you like this gem?", @options=["Yes", "No"], @votes=[], @multi=false, @permissive=false>
28
+ ```
29
+
30
+ and this way:
31
+
32
+ ```ruby
33
+ poll = Strawpoll::Poll.new(title: 'Do you like this gem?', options: ["Yes", "No"])
34
+ poll.create
35
+ => #<Strawpoll::Poll:0x007fb8e40a2dd8 @id=3057160, @title="Do you like this gem?", @options=["Yes", "No"], @votes=[], @multi=false, @permissive=false>
36
+ ```
37
+
38
+ Also to reload poll(to update votes):
39
+
40
+ ```ruby
41
+ poll.reload
42
+ => #<Strawpoll::Poll:0x007fb8e40a2dd8 @id=3057160, @title="Do you like this gem?", @options=["Yes", "No"], @votes=[0, 0], @multi="false", @permissive="false">
43
+ ```
44
+
45
+ You can get existing poll:
46
+
47
+ ```ruby
48
+ Strawpoll::Poll.get(3057160)
49
+ => #<Strawpoll::Poll:0x007fb8e70519d0 @id=3057160, @title="Do you like this gem?", @options=["Yes", "No"], @votes=[0, 0], @multi="false", @permissive="false">
50
+ ```
51
+
52
+ You can vote and get votes count:
53
+
54
+ ```ruby
55
+ poll.vote('Yes')
56
+ => #<Strawpoll::Poll:0x007fb8e40a2dd8 @id=3057160, @title="Do you like this gem?", @options=["Yes", "No"], @votes=[1, 0], @multi="false", @permissive="false">
57
+ poll.votes_count
58
+ => {"Yes"=>1, "No"=>0}
59
+ poll.votes_count("Yes")
60
+ => 1
61
+ ```
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it ( https://github.com/enotpoloskun/strawpoll/fork )
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,9 @@
1
+ require 'active_support/core_ext/hash/keys'
2
+ require "strawpoll/version"
3
+ require "strawpoll/poll"
4
+ require "strawpoll/api"
5
+ require "json"
6
+
7
+ module Strawpoll
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,51 @@
1
+ module Strawpoll
2
+ class API
3
+ API_URI = URI("http://strawpoll.me/")
4
+
5
+ class << self
6
+ def http
7
+ @http ||= Net::HTTP.new(API_URI.host, API_URI.port)
8
+ end
9
+
10
+ def get(id)
11
+ perform("/api/v2/polls/#{id}", :get).parsed_body.symbolize_keys
12
+ end
13
+
14
+ def create(params)
15
+ perform("/api/v2/polls", :post, params).parsed_body.symbolize_keys
16
+ end
17
+
18
+ def patch(id, params)
19
+ perform("/api/v2/polls/#{id}", :patch, params).parsed_body.symbolize_keys
20
+ end
21
+
22
+ def perform(uri, method, params = {})
23
+ request = case method
24
+ when :get
25
+ Net::HTTP::Get.new(uri)
26
+ when :post
27
+ Net::HTTP::Post.new(uri).tap do |request|
28
+ request.set_form_data(params)
29
+ end
30
+ when :patch
31
+ Net::HTTP::Patch.new(uri).tap do |request|
32
+ request.body = params.to_json
33
+ request.content_type = 'application/json'
34
+ end
35
+ end
36
+
37
+ http.request(request).tap do |response|
38
+ raise Strawpoll::API::Error.new(response.body) unless response.kind_of? Net::HTTPSuccess
39
+
40
+ response.parsed_body = JSON.parse(response.body) rescue {}
41
+ end
42
+ end
43
+ end
44
+
45
+ class Error < Exception; end
46
+ end
47
+ end
48
+
49
+ class Net::HTTPResponse
50
+ attr_accessor :parsed_body
51
+ end
@@ -0,0 +1,80 @@
1
+ module Strawpoll
2
+ class Poll
3
+ attr_accessor :id, :title, :options, :votes, :multi, :permissive
4
+
5
+ def initialize(options = {})
6
+ options.symbolize_keys!
7
+
8
+ options = {
9
+ multi: false,
10
+ permissive: false,
11
+ votes: [],
12
+ }.merge(options)
13
+
14
+ [:id, :title, :options, :votes, :multi, :permissive].each do |attribute|
15
+ self.send("#{attribute}=", options[attribute])
16
+ end
17
+
18
+ self
19
+ end
20
+
21
+ def create
22
+ [:title, :options].each do |attribute|
23
+ raise ArgumentError.new('Attributes title and options are required') unless self.send(attribute)
24
+ end
25
+
26
+ response = API.create(title: title, options: options, multi: multi, permissive: permissive)
27
+
28
+ self.id = response[:id]
29
+
30
+ self
31
+ end
32
+
33
+ def reload
34
+ raise ArgumentError.new('You must specify id') unless id
35
+
36
+ API.get(id).tap do |response|
37
+ self.title = response[:title]
38
+ self.multi = response[:multi]
39
+ self.permissive = response[:permissive]
40
+ self.options = response[:options]
41
+ self.votes = response[:votes]
42
+ end
43
+
44
+ self
45
+ end
46
+
47
+ def votes_count(option = nil)
48
+ result = options.inject({}) do |result, option|
49
+ result[option] = votes[options.index(option)] || 0
50
+
51
+ result
52
+ end
53
+
54
+ option.nil? ? result : result[option]
55
+ end
56
+
57
+ def vote(option)
58
+ option = option.to_s
59
+ raise ArgumentError.new('Wrong option') unless options.include?(option)
60
+
61
+ API.patch(id, { votes: [options.index(option)] })
62
+
63
+ reload
64
+ end
65
+
66
+ class << self
67
+ def get(id)
68
+ raise ArgumentError.new('You must specify id') unless id
69
+
70
+ response = API.get(id)
71
+
72
+ self.new(response.merge(id: id))
73
+ end
74
+
75
+ def create(options = {})
76
+ self.new(options).create
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Strawpoll
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,85 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+
7
+ $LOAD_PATH.unshift ::File.realpath "../lib", __dir__
8
+
9
+ require 'strawpoll'
10
+ require 'webmock/rspec'
11
+ require 'rspec/expectations'
12
+
13
+ RSpec.configure do |config|
14
+ # rspec-expectations config goes here. You can use an alternate
15
+ # assertion/expectation library such as wrong or the stdlib/minitest
16
+ # assertions if you prefer.
17
+ config.expect_with :rspec do |expectations|
18
+ # This option will default to `true` in RSpec 4. It makes the `description`
19
+ # and `failure_message` of custom matchers include text for helper methods
20
+ # defined using `chain`, e.g.:
21
+ # be_bigger_than(2).and_smaller_than(4).description
22
+ # # => "be bigger than 2 and smaller than 4"
23
+ # ...rather than:
24
+ # # => "be bigger than 2"
25
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
26
+ end
27
+
28
+ # rspec-mocks config goes here. You can use an alternate test double
29
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
30
+ config.mock_with :rspec do |mocks|
31
+ # Prevents you from mocking or stubbing a method that does not exist on
32
+ # a real object. This is generally recommended, and will default to
33
+ # `true` in RSpec 4.
34
+ mocks.verify_partial_doubles = true
35
+ end
36
+
37
+ # The settings below are suggested to provide a good initial experience
38
+ # with RSpec, but feel free to customize to your heart's content.
39
+ =begin
40
+ # These two settings work together to allow you to limit a spec run
41
+ # to individual examples or groups you care about by tagging them with
42
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
43
+ # get run.
44
+ config.filter_run :focus
45
+ config.run_all_when_everything_filtered = true
46
+
47
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
48
+ # For more details, see:
49
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
50
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
51
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
52
+ config.disable_monkey_patching!
53
+
54
+ # This setting enables warnings. It's recommended, but in some cases may
55
+ # be too noisy due to issues in dependencies.
56
+ config.warnings = true
57
+
58
+ # Many RSpec users commonly either run the entire suite or an individual
59
+ # file, and it's useful to allow more verbose output when running an
60
+ # individual spec file.
61
+ if config.files_to_run.one?
62
+ # Use the documentation formatter for detailed output,
63
+ # unless a formatter has already been configured
64
+ # (e.g. via a command-line flag).
65
+ config.default_formatter = 'doc'
66
+ end
67
+
68
+ # Print the 10 slowest examples and example groups at the
69
+ # end of the spec run, to help surface which specs are running
70
+ # particularly slow.
71
+ config.profile_examples = 10
72
+
73
+ # Run specs in random order to surface order dependencies. If you find an
74
+ # order dependency and want to debug it, you can fix the order by providing
75
+ # the seed, which is printed after each run.
76
+ # --seed 1234
77
+ config.order = :random
78
+
79
+ # Seed global randomization in this process using the `--seed` CLI option.
80
+ # Setting this allows you to use `--seed` to deterministically reproduce
81
+ # test failures related to randomization by passing the same `--seed` value
82
+ # as the one that triggered the failure.
83
+ Kernel.srand config.seed
84
+ =end
85
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe Strawpoll::Poll do
4
+ context 'Instance methods' do
5
+ describe '#create' do
6
+ let(:poll) { Strawpoll::Poll.new(options: ["Option1", "Option2"], title: 'Poll') }
7
+
8
+ before do
9
+ stub_request(:post, "strawpoll.me/api/v2/polls").to_return(status: 200, body: { id: 1234 }.to_json)
10
+ end
11
+
12
+ it 'should assign id after save' do
13
+ poll.create.id.should eq(1234)
14
+ end
15
+ end
16
+
17
+ describe '#vote' do
18
+ end
19
+ end
20
+
21
+ context 'Class methods' do
22
+ describe '#get' do
23
+ context 'Valid params' do
24
+ before do
25
+ stub_request(:get, "http://strawpoll.me/api/v2/polls/1234").to_return(body: { options: ['Option1', 'Option2', 'Option3'] }.to_json, status: 200)
26
+ end
27
+
28
+ it 'should return poll' do
29
+ expect(Strawpoll::Poll.get('1234')).to be_a Strawpoll::Poll
30
+ end
31
+
32
+ it 'should return poll with certain options' do
33
+ Strawpoll::Poll.get('1234').options.should eq ['Option1', 'Option2', 'Option3']
34
+ end
35
+ end
36
+
37
+ context 'Invalid params' do
38
+ it do
39
+ expect { Strawpoll::Poll.get }.to raise_error(ArgumentError)
40
+ end
41
+ end
42
+
43
+ context 'Api error' do
44
+ before do
45
+ stub_request(:get, "http://strawpoll.me/api/v2/polls/1234").to_return(body: { error: "Api error" }.to_json, status: 404)
46
+ end
47
+
48
+ it do
49
+ expect { Strawpoll::Poll.get('1234') }.to raise_error(Strawpoll::API::Error)
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '#create' do
55
+ context 'Valid params' do
56
+ before do
57
+ stub_request(:post, "strawpoll.me/api/v2/polls").to_return(body: { id: 1234 }.to_json, status: 200)
58
+ end
59
+
60
+ it 'should return poll' do
61
+ expect(Strawpoll::Poll.create(options: ['Option1', 'Options2', 'Options3'], title: 'title', multi: false)).to be_a Strawpoll::Poll
62
+ end
63
+ end
64
+
65
+ context 'Invalid params' do
66
+ it do
67
+ expect {
68
+ Strawpoll::Poll.create(options: ['Option1', 'Options2', 'Options3'], multi: false)
69
+ }.to raise_error(ArgumentError)
70
+ end
71
+ end
72
+
73
+ context 'Api error' do
74
+ before do
75
+ stub_request(:post, "strawpoll.me/api/v2/polls").to_return(body: { error: "Api error" }.to_json, status: 400)
76
+ end
77
+
78
+ it do
79
+ expect {
80
+ Strawpoll::Poll.create(options: ['Option1', 'Options2', 'Options3'], title: 'title', multi: false)
81
+ }.to raise_error(Strawpoll::API::Error)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'strawpoll/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "strawpoll"
8
+ spec.version = Strawpoll::VERSION
9
+ spec.authors = ["Pavel Astraukh"]
10
+ spec.email = ["paladin111333@gmail.com"]
11
+ spec.summary = %q{Ruby client for strawpoll.me}
12
+ spec.description = %q{With the help of this gem you can manipulate with polls of strawpoll.me service}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strawpoll
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Astraukh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: With the help of this gem you can manipulate with polls of strawpoll.me
42
+ service
43
+ email:
44
+ - paladin111333@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/strawpoll.rb
56
+ - lib/strawpoll/api.rb
57
+ - lib/strawpoll/poll.rb
58
+ - lib/strawpoll/version.rb
59
+ - spec/spec_helper.rb
60
+ - spec/strawpoll/poll_spec.rb
61
+ - strawpoll.gemspec
62
+ homepage: ''
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Ruby client for strawpoll.me
86
+ test_files:
87
+ - spec/spec_helper.rb
88
+ - spec/strawpoll/poll_spec.rb