badgify-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1c7db705cf7e25a1b531dd5a15c676cfd0fe59bbf52928db237e45a82c83c0bd
4
+ data.tar.gz: fbff088287761405ab62369c07dd3f2d6c37d6395cd9e31697ed59c8d1754c95
5
+ SHA512:
6
+ metadata.gz: 6755f10126ad86ddd0aff78db65e8e4297aaceb7b13135137c6fa43e0c520d9e3e808b22d7cd51286ffe83be6aefae41695fab1ed4d5072a7dd40f9cd047b994
7
+ data.tar.gz: 402481d3e7b4afa5bee295e64c47297fa80480fbfd4bf6b0d8bf3f6c33641b0364aace5309f604486451483e262f278202981a788a2fc5cec2d3a99352a772e9
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0 / 2018-07-26
4
+
5
+ - **Breaking change**: Renamed primary class to `badgify`
6
+
7
+
8
+ ## 0.3.0 / 2018-07-17
9
+
10
+ - Refactoring. Same endpoint, just refactored project files and structure.
11
+ - Breaking change: This gem now parses JSON response and returns it (no longer need to parse response in your project).
12
+
13
+ ## 0.2.0 / 2018-07-13
14
+
15
+ - Fix GET method
16
+
17
+ ## 0.1.0 / 2018-07-13
18
+
19
+ - Initial relase. Category endpoint
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in badgify_ruby.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ badgify-ruby (0.3.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.5.2)
10
+ public_suffix (>= 2.0.2, < 4.0)
11
+ ansi (1.5.0)
12
+ builder (3.2.3)
13
+ crack (0.4.3)
14
+ safe_yaml (~> 1.0.0)
15
+ hashdiff (0.3.7)
16
+ minitest (5.11.3)
17
+ minitest-reporters (1.3.0)
18
+ ansi
19
+ builder
20
+ minitest (>= 5.0)
21
+ ruby-progressbar
22
+ public_suffix (3.0.2)
23
+ rake (10.5.0)
24
+ ruby-progressbar (1.9.0)
25
+ safe_yaml (1.0.4)
26
+ vcr (4.0.0)
27
+ webmock (3.4.2)
28
+ addressable (>= 2.3.6)
29
+ crack (>= 0.3.2)
30
+ hashdiff
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ badgify-ruby!
37
+ bundler (~> 1.16)
38
+ minitest (~> 5.0)
39
+ minitest-reporters (~> 1.3, >= 1.3.0)
40
+ rake (~> 10.0)
41
+ vcr (~> 4.0)
42
+ webmock (~> 3.4)
43
+
44
+ BUNDLED WITH
45
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Yafoy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Badgify
2
+
3
+ [![Build Status](https://semaphoreci.com/api/v1/projects/d74d410a-968c-4281-86da-7ecbe89eecd8/2091580/badge.svg)](https://semaphoreci.com/olimart/badgify_ruby)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'badgify-ruby', require: 'badgify'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install badgify-ruby
20
+
21
+ ## Usage
22
+
23
+ In your application
24
+
25
+ 1. Set token
26
+
27
+ ```ruby
28
+ Badgify.token = ENV['BADGIFY_TOKEN']
29
+ ```
30
+
31
+ 2. Retrieve categories
32
+
33
+ ```ruby
34
+ Badgify::Category.list
35
+ Badgify::Category.retrieve('category-slug', { threshold: 10 })
36
+ ```
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ To experiment with that code, run `bin/console` for an interactive prompt.
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "badgify/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "badgify-ruby"
7
+ spec.version = Badgify::VERSION
8
+ spec.authors = ["Olivier"]
9
+ spec.email = ["olivier@yafoy.com"]
10
+
11
+ spec.summary = "A Ruby gem for interacting with Badgify's REST API"
12
+ spec.homepage = "https://github.com/olimart/badgify_ruby"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "minitest-reporters", "~> 1.3", ">= 1.3.0"
26
+ spec.add_development_dependency "vcr", "~> 4.0"
27
+ spec.add_development_dependency "webmock", "~> 3.4"
28
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "badgify"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/badgify.rb ADDED
@@ -0,0 +1,27 @@
1
+ require "badgify/version"
2
+
3
+ # API operations
4
+ require "badgify/http_decorator"
5
+ require "badgify/api_operation"
6
+
7
+ # Resources
8
+ require "badgify/category"
9
+
10
+ # Errors
11
+ require "badgify/errors/service_error"
12
+ require "badgify/errors/api_error"
13
+ require "badgify/errors/api_connection_error"
14
+ require "badgify/errors/invalid_request_error"
15
+ require "badgify/errors/authentication_error"
16
+
17
+ module Badgify
18
+ @api_base = "https://badgify-yafoy.herokuapp.com/api".freeze
19
+
20
+ class << self
21
+ attr_accessor :token, :api_base
22
+ end
23
+
24
+ def self.api_url(url = "")
25
+ @api_base + url
26
+ end
27
+ end
@@ -0,0 +1,8 @@
1
+ module Badgify
2
+ class ApiOperation
3
+
4
+ def self.api
5
+ @api ||= HttpDecorator.new(Badgify.api_url)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ module Badgify
2
+ class Category < ApiOperation
3
+
4
+ def self.list(params = {})
5
+ api.get(resource_url, params)
6
+ end
7
+
8
+ def self.retrieve(id, params = {})
9
+ api.get(resource_url(id.to_s), params)
10
+ end
11
+
12
+ private
13
+
14
+ def self.resource_url(url = "")
15
+ Badgify.api_url("/categories/") + url
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module Badgify
2
+ class APIConnectionError < ServiceError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Badgify
2
+ class APIError < ServiceError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Badgify
2
+ class AuthenticationError < ServiceError
3
+ end
4
+ end
@@ -0,0 +1,10 @@
1
+ module Badgify
2
+ class InvalidRequestError < ServiceError
3
+ attr_accessor :param
4
+
5
+ def initialize(message, param, http_status = nil, http_body = nil, json_body = nil)
6
+ super(message, http_status, http_body, json_body)
7
+ @param = param
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module Badgify
2
+ class ServiceError < StandardError
3
+ attr_reader :message
4
+ attr_reader :http_status
5
+ attr_reader :http_body
6
+ attr_reader :json_body
7
+
8
+ def initialize(message = nil, http_status = nil, http_body = nil, json_body = nil)
9
+ @message = message
10
+ @http_status = http_status
11
+ @http_body = http_body
12
+ @json_body = json_body
13
+ end
14
+
15
+ def to_s
16
+ status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
17
+ "#{status_string}#{@message}"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,170 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+ require 'openssl'
4
+ require 'net/http'
5
+ require 'json'
6
+ require 'uri'
7
+
8
+ module Badgify
9
+ # https://lecklider.com/2016/09/decorating-ruby-s-net-http-for-fun-and-profit.html
10
+ #
11
+ class HttpDecorator
12
+ # Timeouts
13
+ OPEN_TIMEOUT = 10 # in seconds
14
+ READ_TIMEOUT = 120 # in seconds
15
+
16
+ # Content-types
17
+ CONTENT_TYPE_JSON = 'application/json'
18
+ CONTENT_TYPE_FORM = 'application/x-www-form-urlencoded'
19
+ # CONTENT_TYPE_MULTIPART = "multipart/form-data; boundary=#{ Rack::Multipart::MULTIPART_BOUNDARY }"
20
+
21
+ def initialize(url)
22
+ if Badgify.token.nil?
23
+ raise AuthenticationError.new("No token provided")
24
+ end
25
+
26
+ # Build up our HTTP object
27
+ uri = URI(url)
28
+ @http = Net::HTTP.new(uri.hostname, uri.port)
29
+ @http.use_ssl = true if uri.scheme == "https"
30
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if uri.scheme == "https"
31
+ @http.open_timeout = OPEN_TIMEOUT
32
+ @http.read_timeout = READ_TIMEOUT
33
+
34
+ # In local development we can log requests and responses to $stdout.
35
+ # DO NOT EVER do this in production. EVER.
36
+ if ENV['RACK_ENV'] == 'development'
37
+ @http.set_debug_output($stdout)
38
+ end
39
+ end
40
+
41
+ # Open a connection for multiple calls.
42
+ # - Accepts a block, otherwise just opens the connection.
43
+ # - You'll need to close the connection if you just open it.
44
+ def start
45
+ if block_given?
46
+ # Open the connection.
47
+ @http.start unless @http.started?
48
+
49
+ # Yield to the calling block.
50
+ yield(self)
51
+
52
+ # Clean up the connection.
53
+ @http.finish if @http.started?
54
+ else
55
+ # Open the connection.
56
+ @http.start unless @http.started?
57
+ end
58
+ end
59
+
60
+ # Clean up the connection if needed.
61
+ def finish
62
+ @http.finish if @http.started?
63
+ end
64
+
65
+ # GET
66
+ def get(path, params = {})
67
+ uri = URI.parse(path)
68
+ uri.query = URI.encode_www_form(params) unless params.empty?
69
+ request = Net::HTTP::Get.new(uri.to_s)
70
+
71
+ parse fetch(request)
72
+ end
73
+
74
+ # POST
75
+ def post(path, params = {}, as: :json)
76
+ request = Net::HTTP::Post.new(path)
77
+
78
+ case as
79
+ when :json
80
+ request.content_type = CONTENT_TYPE_JSON
81
+ request.body = JSON.generate(params) unless params.empty?
82
+ else
83
+ request.content_type = CONTENT_TYPE_FORM
84
+ request.body = URI.encode_www_form(params) unless params.empty?
85
+ end
86
+
87
+ parse fetch(request)
88
+ end
89
+
90
+ # DELETE
91
+ def delete(path)
92
+ request = Net::HTTP::Delete.new(path)
93
+
94
+ parse fetch(request)
95
+ end
96
+
97
+ # PATCH
98
+ def patch(path, params = {}, as: :form)
99
+ request = Net::HTTP::Patch.new(path)
100
+
101
+ case as
102
+ when :json
103
+ request.content_type = CONTENT_TYPE_JSON
104
+ request.body = JSON.generate(params) unless params.empty?
105
+ else
106
+ request.content_type = CONTENT_TYPE_FORM
107
+ request.body = URI.encode_www_form(params) unless params.empty?
108
+ end
109
+
110
+ parse fetch(request)
111
+ end
112
+
113
+ # PUT
114
+ def put(path, params = {}, as: :json)
115
+ request = Net::HTTP::Put.new(path)
116
+
117
+ case as
118
+ when :json
119
+ request.content_type = CONTENT_TYPE_JSON
120
+ request.body = JSON.generate(params) unless params.empty?
121
+ else
122
+ request.content_type = CONTENT_TYPE_FORM
123
+ request.body = URI.encode_www_form(params) unless params.empty?
124
+ end
125
+
126
+ parse fetch(request)
127
+ end
128
+
129
+ # POST multipart
130
+ def multipart(path, params)
131
+ request = Net::HTTP::Post.new(path)
132
+
133
+ request.content_type = CONTENT_TYPE_MULTIPART
134
+ request.body = Rack::Multipart::Generator.new(
135
+ 'file' => Rack::Multipart::UploadedFile.new(params['file'][:tempfile].path, params['file'][:type])
136
+ ).dump
137
+
138
+ parse fetch(request)
139
+ end
140
+
141
+ private
142
+
143
+ # Perform the request.
144
+ def fetch(request)
145
+ # Shore up default headers for the request.
146
+ request['Accept'] = CONTENT_TYPE_JSON
147
+ request['Connection'] = 'keep-alive'
148
+ request['User-Agent'] = "Badgify Ruby v#{Badgify::VERSION}"
149
+ request["Authorization"] = "Token token=\"#{Badgify.token}\""
150
+
151
+ # Actually make the request.
152
+ response = @http.request(request)
153
+
154
+ # Net::HTTPResponse.value will raise an error for non-200 responses.
155
+ # Simpler than trying to detect every possible exception.
156
+ response.value || response
157
+ end
158
+
159
+ def parse(response)
160
+ # Parse the response as JSON if possible.
161
+ if response.content_type == CONTENT_TYPE_JSON
162
+ JSON.parse(response.body)
163
+
164
+ # Otherwise just return the response body.
165
+ else
166
+ response.body
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,3 @@
1
+ module Badgify
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: badgify-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Olivier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-26 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.3.0
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '1.3'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 1.3.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: vcr
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '4.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '4.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: webmock
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.4'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.4'
103
+ description:
104
+ email:
105
+ - olivier@yafoy.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - CHANGELOG.md
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - badgify-ruby.gemspec
118
+ - bin/console
119
+ - bin/setup
120
+ - lib/badgify.rb
121
+ - lib/badgify/api_operation.rb
122
+ - lib/badgify/category.rb
123
+ - lib/badgify/errors/api_connection_error.rb
124
+ - lib/badgify/errors/api_error.rb
125
+ - lib/badgify/errors/authentication_error.rb
126
+ - lib/badgify/errors/invalid_request_error.rb
127
+ - lib/badgify/errors/service_error.rb
128
+ - lib/badgify/http_decorator.rb
129
+ - lib/badgify/version.rb
130
+ homepage: https://github.com/olimart/badgify_ruby
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.7.6
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: A Ruby gem for interacting with Badgify's REST API
154
+ test_files: []