Class: Conf::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/conf/api/client.rb,
lib/conf/api/client/version.rb

Constant Summary collapse

VERSION =
'0.1.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, pass, url) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/conf/api/client.rb', line 10

def initialize(user, pass, url)
  self.user = user
  self.pass = pass
  self.url  = url
  self.conn = Faraday.new(url: url) do |faraday|
    faraday.request :url_encoded # form-encode POST params
    # faraday.response :logger                  # log requests to STDOUT
    faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
    faraday.basic_auth(self.user, self.pass)
  end
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



8
9
10
# File 'lib/conf/api/client.rb', line 8

def conn
  @conn
end

#passObject

Returns the value of attribute pass.



8
9
10
# File 'lib/conf/api/client.rb', line 8

def pass
  @pass
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/conf/api/client.rb', line 8

def url
  @url
end

#userObject

Returns the value of attribute user.



8
9
10
# File 'lib/conf/api/client.rb', line 8

def user
  @user
end

Instance Method Details

#create(params) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/conf/api/client.rb', line 32

def create(params)
  response = conn.post do |req|
    req.url '/rest/api/content'
    req.headers['Content-Type'] = 'application/json'
    req.body                    = params.to_json
  end
  JSON.parse(response.body)
end

#get(params) ⇒ Object



22
23
24
25
# File 'lib/conf/api/client.rb', line 22

def get(params)
  response = conn.get('/rest/api/content', params)
  JSON.parse(response.body)['results']
end

#get_by_id(id) ⇒ Object



27
28
29
30
# File 'lib/conf/api/client.rb', line 27

def get_by_id(id)
  response = conn.get('/rest/api/content/' + id)
  JSON.parse(response.body)
end

#update(id, params) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/conf/api/client.rb', line 41

def update(id, params)
  response = conn.put do |req|
    req.url "/rest/api/content/#{id}"
    req.headers['Content-Type'] = 'application/json'
    req.body                    = params.to_json
  end
  JSON.parse(response.body)
end