sinew 3.0.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sinew/request.rb DELETED
@@ -1,86 +0,0 @@
1
- require 'sterile'
2
-
3
- #
4
- # Process a single HTTP request.
5
- #
6
-
7
- module Sinew
8
- class Error < StandardError; end
9
-
10
- class Request
11
- VALID_METHODS = %w[get post patch put delete head options].freeze
12
- METHODS_WITH_BODY = %w[patch post put].freeze
13
-
14
- attr_reader :method, :options, :uri
15
-
16
- # Supported options:
17
- # body: Body of http post
18
- # headers: Hash of HTTP headers (combined with runtime_options.headers)
19
- # query: Hash of query parameters to add to url
20
- def initialize(method, url, options = {})
21
- @method = method
22
- @options = options.dup
23
- @uri = parse_url(url)
24
- end
25
-
26
- # run the request, return the result
27
- def perform(connection)
28
- validate!
29
-
30
- body = options.delete(:body)
31
- fday_response = connection.send(method, uri, body) do
32
- _1.headers.update(options[:headers]) if options[:headers]
33
- _1.options[:proxy] = options[:proxy]
34
- end
35
-
36
- Response.from_network(self, fday_response)
37
- end
38
-
39
- # We accept sloppy urls and attempt to clean them up
40
- def parse_url(url)
41
- s = url.to_s
42
-
43
- # remove entities
44
- s = Sterile.decode_entities(s)
45
-
46
- # fix a couple of common encoding bugs
47
- s = s.gsub(' ', '%20')
48
- s = s.gsub("'", '%27')
49
-
50
- # append query manually (instead of letting Faraday handle it) for consistent
51
- # Request#uri and Response#uri
52
- query = options.delete(:query)
53
- if query.present?
54
- q = Faraday::Utils.default_params_encoder.encode(query)
55
- separator = s.include?('?') ? '&' : '?'
56
- s = "#{s}#{separator}#{q}"
57
- end
58
-
59
- URI.parse(s)
60
- end
61
- protected :parse_url
62
-
63
- def validate!
64
- raise "invalid method #{method}" if !VALID_METHODS.include?(method)
65
- raise "invalid url #{uri}" if uri.scheme !~ /^http/
66
- raise "can't #{method} with a body" if body && !METHODS_WITH_BODY.include?(method)
67
- raise "Content-Type doesn't make sense without a body" if content_type && !body
68
- end
69
- protected :validate!
70
-
71
- def body
72
- options[:body]
73
- end
74
- protected :body
75
-
76
- def headers
77
- options[:headers]
78
- end
79
- protected :headers
80
-
81
- def content_type
82
- headers && headers['Content-Type']
83
- end
84
- protected :content_type
85
- end
86
- end
@@ -1,28 +0,0 @@
1
- #
2
- # Runtime options that sinew files can modify.
3
- #
4
-
5
- module Sinew
6
- class RuntimeOptions
7
- attr_accessor :retries
8
- attr_accessor :rate_limit
9
- attr_accessor :headers
10
- attr_accessor :httpdisk_options
11
- attr_accessor :insecure
12
-
13
- def initialize
14
- self.retries = 3
15
- self.rate_limit = 1
16
- self.headers = {
17
- 'User-Agent' => "sinew/#{VERSION}",
18
- }
19
- self.httpdisk_options = {}
20
- self.insecure = false
21
-
22
- # for testing
23
- if ENV['SINEW_TEST']
24
- self.rate_limit = 0
25
- end
26
- end
27
- end
28
- end