Module: Cloudxls::BaseRequest
- Included in:
- ReadRequest, WriteRequest
- Defined in:
- lib/cloudxls.rb
Instance Method Summary collapse
- #api_base ⇒ Object
- #api_key ⇒ Object
- #build_request ⇒ Object
-
#each(&block) ⇒ Object
Starts request and yields response to block.
- #initialize(client_options = nil) ⇒ Object
- #path_to(path) ⇒ Object
-
#start(&block) ⇒ Object
Internal.
-
#test_key? ⇒ Boolean
Is Api Key for testing/sandbox only?.
- #user_agent ⇒ Object
Instance Method Details
#api_base ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/cloudxls.rb', line 85 def api_base if test_key? Cloudxls.sandbox_base else [:api_base] end end |
#api_key ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/cloudxls.rb', line 69 def api_key key = [:api_key] if key.nil? raise "api_key is nil. Configure using CLOUDXLS_API_KEY ENV variable or Cloudxls.api_key = ..." end key end |
#build_request ⇒ Object
134 135 136 137 138 139 |
# File 'lib/cloudxls.rb', line 134 def build_request request = Net::HTTP::Post::Multipart.new(self.path, @post_data) request.basic_auth api_key, "" request['User-Agent'] = user_agent request end |
#each(&block) ⇒ Object
Starts request and yields response to block
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/cloudxls.rb', line 111 def each(&block) raise "#{self.class.name} already executed" if @finished start do |http| request = build_request if block_given? http.request(request) do |response| if Net::HTTPSuccess === response response.read_body(&block) else raise ApiError.new("#{response.code} #{response.class.name.to_s}: #{response.body}") end end else http.request(request) end end @finished = true self end |
#initialize(client_options = nil) ⇒ Object
63 64 65 66 67 |
# File 'lib/cloudxls.rb', line 63 def initialize( = nil) @post_data = [] @finished = false @client_options = || Cloudxls. end |
#path_to(path) ⇒ Object
99 100 101 |
# File 'lib/cloudxls.rb', line 99 def path_to(path) "/#{[:api_version]}/#{path}" end |
#start(&block) ⇒ Object
Internal. Starts the request.
95 96 97 |
# File 'lib/cloudxls.rb', line 95 def start(&block) Net::HTTP.start(api_base, [:port], use_ssl: true, &block) end |
#test_key? ⇒ Boolean
Is Api Key for testing/sandbox only?
81 82 83 |
# File 'lib/cloudxls.rb', line 81 def test_key? api_key.to_s.downcase.start_with?("test") end |
#user_agent ⇒ Object
103 104 105 |
# File 'lib/cloudxls.rb', line 103 def user_agent "cloudxls-ruby #{VERSION}" end |