Class: Pdm::ApiRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/pdm/api_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, api_key, accept = :json) ⇒ ApiRequest

Returns a new instance of ApiRequest.



13
14
15
16
17
# File 'lib/pdm/api_request.rb', line 13

def initialize(endpoint, api_key, accept = :json)
  self.endpoint = endpoint
  self.api_key = api_key
  self.accept = accept
end

Instance Attribute Details

#acceptObject

Returns the value of attribute accept.



11
12
13
# File 'lib/pdm/api_request.rb', line 11

def accept
  @accept
end

#api_keyObject

Returns the value of attribute api_key.



11
12
13
# File 'lib/pdm/api_request.rb', line 11

def api_key
  @api_key
end

#endpointObject

Returns the value of attribute endpoint.



11
12
13
# File 'lib/pdm/api_request.rb', line 11

def endpoint
  @endpoint
end

Instance Method Details

#decorate_params(params) ⇒ Object



20
21
22
23
24
# File 'lib/pdm/api_request.rb', line 20

def decorate_params(params)
  params = params.dup
  params["api_key"] = self.api_key
  params
end

#get(path, params) ⇒ Object



36
37
38
39
40
# File 'lib/pdm/api_request.rb', line 36

def get(path, params)
  with_rest_client(path, params) do |r, uri, params|
    r.get(uri, :params => params, :accept => accept)
  end
end

#post(path, params) ⇒ Object



42
43
44
45
46
# File 'lib/pdm/api_request.rb', line 42

def post(path, params)
  with_rest_client(path, params) do |r, uri, params|
    r.post(uri, params, :accept => accept)
  end
end

#put(path, params) ⇒ Object



48
49
50
51
52
# File 'lib/pdm/api_request.rb', line 48

def put(path, params)
  with_rest_client(path, params) do |r, uri, params|
    r.put(uri, params, :accept => accept)
  end
end

#uri_from_path(path) ⇒ Object



26
27
28
# File 'lib/pdm/api_request.rb', line 26

def uri_from_path(path)
  URI.join(self.endpoint, path.to_s)
end

#with_rest_client(path, params) {|::RestClient, uri.to_s, params| ... } ⇒ Object

Yields:

  • (::RestClient, uri.to_s, params)


30
31
32
33
34
# File 'lib/pdm/api_request.rb', line 30

def with_rest_client(path, params)
  uri = uri_from_path(path)
  params = decorate_params(params)
  yield(::RestClient, uri.to_s, params)
end