Class: IpTracker::Client

Inherits:
Object
  • Object
show all
Includes:
Connection, Request
Defined in:
lib/ip_tracker/client.rb,
lib/ip_tracker/client/errors.rb,
lib/ip_tracker/client/request.rb,
lib/ip_tracker/client/connection.rb,
lib/ip_tracker/client/authentication.rb

Defined Under Namespace

Modules: Authentication, Connection, Request Classes: HostTakenError, TargetError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Request

#post, #put

Constructor Details

#initializeClient

Returns a new instance of Client.



11
12
13
14
# File 'lib/ip_tracker/client.rb', line 11

def initialize
  @target_url = IpTracker::DEFAULT_LOCAL_TARGET
  @http_adapter = :net_http
end

Instance Attribute Details

#host_tokenObject

Returns the value of attribute host_token.



20
21
22
# File 'lib/ip_tracker/client.rb', line 20

def host_token
  @host_token
end

#http_adapterObject (readonly)

Returns the value of attribute http_adapter.



9
10
11
# File 'lib/ip_tracker/client.rb', line 9

def http_adapter
  @http_adapter
end

#target_urlObject (readonly)

Returns the value of attribute target_url.



9
10
11
# File 'lib/ip_tracker/client.rb', line 9

def target_url
  @target_url
end

Instance Method Details

#register(hostname) ⇒ Object

Raises:



22
23
24
25
26
27
28
# File 'lib/ip_tracker/client.rb', line 22

def register hostname
  response = post(IpTracker::HOSTS_PATH, :body => { name: hostname } )
  raise HostTakenError if response.code == 201
  raise TargetError if response.code == 200 || response.id == nil

  @host_token = response.id
end

#update(host_id, attr, value) ⇒ Object

Raises:



30
31
32
33
34
35
36
# File 'lib/ip_tracker/client.rb', line 30

def update host_id, attr, value
  response = put(IpTracker::HOSTS_PATH + "/#{host_id}", :body => {attr.to_sym => value})

  raise TargetError if response.code == 200 || response.send(attr) != value

  true
end