Class: Strobe::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/connection.rb

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, headers = {}) ⇒ Connection

Returns a new instance of Connection.



14
15
16
17
18
19
# File 'lib/strobe/connection.rb', line 14

def initialize(url, headers = {})
  @host, @port = self.class.host_and_port(url)
  @headers     = headers
  @user        = nil
  @password    = nil
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/strobe/connection.rb', line 12

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/strobe/connection.rb', line 12

def port
  @port
end

Class Method Details

.host_and_port(uri) ⇒ Object



6
7
8
9
10
# File 'lib/strobe/connection.rb', line 6

def self.host_and_port(uri)
  uri = "http://#{uri}" unless uri =~ %r[^https?://]
  uri = URI(uri)
  [ uri.host, uri.port ]
end

Instance Method Details

#authenticate_with_token(token) ⇒ Object



21
22
23
# File 'lib/strobe/connection.rb', line 21

def authenticate_with_token(token)
  authenticate_with_user_and_password(token, '')
end

#authenticate_with_user_and_password(user, password) ⇒ Object



25
26
27
28
29
# File 'lib/strobe/connection.rb', line 25

def authenticate_with_user_and_password(user, password)
  @json_connection = nil
  @reg_connection  = nil
  @user, @password = user, password
end

#delete(*args) ⇒ Object



43
44
45
# File 'lib/strobe/connection.rb', line 43

def delete(*args)
  request :delete, *args
end

#get(*args) ⇒ Object



31
32
33
# File 'lib/strobe/connection.rb', line 31

def get(*args)
  request :get, *args
end

#post(*args) ⇒ Object



35
36
37
# File 'lib/strobe/connection.rb', line 35

def post(*args)
  request :post, *args
end

#put(*args) ⇒ Object



39
40
41
# File 'lib/strobe/connection.rb', line 39

def put(*args)
  request :put, *args
end

#request(method, path, body = nil, headers = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/strobe/connection.rb', line 106

def request(method, path, body = nil, headers = {})
  headers.keys.each do |key|
    headers[key.to_s.downcase] = headers.delete(key)
  end

  # add connection-global headers, which will be
  # overwritten by request-specific headers
  @headers.each do |k, v|
    headers[k.to_s.downcase] ||= v if v
  end

  headers['authorization'] ||= authorization_header

  if body
    headers['content-type'] ||= 'application/json'

    if headers['content-type'] == 'application/json'
      body = ActiveSupport::JSON.encode(body)
    end
  end

  http    = build_http
  request = Net::HTTPGenericRequest.new(
    method.to_s.upcase, !!body, true, path, headers)

  if body.respond_to?(:read)
    request.body_stream = body
    body = nil
  end

  Response.new(http.request(request, body))
end