Class: GunBroker::API

Inherits:
Object
  • Object
show all
Defined in:
lib/gun_broker/api.rb

Constant Summary collapse

GUNBROKER_API =
'https://api.gunbroker.com/v1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, params = {}, headers = {}) ⇒ API


9
10
11
12
13
14
15
# File 'lib/gun_broker/api.rb', line 9

def initialize(path, params = {}, headers = {})
  raise "Path must start with '/': #{path}" unless path.start_with?('/')

  @path = path
  @params = params
  @headers = headers
end

Class Method Details

.delete(*args) ⇒ Object


17
18
19
# File 'lib/gun_broker/api.rb', line 17

def self.delete(*args)
  new(*args).delete!
end

.get(*args) ⇒ Object


21
22
23
# File 'lib/gun_broker/api.rb', line 21

def self.get(*args)
  new(*args).get!
end

.post(*args) ⇒ Object


25
26
27
# File 'lib/gun_broker/api.rb', line 25

def self.post(*args)
  new(*args).post!
end

Instance Method Details

#delete!Object


29
30
31
32
33
# File 'lib/gun_broker/api.rb', line 29

def delete!
  request = Net::HTTP::Delete.new(uri)
  response = get_response(request)
  handle_response(response)
end

#get!Object


35
36
37
38
39
40
41
# File 'lib/gun_broker/api.rb', line 35

def get!
  uri.query = URI.encode_www_form(@params)

  request = Net::HTTP::Get.new(uri)
  response = get_response(request)
  handle_response(response)
end

#post!Object


43
44
45
46
47
48
49
# File 'lib/gun_broker/api.rb', line 43

def post!
  request = Net::HTTP::Post.new(uri)
  request.body = @params.to_json

  response = get_response(request)
  handle_response(response)
end