Class: SeismicAPI::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/seismic_api/request.rb

Overview

Request encapsulates the specifics of making requests to the Seismic API for the common http verbs.

Instance Method Summary collapse

Constructor Details

#initialize(oauth_token:) ⇒ Request

Returns a new instance of Request.


7
8
9
# File 'lib/seismic_api/request.rb', line 7

def initialize(oauth_token:)
  @oauth_token = oauth_token
end

Instance Method Details

#get(uri, params: {}) ⇒ SeismicAPI::Response

Get request

Parameters:

  • uri (String)
  • params (Hash) (defaults to: {})

    query params for this request

Returns:


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/seismic_api/request.rb', line 18

def get(uri, params: {})
  uri = URI.parse(uri)
  uri.query = URI.encode_www_form(params) if params
  req = Net::HTTP::Get.new(uri)
  if @oauth_token
    req['Authorization'] = "Bearer #{@oauth_token}"
  end

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  Response.new(res).raise_on_error
end

#patch(uri, body: nil) ⇒ SeismicAPI::Response

Patch request

Parameters:

  • uri (String)
  • body (#to_json) (defaults to: nil)

    post body

Returns:


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/seismic_api/request.rb', line 61

def patch(uri, body: nil)
  uri = URI.parse(uri)
  req = Net::HTTP::Patch.new(uri)
  req['Authorization'] = "Bearer #{@oauth_token}"
  req.body = body.to_json if body
  req['Content-Type'] = 'application/json'

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  Response.new(res).raise_on_error
end

#post(uri, body: nil) ⇒ SeismicAPI::Response

Post request

Parameters:

  • uri (String)
  • body (#to_json) (defaults to: nil)

    post body

Returns:


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/seismic_api/request.rb', line 40

def post(uri, body: nil)
  uri = URI.parse(uri)
  req = Net::HTTP::Post.new(uri)
  req['Authorization'] = "Bearer #{@oauth_token}"
  req.body = body.to_json if body
  req['Content-Type'] = 'application/json'

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  Response.new(res).raise_on_error
end

#put(uri) ⇒ SeismicAPI::Response

Put request

Parameters:

  • uri (String)

Returns:


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/seismic_api/request.rb', line 81

def put(uri)
  uri = URI.parse(uri)
  req = Net::HTTP::Put.new(uri)
  if @oauth_token
    req['Authorization'] = "Bearer #{@oauth_token}"
  end
  req['Content-Type'] = 'application/json'

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  Response.new(res).raise_on_error
end