wor-requests 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ab5a51f2877974f836e6c6275f03a461c3b4f707
4
- data.tar.gz: b06bac7b122ee50d17a3232f99b250000850320f
2
+ SHA256:
3
+ metadata.gz: f9b90d0e2c2d7625668dd56b8b5ca6fd2bb1be90a95f64dab63351ae68f34f39
4
+ data.tar.gz: 18ad91e482c4308f8867a41b87422f3de9711996a385121fbe13a5c300404f50
5
5
  SHA512:
6
- metadata.gz: 2c1ea9a833e798677bc1314f5131408fe24a599fdfb353d34c53bf77366ddada243e74ec54e76fb55129fbebc404064b185ad542b81c663d216321c19b2a4c16
7
- data.tar.gz: 9e0f623dec18850fdb63fafaa732930f0dae07b3ccdec2d9fe81d6097b21468f41867e6b4778bb613ee43b213f073f06457b1b118dee17a0ec7369c3cd1f9944
6
+ metadata.gz: c91be95c5797371e4f9c96b0f0d18a935df24cafacf81e14a4c039daca2204cdca4f3525d216c9dd90fa2c2f60e1c4a63d66165505ff744410d39cc58fad4a9e
7
+ data.tar.gz: ca4426836c9832f24ead8f3091c5cb073a5de770e89cd431064ff108bc889da3fc347ee8ef542e0b770b4f33998acb2163e044a1c2402260fd9cfaa2175d1818
@@ -1,14 +1,15 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.2
4
- - 2.3.3
5
- - 2.4.0
3
+ - 2.3.8
4
+ - 2.4.6
5
+ - 2.5.5
6
+ - 2.6.3
6
7
 
7
- install:
8
+ before_install:
9
+ - gem update --system
8
10
  - gem install bundler
9
- - bundle install --retry=3
10
11
 
11
12
  script:
13
+ - bundle exec rubocop lib spec -R --format simple
12
14
  - bundle exec rspec
13
- - bundle exec rubocop -R --format simple
14
15
  - bundle exec codeclimate-test-reporter
@@ -1,6 +1,14 @@
1
1
 
2
2
  ## Change log
3
3
 
