Class: EngagingNetworksRest::Client

Inherits:
Object
  • Object
show all
Includes:
Pages
Defined in:
lib/engaging_networks_rest/client.rb,
lib/engaging_networks_rest/client/pages.rb

Defined Under Namespace

Modules: Pages

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pages

#page, #pages, #process_page_request

Constructor Details

#initialize(api_key:, host:) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/engaging_networks_rest/client.rb', line 12

def initialize(api_key:, host:)
  @api_key = api_key

  @connection = Faraday.new(url: "https://#{host}") do |conn|
    conn.request :json
    conn.response :json, content_type: /\bjson$/

    conn.use Faraday::Response::Logger if ENV['DEBUG']
    conn.use EngagingNetworksRest::Response::RaiseError

    conn.adapter :net_http
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/engaging_networks_rest/client.rb', line 10

def api_key
  @api_key
end

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/engaging_networks_rest/client.rb', line 10

def connection
  @connection
end

#ens_auth_keyObject (readonly)

Returns the value of attribute ens_auth_key.



10
11
12
# File 'lib/engaging_networks_rest/client.rb', line 10

def ens_auth_key
  @ens_auth_key
end

Instance Method Details

#authenticate!Object



26
27
28
29
30
31
32
33
34
# File 'lib/engaging_networks_rest/client.rb', line 26

def authenticate!
  response = connection.post do |req|
    req.headers['Content-Type'] = 'application/json'
    req.url '/ens/service/authenticate'
    req.body = api_key
  end

  @ens_auth_key = response.body['ens-auth-token']
end

#authenticated?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/engaging_networks_rest/client.rb', line 36

def authenticated?
  !ens_auth_key.nil?
end

#get(path:, params: {}) ⇒ Object



40
41
42
# File 'lib/engaging_networks_rest/client.rb', line 40

def get(path:, params: {})
  request(method: :get, path:, params:)
end

#post(path:, body: {}) ⇒ Object



44
45
46
# File 'lib/engaging_networks_rest/client.rb', line 44

def post(path:, body: {})
  request(method: :post, path:, body:)
end