Module: KnowsAboutApiRequests

Defined in:
lib/cucumber/blinkbox/requests.rb

Constant Summary collapse

CONTENT_TYPE =
"application/vnd.blinkboxbooks.data.v1+json"

Instance Method Summary collapse

Instance Method Details

#http_clientObject


4
5
6
7
8
9
10
11
# File 'lib/cucumber/blinkbox/requests.rb', line 4

def http_client
  @http ||= HTTPClient.new(TEST_CONFIG["proxy"])
  # Ensure we're using the system SSL certs, as per other libraries (like HTTParty)
  @http.ssl_config.set_trust_ca(OpenSSL::X509::DEFAULT_CERT_FILE)
  @http.debug_dev = STDOUT if TEST_CONFIG["debug"]
  #@http.reset_all
  @http
end

#http_delete(server, path, header = {}) ⇒ Object


72
73
74
75
76
# File 'lib/cucumber/blinkbox/requests.rb', line 72

def http_delete(server, path, header = {})
  uri = qualified_uri(server, path)
  params = query if query.count > 0
  @response = http_client.delete(uri, query: params, header: request_headers(header))
end

#http_get(server, path, header = {}) ⇒ Object

request methods


56
57
58
59
60
# File 'lib/cucumber/blinkbox/requests.rb', line 56

def http_get(server, path, header = {})
  uri = qualified_uri(server, path)
  params = query if query.count > 0
  @response = http_client.get(uri, query: params, header: request_headers(header), follow_redirect: true)
end

#http_post(server, path, body = {}, header = {}) ⇒ Object


62
63
64
65
# File 'lib/cucumber/blinkbox/requests.rb', line 62

def http_post(server, path, body = {}, header = {})
  uri = qualified_uri(server, path)
  @response = http_client.post(uri, body: format_body(body), header: request_headers({"Content-Type" => CONTENT_TYPE}.merge(header)))
end

#http_put(server, path, body = {}, header = {}) ⇒ Object


67
68
69
70
# File 'lib/cucumber/blinkbox/requests.rb', line 67

def http_put(server, path, body = {}, header = {})
  uri = qualified_uri(server, path)
  @response = http_client.put(uri, body: format_body(body), header: request_headers({"Content-Type" => CONTENT_TYPE}.merge(header)))
end

#qualified_uri(server, path) ⇒ Object


48
49
50
51
52
# File 'lib/cucumber/blinkbox/requests.rb', line 48

def qualified_uri(server, path)
  uri = test_env.servers[server.to_sym]
  raise "Test Error: #{server} doesn't appear to be defined in the environments.yml" if uri.nil?
  File.join(uri.to_s, path)
end

#queryObject


13
14
15
# File 'lib/cucumber/blinkbox/requests.rb', line 13

def query
  @query ||= {}
end

#request_headers(header = {}) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cucumber/blinkbox/requests.rb', line 17

def request_headers(header = {})
  @request_headers ||= {
      "Accept" => CONTENT_TYPE
  }
  if @access_token
    @request_headers["Authorization"] = "Bearer #{@access_token}" 
  else
    @request_headers.delete("Authorization")
  end
  @request_headers.merge!(header)
  @request_headers
end

#set_query_param(name, value) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cucumber/blinkbox/requests.rb', line 30

def set_query_param(name, value)
  if value.respond_to?(:each)
    value.each { |v| set_query_param(name, v) }
  else
    name = name.camel_case
    value = is_enum_param(name) ? value.snake_case(:upper) : value
    current_value = query[name]

    if current_value.nil?
      query[name] = value
    elsif current_value.is_a?(Array)
      query[name] << value
    else
      query[name] = [current_value, value]
    end
  end
end