4
+ ### V0.1.2
5
+ * [#42](https://github.com/Wolox/wor-requests/pull/42): Service generator - [@blacksam07](https://github.com/blacksam07).
6
+ * [#41](https://github.com/Wolox/wor-requests/pull/41): Add validation when base_url method is empty - [@blacksam07](https://github.com/blacksam07).
7
+ * [#39](https://github.com/Wolox/wor-requests/pull/39): Handling the case when response body is empty - [@mnmallea](https://github.com/mnmallea).
8
+
9
+ ### V0.1.1
10
+ * [#35](https://github.com/Wolox/wor-requests/pull/35): updated version - [@hdf1986](https://github.com/hdf1986).
11
+
4
12
  ### [0.1.0]
5
13
 
6
14
  #### Added
data/README.md CHANGED
@@ -1,9 +1,7 @@
1
1
  # Wolox on Rails - Requests
2
2
  [![Gem Version](https://badge.fury.io/rb/wor-requests.svg)](https://badge.fury.io/rb/wor-requests)
3
- [![Dependency Status](https://gemnasium.com/badges/github.com/Wolox/wor-requests.svg)](https://gemnasium.com/github.com/Wolox/wor-requests)
4
3
  [![Build Status](https://travis-ci.org/Wolox/wor-requests.svg)](https://travis-ci.org/Wolox/wor-requests)
5
4
  [![Code Climate](https://codeclimate.com/github/Wolox/wor-requests/badges/gpa.svg)](https://codeclimate.com/github/Wolox/wor-requests)
6
- [![Test Coverage](https://codeclimate.com/github/Wolox/wor-requests/badges/coverage.svg)](https://codeclimate.com/github/Wolox/wor-requests/coverage)
7
5
 
8
6
  Make external requests in you service objects, with easy logging and error handling!
9
7
 
@@ -33,8 +31,29 @@ Wor::Requests.configure do |config|
33
31
  end
34
32
  ```
35
33
 
36
- ## Usage
34
+ ## Generators
35
+ ```ruby
36
+ rails generate wor:requests:service NAME
37
+ ```
38
+
39
+ ### Generator options
40
+ #### module
41
+ Specifying the module name as:
42
+
43
+ ```ruby
44
+ rails generate wor:requests:service NAME --module MODULE_NAME
45
+ ```
46
+ We can create a service with an inner class called NAME, and external module called MODULE_NAME
37
47
 
48
+ ```ruby
49
+ module ModuleName
50
+ class NameService < Wor::Requests::Base
51
+ # Your code here
52
+ end
53
+ end
54
+ ```
55
+
56
+ ## Service example
38
57
  To write your first Service using Wor-requests you can write something like this:
39
58
 
40
59
  ```ruby
@@ -57,12 +76,9 @@ class GithubService < Wor::Requests::Base
57
76
  end
58
77
  end
59
78
 
60
- puts GithubService.new.repositories('alebian')
79
+ puts GithubService.new.repositories('wolox')
61
80
  ```
62
81
 
63
- Or, even easier, run `rails generate wor:requests:service NAME` in your Rails root
64
-
65
-
66
82
  If you need to send body parameters in a post request you can write something like this:
67
83
 
68
84
  ```ruby
@@ -104,8 +120,12 @@ ExternalService.new.post_request_example(token)
104
120
  7. Create a new Pull Request
105
121
 
106
122
  ## About ##
123
+ This project was developed by [Diego Raffo](https://github.com/enanodr) along with [Michel Agopian](https://github.com/mishuagopian) and it was written by [Wolox](http://www.wolox.com.ar).
124
+
125
+ Maintainers: [Samir Tapiero](https://github.com/blacksam07)
126
+
127
+ Contributors: [Diego Raffo](https://github.com/enanodr) [Michel Agopian](https://github.com/mishuagopian)
107
128
 
108
- This project is maintained by [Diego Raffo](https://github.com/enanodr) along with [Michel Agopian](https://github.com/mishuagopian) and it was written by [Wolox](http://www.wolox.com.ar).
109
129
  ![Wolox](https://raw.githubusercontent.com/Wolox/press-kit/master/logos/logo_banner.png)
110
130
 
111
131
  ## License
@@ -1,3 +1,24 @@
1
+ <% if @module_name.present? %>
2
+ module <%= @module_name.camelize %>
3
+ class <%= class_name %>Service < Wor::Requests::Base
4
+ # An example of how to get all repositories for the specified user in github.com
5
+ # def repositories(username)
6
+ # get(
7
+ # attempting_to: "get repositories of #{username}",
8
+ # path: "/users/#{username}/repos"
9
+ # )
10
+ # rescue Wor::Requests::RequestError => e
11
+ # puts e.message
12
+ # end
13
+
14
+ # protected
15
+
16
+ # def base_url
17
+ # 'https://api.github.com'
18
+ # end
19
+ end
20
+ end
21
+ <% else %>
1
22
  class <%= class_name %>Service < Wor::Requests::Base
2
23
  # An example of how to get all repositories for the specified user in github.com
3
24
  # def repositories(username)
@@ -15,3 +36,4 @@ class <%= class_name %>Service < Wor::Requests::Base
15
36
  # 'https://api.github.com'
16
37
  # end
17
38
  end
39
+ <% end %>
@@ -2,11 +2,22 @@ module Wor
2
2
  module Requests
3
3
  module Generators
4
4
  class ServiceGenerator < Rails::Generators::NamedBase
5
+ SERVICE_DIR_PATH = 'app/services'.freeze
5
6
  source_root File.expand_path('../../../templates', __FILE__)
6
7
  desc 'Creates Wor-requests service for your application'
7
8
 
9
+ class_option :module, type: :string
10
+
8
11
  def create_service
9
- template 'base_service.rb.erb', "app/services/#{plural_name}_service.rb"
12
+ @module_name = options[:module]
13
+
14
+ generator_dir_path = "#{SERVICE_DIR_PATH}
15
+ #{("/#{@module_name.underscore}" if @module_name.present?)}"
16
+ generator_path = "#{generator_dir_path}/#{file_name}_service.rb"
17
+
18
+ FileUtils.mkdir_p(generator_dir_path) unless File.exist?(generator_dir_path)
19
+
20
+ template 'base_service.rb.erb', generator_path
10
21
  end
11
22
  end
12
23
  end
@@ -1,5 +1,6 @@
1
1
  require_relative 'requests/base'
2
2
  require_relative 'requests/request_error'
3
+ require_relative 'requests/malformed_base_url'
3
4
  require_relative 'requests/version'
4
5
  require 'logger'
5
6
 
@@ -22,6 +23,7 @@ module Wor
22
23
 
23
24
  def self.default_response_type=(type)
24
25
  return unless VALID_RESPONSE_TYPES.include?(type)
26
+
25
27
  @config[:default_response_type] = type
26
28
  end
27
29
 
@@ -45,6 +45,7 @@ module Wor
45
45
  resp = HTTParty.send(options[:method], uri(options[:path]), request_parameters(options))
46
46
 
47
47
  return after_success(resp, options, &block) if resp.success?
48
+
48
49
  after_error(resp, options)
49
50
  end
50
51
 
@@ -72,6 +73,7 @@ module Wor
72
73
  log_success(options[:attempting_to])
73
74
 
74
75
  return yield(response) if block_given?
76
+
75
77
  handle_response(response, options[:response_type])
76
78
  end
77
79
 
@@ -85,35 +87,39 @@ module Wor
85
87
  end
86
88
 
87
89
  def formatted_base_url
90
+ raise MalformedBaseUrl if base_url.nil?
91
+
88
92
  base_url[-1] != '/' ? "#{base_url}/" : base_url
89
93
  end
90
94
 
91
95
  def formatted_path(path)
92
96
  return '' if path.nil?
93
97
  return path if path[0] != '/'
98
+
94
99
  path.slice!(0)
95
100
  path
96
101
  end
97
102
 
98
103
  def log_success(attempt)
99
104
  return unless present?(attempt)
105
+
100
106
  logger.info "SUCCESS: #{attempt}"
101
107
  end
102
108
 
103
109
  def log_attempt(attempt)
104
110
  return unless present?(attempt)
111
+
105
112
  logger.info "ATTEMPTING TO: #{attempt}"
106
113
  end
107
114
 
108
115
  def log_error(response, attempting_to)
109
116
  return unless present?(attempting_to)
117
+
110
118
  response_error = "ERROR when trying to #{attempting_to}. "
111
119
  response_error << "Got status code: #{response.code}. "
112
- if present?(response.body)
113
- response_error << "Response error: #{JSON.parse(response.body)}"
114
- end
120
+ response_error << "Response error: #{JSON.parse(response.body)}" if present?(response.body)
115
121
  logger.error response_error
116
- rescue => e
122
+ rescue JSON::ParserError => e
117
123
  logger.error("#{response_error} ERROR while parsing response body: #{e.message}.")
118
124
  end
119
125
 
@@ -139,18 +145,20 @@ module Wor
139
145
 
140
146
  def validate_method!(method)
141
147
  return true if VALID_HTTP_VERBS.include?(method)
148
+
142
149
  raise ArgumentError, "#{method} is not a valid method."
143
150
  end
144
151
 
145
152
  def response_type(type)
146
153
  return type if Wor::Requests::VALID_RESPONSE_TYPES.include?(type)
154
+
147
155
  default_response_type
148
156
  end
149
157
 
150
158
  def handle_response(response, response_type)
151
159
  case response_type(response_type)
152
160
  when :json
153
- JSON.parse(response.body)
161
+ JSON.parse(response.body) if response.body.present?
154
162
  else
155
163
  response.body
156
164
  end
@@ -0,0 +1,9 @@
1
+ module Wor
2
+ module Requests
3
+ class MalformedBaseUrl < StandardError
4
+ def initialize(msg = 'The method base_url is malformed')
5
+ super(msg)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Wor
2
2
  module Requests
3
- VERSION = '0.1.1'.freeze
3
+ VERSION = '0.1.2'.freeze
4
4
  end
5
5
  end
@@ -22,13 +22,13 @@ Gem::Specification.new do |spec|
22
22
  spec.require_paths = ['lib']
23
23
 
24
24
  spec.add_dependency 'httparty', '~> 0.13'
25
- spec.add_dependency 'railties', '>= 4.1.0', '< 5.2'
25
+ spec.add_dependency 'railties', '>= 4.1.0', '<= 6.0.0'
26
26
 
27
27
  spec.add_development_dependency 'faker'
28
28
  spec.add_development_dependency 'webmock'
29
29
  spec.add_development_dependency 'byebug', '~> 9.0'
30
30
  spec.add_development_dependency 'rubocop', '~> 0.47.0'
31
- spec.add_development_dependency 'bundler', '~> 1.13'
31
+ spec.add_development_dependency 'bundler', '~> 2.0'
32
32
  spec.add_development_dependency 'rake', '~> 10.0'
33
33
  spec.add_development_dependency 'rspec', '~> 3.0'
34
34
  spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wor-requests
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - draffo
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-11-16 00:00:00.000000000 Z
12
+ date: 2019-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -32,9 +32,9 @@ dependencies:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: 4.1.0
35
- - - "<"
35
+ - - "<="
36
36
  - !ruby/object:Gem::Version
37
- version: '5.2'
37
+ version: 6.0.0
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,9 +42,9 @@ dependencies:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
44
  version: 4.1.0
45
- - - "<"
45
+ - - "<="
46
46
  - !ruby/object:Gem::Version
47
- version: '5.2'
47
+ version: 6.0.0
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: faker
50
50
  requirement: !ruby/object:Gem::Requirement
@@ -107,14 +107,14 @@ dependencies:
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '1.13'
110
+ version: '2.0'
111
111
  type: :development
112
112
  prerelease: false
113
113
  version_requirements: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '1.13'
117
+ version: '2.0'
118
118
  - !ruby/object:Gem::Dependency
119
119
  name: rake
120
120
  requirement: !ruby/object:Gem::Requirement
@@ -210,6 +210,7 @@ files:
210
210
  - lib/wor/requests.rb
211
211
  - lib/wor/requests/base.rb
212
212
  - lib/wor/requests/invalid_options_error.rb
213
+ - lib/wor/requests/malformed_base_url.rb
213
214
  - lib/wor/requests/request_error.rb
214
215
  - lib/wor/requests/version.rb
215
216
  - wor-requests.gemspec
@@ -232,8 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
232
233
  - !ruby/object:Gem::Version
233
234
  version: '0'
234
235
  requirements: []
235
- rubyforge_project:
236
- rubygems_version: 2.6.11
236
+ rubygems_version: 3.0.1
237
237
  signing_key:
238
238
  specification_version: 4
239
239
  summary: Make external requests in you service objects, with easy logging and error