Class: GMO::PG::Dispatcher
- Inherits:
-
Object
- Object
- GMO::PG::Dispatcher
- Extended by:
- Forwardable
- Includes:
- Shorthands
- Defined in:
- lib/gmo-pg/dispatcher.rb,
lib/gmo-pg/dispatcher/shorthands.rb
Defined Under Namespace
Modules: Shorthands
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#raise_on_api_error ⇒ Object
Returns the value of attribute raise_on_api_error.
Instance Method Summary collapse
- #connect ⇒ Object
- #dispatch(request) ⇒ Object
-
#initialize(base_url) {|_self| ... } ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #use_proxy(address: :ENV, port: nil, user: nil, pass: nil) ⇒ Object
Constructor Details
#initialize(base_url) {|_self| ... } ⇒ Dispatcher
Returns a new instance of Dispatcher.
18 19 20 21 22 23 24 |
# File 'lib/gmo-pg/dispatcher.rb', line 18 def initialize(base_url) @base_url = base_url url = URI.parse(@base_url) @http = Net::HTTP.new(url.host, url.port) @http.use_ssl = true if url.scheme == 'https' yield self if block_given? end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/gmo-pg/dispatcher.rb', line 9 def base_url @base_url end |
#raise_on_api_error ⇒ Object
Returns the value of attribute raise_on_api_error.
10 11 12 |
# File 'lib/gmo-pg/dispatcher.rb', line 10 def raise_on_api_error @raise_on_api_error end |
Instance Method Details
#connect ⇒ Object
35 36 37 38 39 |
# File 'lib/gmo-pg/dispatcher.rb', line 35 def connect handle_error do @http.start { yield self if block_given? } end end |
#dispatch(request) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gmo-pg/dispatcher.rb', line 41 def dispatch(request) req = Net::HTTP::Post.new(request.path) req.body = GMO::PG::Payload.encode(request.payload) handle_error do res = @http.request(req) case res when Net::HTTPSuccess payload = GMO::PG::Payload.decode(res.body) response = request.class.corresponding_response_class.new(payload) raise response.errors.first.to_error if @raise_on_api_error && response.error? response else res.value # raise Net::XxxError end end end |
#use_proxy(address: :ENV, port: nil, user: nil, pass: nil) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/gmo-pg/dispatcher.rb', line 26 def use_proxy(address: :ENV, port: nil, user: nil, pass: nil) @http.proxy_from_env = (address == :ENV) @http.proxy_address = address @http.proxy_port = port @http.proxy_user = user @http.proxy_pass = pass self end |