safettp 0.3.1 → 0.3.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
2
  SHA1:
3
- metadata.gz: 0aacb6665f1ef7a0e355843bc2accae2d4994e4c
4
- data.tar.gz: d74f7dfbac3ca4feca94d6ff59848888b12676f2
3
+ metadata.gz: 6618b2cd29258b2e91b22b243155349ba66dea7c
4
+ data.tar.gz: e817b38e0b1f6b85e8667aa6b2778757ecbb9737
5
5
  SHA512:
6
- metadata.gz: 3bd9caea86427d62a6ec42cdcb821ab01f835a2a42f85b29a316e587aa64b8e5a307d8b56976b4d32dea6a457bfb7a2bac894ed4f97561940fce7886fc81c8f1
7
- data.tar.gz: 15644c03034daa143f7864e2c7c63a4921a1abc6f42ac8795a99c2641f0b927badb1ba966eb1e394873f977e29640580a522c41174dc6a8b4b0621268e1ee8bc
6
+ metadata.gz: 0d3a7143f74745f395407e47f86a5898cb842829334b2e28abb5a713fd73a3bd9786a58f85b173fb29bf9b18ecf0421aa9c090736cc02fe1c6cbc47a41254845
7
+ data.tar.gz: 817c3da02a82539ff7770efeb2683e13449de834019ec1845893b36992959496fd5b3d2a10b5ea1e4c3cda2eb25e097c8b291950bbb7c6d6d14a8f043e339c53
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # Safettp
2
+ Make sure HTTP requests never fail.
2
3
 
3
- Safettp is another HTTP library. It encourages your requests to always handle
4
- the failure state, and does so in a straightforward and easy way.
4
+ ## Synopsis
5
+ Safettp is an easy configurable HTTP library that encourages you to always cover what will happen after a request has been made.
5
6
 
6
7
  ## Installation
7
-
8
8
  Add this line to your application's Gemfile:
9
9
 
10
10
  ```ruby
@@ -19,22 +19,33 @@ Or install it yourself as:
19
19
 
20
20
  $ gem install safettp
21
21
 
22
- ## Synopsis
22
+ ## Usage
23
+
24
+ ### A simple example for the cool kids.
23
25
  ```ruby
24
- class MyHttpClient
26
+ class HTTPBinClient
25
27
  include Safettp::Client
26
28
 
29
+ # Setup the default setting for any given request.
27
30
  configure do |config|
31
+ # The base url every request url will be appended to.
28
32
  config.base_url = 'https://httpbin.org'
33
+
34
+ # Additional options that specify the nature of the default request.
35
+ # Find all options available (here)[#options]
29
36
  config.default_options = { headers: { Accept: 'application/json' } }
30
37
  end
31
38
 
32
- def do_post(payload, &block)
33
- post('/post', payload, &block)
39
+ # Define a custom client method. This method will be available both on a new
40
+ # client instance and on the class itself.
41
+ def test_post(payload, &block)
42
+ post('/post', body: payload, &block)
34
43
  end
35
44
  end
36
45
 
37
- MyHttpClient.do_post({ body: 'my_body' }) do |result|
46
+ # Perform a request. The result object must be given a block for both the
47
+ # success state and failure state.
48
+ MyHttpClient.do_post(message: 'Hello world!') do |result|
38
49
  result.on_success do |response|
39
50
  puts response.parsed_body
40
51
  end
@@ -44,9 +55,88 @@ MyHttpClient.do_post({ body: 'my_body' }) do |result|
44
55
  end
45
56
  end
46
57
  ```
58
+ ### The client
59
+ To make use of the available functionality in Safettp, you will have to include the `Safettp::Client` module on a class of yours.
60
+ ```ruby
61
+ class HTTPBinClient
62
+ include Safettp::Client
63
+ end
64
+ ```
47
65
 
48
- ## Development
66
+ Most often your HTTP client will be using the same set of options for every request. You can configure them like so:
67
+ ```ruby
68
+ class HTTPBinClient
69
+ include Safettp::Client
49
70
 
