Class: RestPack::Client::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/restpack-group-client/restpack_client_api.rb

Overview

TODO: GJ: move to common client gem

Direct Known Subclasses

Group::Client::Api, Group::Client::BaseApi

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Api

Returns a new instance of Api.



7
8
9
10
11
12
13
14
# File 'lib/restpack-group-client/restpack_client_api.rb', line 7

def initialize(options = {})        
  @options = {
    use_https: true,
    domain: 'localhost:1113',
    username: nil,
    password: nil
  }.merge options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/restpack-group-client/restpack_client_api.rb', line 6

def options
  @options
end

Instance Method Details

#build_url(path) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/restpack-group-client/restpack_client_api.rb', line 34

def build_url(path)
  protocol = @options[:use_https] ? "https://" : "http://"
  username_and_password = ''
  if options[:username] || options[:password]
    username_and_password = "#{options[:username]}:#{options[:password]}@"
  end
  protocol + username_and_password + options[:domain] + path
end

#http_get(path, params = {}) ⇒ Object



16
17
18
19
20
# File 'lib/restpack-group-client/restpack_client_api.rb', line 16

def http_get(path, params = {})
  url = build_url(path)
  json = RestClient.get(url, params: params)
  Yajl::Parser.parse(json, :symbolize_keys => true)
end

#http_post(path, params = {}) ⇒ Object



22
23
24
25
26
# File 'lib/restpack-group-client/restpack_client_api.rb', line 22

def http_post(path, params = {})
  url = build_url(path)
  json = RestClient.post(url, params)
  Yajl::Parser.parse(json, :symbolize_keys => true)
end

#http_put(path, params = {}) ⇒ Object



28
29
30
31
32
# File 'lib/restpack-group-client/restpack_client_api.rb', line 28

def http_put(path, params = {})
  url = build_url(path)
  json = RestClient.put(url, params)
  Yajl::Parser.parse(json, :symbolize_keys => true)
end