simple_response 0.5.0 → 0.7.0

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00a4c66d5cb1aa579d9a927d8d721d4068131623
4
- data.tar.gz: 4c44f3771dde17e78e78386e2210bf4d806820ed
3
+ metadata.gz: b8b127d41f2b2ef515fbdbb87348d006ad2212e6
4
+ data.tar.gz: 16650b8be8ab004edd6fe2ef7540efc54906a305
5
5
  SHA512:
6
- metadata.gz: 5dc3e1810ad210658789b3b3b22815f6bd4ef84940093e2472d919b99844c893db0c51ccd796337b2e114f98c7ca7ead82a97e82bbc8a216a835e03e98fce5e9
7
- data.tar.gz: e815e7190453944f818ef57a81c9b52974770f53cb2271968134beb93e9e38140a5fad41b2193576ebf5b20131cc27af8ec707dbd145ef3e8c32453bbc3bb3a4
6
+ metadata.gz: 26ffe67bb2833619847313145607b426f8075660fe90fd85882c4241e77074ec7489d8da527055787e590283afd8b35ae58278edc43eae2a3cbf342494b48ba2
7
+ data.tar.gz: cf84ffd0d61d245c527230f89935c13f1b09b3e02e8678ffb1eec1a86f840677c9b2b2a4b2895cdbd8f7a05289a1e73914a28d1c1057819430e4266e98758286
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_response (0.4.0)
4
+ simple_response (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,10 +1,46 @@
1
1
  [![Maintainability](https://api.codeclimate.com/v1/badges/54e7c14cd8f10bd26aa5/maintainability)](https://codeclimate.com/github/giovannibenussi/simple-response/maintainability)
2
2
  [![Test Coverage](https://api.codeclimate.com/v1/badges/54e7c14cd8f10bd26aa5/test_coverage)](https://codeclimate.com/github/giovannibenussi/simple-response/test_coverage)
3
+ <a target="_blank" href="https://badge.fury.io/rb/simple_response"><img src="https://badge.fury.io/rb/simple_response.svg" alt="Gem Version" height="18"></a>
4
+
3
5
  # SimpleResponse
4
6
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/simple_response`. To experiment with that code, run `bin/console` for an interactive prompt.
7
+ SimpleResponse is a lightweight gem that will allow you to create Response Objects in an easy but robust way.
8
+
9
+ Basically, a Response Object is an object that improves the communication between the functions of your application. By using them, you will be able to reduce the number of bugs and its hunt easier than alternative ways like return boolean or string values.
10
+
11
+ <!-- In a Response Object you have two types of responses: success and failures. Also, you could associate any data that you want with the Response Object in case you need to provide extra data. -->
12
+ In a Response Object you can have two types of responses (success and failure) and any custom associated data that you want.
13
+
14
+ Following is a simple code that uses SimpleResponse to improve both its readability and flow control.
15
+
16
+ ```ruby
17
+ response = ThirdPartyAPI.get('posts')
18
+ if response.success?
19
+ puts "Post retrieval succeeds in #{response.retrieval_time}"
20
+ puts "We retrieve #{response.posts.count} posts."
21
+ else
22
+ puts "Error retrieving posts: #{response.error_message}"
23
+ end
24
+ ```
25
+
26
+ To implement this, you could write something like this using SimpleResponse gem:
27
+
28
+ ```ruby
29
+ class ThirdPartyAPI
30
+ def get('posts')
31
+ # call third party api...
32
+ SimpleResponse.success(posts: posts, retrieval_time: retrieval_time)
33
+ rescue ThirdPartyAPI::CredentialsError => e
34
+ SimpleResponse.failure(error_message: 'invalid credentials')
35
+ end
36
+ end
37
+ ```
38
+
39
+ ## Why?
40
+
41
+ By using Response Objects you will be able to reduce the number and difficulty of your bugs because you will be easing the communication between your application layers that is where bugs used to come from.
6
42
 
7
- TODO: Delete this and the text above, and describe your gem
43
+ SimpleResponse's proposal is a lightweight <a href="http://rubyonrails.org/doctrine/#beautiful-code" target="_blank">syntactic sugar for your response objects</a>.
8
44
 
9
45
  ## Installation
10
46
 
@@ -24,7 +60,7 @@ Or install it yourself as:
24
60
 
25
61
  ## Usage
26
62
 
27
- Example of how to create a success response object:
63
+ Create a success response object:
28
64
 
29
65
  ```ruby
30
66
  response = SimpleResponse.success(user_id: 1)
@@ -34,20 +70,41 @@ response.user_id # 1
34
70
  response.failure? # false
35
71
  ```
36
72
 
37
- ## Development
73
+ Create a failure response object:
38
74
 
39
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
75
+ ```ruby
76
+ response = SimpleResponse.failure(user_id: 1)
77
+
78
+ response.success? # false
79
+ response.user_id # 1
80
+ response.failure? # true
81
+ ```
82
+
83
+ You also have automatically query methods on boolean attributes:
84
+
85
+ ```
86
+ response.works = true
87
+ response.works # true
88
+ response.works? # true
89
+ ```
90
+
91
+ To define dynamically the response status you could call directly the method new on SimpleResponse's class:
40
92
 
41
- 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).
93
+ ```ruby
94
+ SimpleResponse.new(success: true)
95
+
96
+ # with custom data:
97
+ SimpleResponse.new(success: true, user_id: 1)
98
+ ```
42
99
 
43
100
  ## Contributing
44
101
 
45
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/simple_response. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/giovannibenussi/simple_response. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
46
103
 
47
104
  ## License
48
105
 
49
106
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
50
107
 
51
- ## Code of Conduct
108
+ <!-- ## Code of Conduct
52
109
 
53
- Everyone interacting in the SimpleResponse project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/simple_response/blob/master/CODE_OF_CONDUCT.md).
110
+ Everyone interacting in the SimpleResponse project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/giovannibenussi/simple_response/blob/master/CODE_OF_CONDUCT.md). -->
@@ -0,0 +1,21 @@
1
+ module SimpleResponse
2
+ module QueryMethods
3
+ def existing_query_method?(name)
4
+ return false unless query_method?(name)
5
+ return false unless respond_to?(name[0...-1])
6
+
7
+ attribute_class = send(name[0...-1]).class
8
+ attribute_class == TrueClass || attribute_class == FalseClass
9
+ end
10
+
11
+ private
12
+
13
+ def query_method?(name)
14
+ name[-1] == '?'
15
+ end
16
+
17
+ def query_method(name)
18
+ send(name[0...-1])
19
+ end
20
+ end
21
+ end
@@ -1,7 +1,9 @@
1
1
  module SimpleResponse
2
2
  require 'ostruct'
3
+ require 'simple_response/query_methods'
4
+ require 'simple_response/simple_struct'
3
5
 
4
- class Response < OpenStruct
6
+ class Response < SimpleStruct
5
7
  def initialize(success:)
6
8
  super
7
9
  self.success = success
@@ -15,22 +17,8 @@ module SimpleResponse
15
17
  !success?
16
18
  end
17
19
 
18
- def method_missing(name, *args, &block)
19
- if existing_query_method?(name)
20
- send(name[0...-1])
21
- else
22
- super
23
- end
24
- end
25
-
26
- def respond_to_missing?(name, include_private = false)
27
- existing_query_method?(name)
28
- end
29
-
30
- private
31
-
32
- def existing_query_method?(name)
33
- name[-1] == '?' && respond_to?(name[0...-1])
20
+ def keys
21
+ to_h.keys
34
22
  end
35
23
  end
36
24
  end
@@ -0,0 +1,17 @@
1
+ module SimpleResponse
2
+ class SimpleStruct < OpenStruct
3
+ include SimpleResponse::QueryMethods
4
+
5
+ def method_missing(name, *args, &block)
6
+ if existing_query_method?(name)
7
+ query_method(name)
8
+ else
9
+ super
10
+ end
11
+ end
12
+
13
+ def respond_to_missing?(name, include_private = false)
14
+ existing_query_method?(name)
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleResponse
2
- VERSION = '0.5.0'.freeze
2
+ VERSION = '0.7.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_response
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giovanni Benussi
@@ -71,7 +71,9 @@ files:
71
71
  - bin/console
72
72
  - bin/setup
73
73
  - lib/simple_response.rb
74
+ - lib/simple_response/query_methods.rb
74
75
  - lib/simple_response/response.rb
76
+ - lib/simple_response/simple_struct.rb
75
77
  - lib/simple_response/version.rb
76
78
  - simple_response.gemspec
77
79
  homepage: https://github.com/giovannibenussi/simple-response