71
+ configure do |config|
72
+ config.base_url = 'https://httpbin.org'
73
+ config.default_options = { headers: { Accept: 'application/json' } }
74
+ end
75
+ end
76
+ ```
77
+
78
+ From this point on your client is fit for fight!
79
+
80
+ ### Performing a request
81
+ A client will be able to perform the 4 common HTTP methods like so:
82
+ ```ruby
83
+ # You can replace `post` with your prefered method
84
+ HTTPBinClient.post('https://httpbin.org/post', options) do |result|
85
+ result.on_success do |response|
86
+ # Your code goes here upon success.
87
+ end
88
+
89
+ result.on_failure do |response|
90
+ # Your code goes here upon failure.
91
+ end
92
+ end
93
+ ```
94
+
95
+ As you can see two separate blocks are yielded whether the request succeeded or failed. In order to use the method you will have to provide both. This will guard you from unexpected errors, neat right?
96
+
97
+ The options hash provided as the second parameter in the example above can be used to append additional information to the request. _You can read about the available options [here](###\ Request\ options)_
98
+
99
+ ### The response object
100
+ To retrieve the data obtained from the request you call `#parsed_body`. It will parse the data from JSON.
101
+ ```ruby
102
+ result.on_success do |response|
103
+ puts response.parsed_body
104
+ end
105
+ ```
106
+
107
+ In case of a failed request, you can find additional information by calling `#http_response`. This will return a `Net::HTTPResponse` object which you can investigate further.
108
+
109
+ ### Request options
110
+ #### Request body
111
+ A body can be set with the `:body` option. It will be parsed to JSON.
112
+ ```ruby
113
+ HTTPBinClient.post('/post', body: { foo: 'bar' })
114
+ # ...
115
+ ```
116
+
117
+ #### Query parameters
118
+ Query parameters can be set with the `:query` option.
119
+ ```ruby
120
+ HTTPBinClient.get('/get', query: { foo: 'bar' })
121
+ ```
122
+
123
+ #### Headers
124
+ Headers can be set with the `:headers` option.
125
+ ```ruby
126
+ HTTPBinClient.get('/get', headers: { Accept: 'application/json' })
127
+ ```
128
+
129
+ #### Authentication
130
+ Authentication can be set with the `:auth` option. As of now Safettp only supports basic authentication.
131
+ ```ruby
132
+ HTTPBinClient.get('/get', auth: {
133
+ type: :basic,
134
+ username: 'username',
135
+ password: 'password'
136
+ })
137
+ ```
138
+
139
+ ## Development
50
140
  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.
51
141
 
52
142
  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).
@@ -21,7 +21,11 @@ module Safettp::Client
21
21
  end
22
22
 
23
23
  module ClassMethods
24
- attr_accessor :config
24
+ attr_writer :config
25
+
26
+ def config
27
+ @config || Safettp::Client::Configuration.new
28
+ end
25
29
 
26
30
  def instance_from_default_options
27
31
  new(config.base_url, config.default_options)
@@ -37,8 +41,7 @@ module Safettp::Client
37
41
  end
38
42
 
39
43
  def configure
40
- self.config ||= Safettp::Client::Configuration.new
41
- yield(config)
44
+ yield(@config)
42
45
  end
43
46
  end
44
47
 
@@ -1,3 +1,3 @@
1
1
  module Safettp
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Safettp::VERSION
9
9
  spec.authors = ["Emric"]
10
10
  spec.email = ["emric.mansson@gmail.com"]
11
-
11
+ spec.homepage = "https://github.com/istanful/safettp"
12
12
  spec.summary = %q{Simple HTTP library with guarded requests.}
13
13
  spec.license = "MIT"
14
14
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safettp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emric
@@ -97,7 +97,7 @@ files:
97
97
  - lib/safettp/response.rb
98
98
  - lib/safettp/version.rb
99
99
  - safettp.gemspec
100
- homepage:
100
+ homepage: https://github.com/istanful/safettp
101
101
  licenses:
102
102
  - MIT
103
103
  metadata: