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
23
24
25
# 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

  @connection = Net::HTTP.new(host, port)
  @connection.use_ssl = ssl

  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



36
37
38
39
40
# File 'lib/activehistory/connection.rb', line 36

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



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
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/activehistory/connection.rb', line 46

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.request(request) do |response|
      if response['API-Version-Deprecated']
        logger.warn("DEPRECATION WARNING: API v#{API_VERSION} is being phased out")
      end

      validate_response_code(response)

      # Get the cookies
      response.each_header do |key, value|
        if key.downcase == 'set-cookie' && Thread.current[:sunstone_cookie_store]
          Thread.current[:sunstone_cookie_store].set_cookie(request_uri, value)
        end
      end

      if block_given?
        return_value =yield(response)
      else
        return_value =response
      end
    end
  rescue ActiveRecord::ConnectionNotEstablished
    retry_count += 1
    retry_count == 1 ? retry : raise
  end

  return_value
end

#urlObject



42
43
44
# File 'lib/activehistory/connection.rb', line 42

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

#user_agentObject



27
28
29
30
31
32
33
34
# File 'lib/activehistory/connection.rb', line 27

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

#validate_response_code(response) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/activehistory/connection.rb', line 92

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