Module: Pebblebed::Http

Defined in:
lib/pebblebed/http.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

DEFAULT_REQUEST_TIMEOUT =
30
DEFAULT_CONNECT_TIMEOUT =
30
DEFAULT_READ_TIMEOUT =
30
DEFAULT_WRITE_TIMEOUT =
60

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.connect_timeoutObject

Returns the value of attribute connect_timeout.



57
58
59
# File 'lib/pebblebed/http.rb', line 57

def connect_timeout
  @connect_timeout
end

.read_timeoutObject

Returns the value of attribute read_timeout.



57
58
59
# File 'lib/pebblebed/http.rb', line 57

def read_timeout
  @read_timeout
end

.write_timeoutObject

Returns the value of attribute write_timeout.



57
58
59
# File 'lib/pebblebed/http.rb', line 57

def write_timeout
  @write_timeout
end

Class Method Details

.delete(url, params, &block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/pebblebed/http.rb', line 130

def self.delete(url, params, &block)
  url, params, query = url_and_params_from_args(url, params, &block)
  content_type, body = serialize_params(params)
  return do_request(url) { |connection|
    connection.delete(
      :host => url.host,
      :path => url.path,
      :headers => {
        'Accept' => 'application/json',
        'Content-Type' => content_type
      },
      :body => body,
      :query => query,
      :persistent => true
    )
  }
end

.get(url = nil, params = nil, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pebblebed/http.rb', line 82

def self.get(url = nil, params = nil, &block)
  url, params, query = url_and_params_from_args(url, params, &block)
  return do_request(url) { |connection|
    connection.get(
      :host => url.host,
      :path => url.path,
      :query => QueryParams.encode((params || {}).merge(query)),
      :persistent => true
    )
  }
end

.post(url, params, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pebblebed/http.rb', line 94

def self.post(url, params, &block)
  url, params, query = url_and_params_from_args(url, params, &block)
  content_type, body = serialize_params(params)
  return do_request(url) { |connection|
    connection.post(
      :host => url.host,
      :path => url.path,
      :headers => {
        'Accept' => 'application/json',
        'Content-Type' => content_type
      },
      :body => body,
      :query => query,
      :persistent => true
    )
  }
end

.put(url, params, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/pebblebed/http.rb', line 112

def self.put(url, params, &block)
  url, params, query = url_and_params_from_args(url, params, &block)
  content_type, body = serialize_params(params)
  return do_request(url) { |connection|
    connection.put(
      :host => url.host,
      :path => url.path,
      :headers => {
        'Accept' => 'application/json',
        'Content-Type' => content_type
      },
      :body => body,
      :query => query,
      :persistent => true
    )
  }
end

.stream_get(url = nil, params = nil, headers: {}, on_data:) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/pebblebed/http.rb', line 155

def self.stream_get(url = nil, params = nil, headers: {}, on_data:)
  url, params, query = url_and_params_from_args(url, params)
  return do_request(url, share: false) { |connection|
    connection.get(
      :host => url.host,
      :path => url.path,
      :headers => headers,
      :query => QueryParams.encode((params || {}).merge(query)),
      :persistent => false,
      :response_block => streamer(on_data)
    )
  }
end

.stream_post(url, params, headers: {}, on_data:) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/pebblebed/http.rb', line 169

def self.stream_post(url, params, headers: {}, on_data:)
  url, params, query = url_and_params_from_args(url, params)
  content_type, body = serialize_params(params)
  return do_request(url, share: false) { |connection|
    connection.post(
      :host => url.host,
      :path => url.path,
      :headers => {
        'Accept' => 'application/json',
        'Content-Type' => content_type
      }.merge(headers),
      :body => body,
      :persistent => false,
      :query => query,
      :response_block => streamer(on_data)
    )
  }
end

.stream_put(url, params, on_data:) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/pebblebed/http.rb', line 188

def self.stream_put(url, params, on_data:)
  url, params, query = url_and_params_from_args(url, params)
  content_type, body = serialize_params(params)
  return do_request(url, share: false) { |connection|
    connection.put(
      :host => url.host,
      :path => url.path,
      :headers => {
        'Accept' => 'application/json',
        'Content-Type' => content_type
      }.merge(headers),
      :body => body,
      :query => query,
      :persistent => false,
      :response_block => streamer(on_data)
    )
  }
end

.streamer(on_data) ⇒ Object



148
149
150
151
152
153
# File 'lib/pebblebed/http.rb', line 148

def self.streamer(on_data)
  lambda do |chunk, remaining_bytes, total_bytes|
    on_data.call(chunk)
    total_bytes
  end
end