Class: ActiveHistory::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/activehistory/connection.rb', line 8

def initialize(config)
  if config[:url]
    uri = URI.parse(config.delete(:url))
    config[:api_key]  ||= (uri.user ? CGI.unescape(uri.user) : nil)
    config[:host]     ||= uri.host
    config[:port]     ||= uri.port
    config[:ssl]      ||= (uri.scheme == 'https')
  end

  [:api_key, :host, :port, :ssl, :user_agent].each do |key|
    self.instance_variable_set(:"@#{key}", config[key])
  end

  true
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



6
7
8
# File 'lib/activehistory/connection.rb', line 6

def api_key
  @api_key
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/activehistory/connection.rb', line 6

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/activehistory/connection.rb', line 6

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



6
7
8
# File 'lib/activehistory/connection.rb', line 6

def ssl
  @ssl
end

Instance Method Details

#post(path, body = nil, &block) ⇒ Object



33
34
35
36
37
# File 'lib/activehistory/connection.rb', line 33

def post(path, body=nil, &block)
  request = Net::HTTP::Post.new(path)

  send_request(request, body, &block)
end

#send_request(request, body = nil, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/activehistory/connection.rb', line 43

def send_request(request, body=nil, &block)
  request['Accept'] = 'application/json'
  request['User-Agent'] = user_agent
  request['Api-Key'] = api_key
  request['Content-Type'] = 'application/json'
  
  if body.is_a?(IO)
    request['Transfer-Encoding'] = 'chunked'
    request.body_stream =  body
  elsif body.is_a?(String)
    request.body = body
  elsif body
    request.body = JSON.generate(body)
  end

  return_value = nil
  retry_count = 0
  begin
    connection = Net::HTTP.new(host, port)
    connection.use_ssl = ssl
    connection.request(request) do |response|
      validate_response_code(response)
      
      if block_given?
        return_value = yield(response)
      else
        return_value = response
      end
    end
  rescue ActiveHistory::Exception::ServiceUnavailable
    retry_count += 1
    retry_count == 1 ? retry : raise
  end

  return_value
end

#urlObject



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

def url
  "http#{ssl ? 's' : ''}://#{host}#{port != 80 ? (port == 443 && ssl ? '' : ":#{port}") : ''}"
end

#user_agentObject



24
25
26
27
28
29
30
31
# File 'lib/activehistory/connection.rb', line 24

def user_agent
  [
    @user_agent,
    "Sunstone/#{ActiveHistory::VERSION}",
    "Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}",
    RUBY_PLATFORM
  ].compact.join(' ')
end

#validate_response_code(response) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/activehistory/connection.rb', line 80

def validate_response_code(response)
  code = response.code.to_i

  if !(200..299).include?(code)
    case code
    when 400
      raise ActiveHistory::Exception::BadRequest, response.body
    when 401
      raise ActiveHistory::Exception::Unauthorized, response
    when 404
      raise ActiveHistory::Exception::NotFound, response
    when 410
      raise ActiveHistory::Exception::Gone, response
    when 422
      raise ActiveHistory::Exception::ApiVersionUnsupported, response
    when 503
      raise ActiveHistory::Exception::ServiceUnavailable, response
    when 301
      raise ActiveHistory::Exception::MovedPermanently, response
    when 502
      raise ActiveHistory::Exception::BadGateway, response
    when 300..599
      raise ActiveHistory::Exception, response
    else
      raise ActiveHistory::Exception, response
    end
  end
end