Class: APIConsumer

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

Constant Summary collapse

DEFAULT_REQUEST_OPTS =
{:method => :get, :headers => { "Accept" => "application/json", "Content-Type" => "application/json", "User-Agent" => "EME-WEB-STORE-#{ENV['RACK_ENV']|| 'dev'}" }}

Class Method Summary collapse

Class Method Details

.cacheObject



95
96
97
# File 'lib/api_consumer.rb', line 95

def cache
  @cache ||= UberCache.new(settings[:cache_prefix], settings[:memcache_hosts])
end

.connection(connection_flag = :normal) ⇒ Object



73
74
75
76
77
# File 'lib/api_consumer.rb', line 73

def connection(connection_flag = :normal)
  @connections ||= {}
  return @connections[connection_flag] if @connections[connection_flag]
  @connections[connection_flag] = create_connection
end

.create_connection(debug = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/api_consumer.rb', line 79

def create_connection(debug = false)
  if @uri.nil? || @uri.port.nil?
    #puts "TRYING TO CONNECT: #{settings[:url]}"
    @uri = URI.parse("#{settings[:url]}/")
  end
  http = Net::HTTP.new(@uri.host, @uri.port)
  if settings[:ssl] == true
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http.set_debug_output $stderr if debug
  http.open_timeout = 7
  http.read_timeout = 15
  http
end

.do_request(path, conn, opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
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
# File 'lib/api_consumer.rb', line 33

def do_request(path, conn, opts = {})
  opts[:headers] = DEFAULT_REQUEST_OPTS[:headers].merge(opts[:headers] || {})
  opts[:method] = opts[:method] || DEFAULT_REQUEST_OPTS[:method]

  req = if( opts[:method] == :get)
    Net::HTTP::Get.new(path)
  elsif( opts[:method] == :post)
    Net::HTTP::Post.new(path)
  else
    puts "BUG - method=>(#{opts[:method]})"
  end
  opts[:headers].each { |k,v| req[k] = v }
  req.basic_auth settings[:api_user], settings[:api_password] if settings[:api_user] && settings[:api_password]
  req["connection"] = 'keep-alive'
  req.body = opts[:body] if opts[:body]
  #puts( "REQUEST!!! #{opts[:headers]} #{path};\n#{@uri.host}:::#{@uri.port}")
  #puts("BODY: #{req.body}")

  response = nil
  begin
    response = conn.request(req)
    if( settings[:type] == "json")
      results = JSON.parse(response.body)
      if ![200, 201].include?(response.code.to_i)
        results = error_code(response.code, opts[:errors])
      end
      return results
    end
  rescue Exception => exception
    puts exception.message
    puts exception.backtrace
    puts "================="
    # Airbrake.notify(exception)
    if( settings[:type] == "json")
      return error_code(response.code, opts[:errors])
    end
  end
  return response.body
end

.inherited(subclass) ⇒ Object



10
11
12
13
14
# File 'lib/api_consumer.rb', line 10

def inherited(subclass)
  configs = YAML.load_file("config/#{snake_case(subclass)}.yml")
  configs[snake_case(subclass)].each{ |k,v| subclass.set(k.to_sym, v) }
  super
end

.memcache?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/api_consumer.rb', line 16

def memcache?
  settings[:use_memcache]
end

.memcache_hostsObject



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

def memcache_hosts
  settings[:memcache_hosts]
end

.set(key, val) ⇒ Object



24
25
26
# File 'lib/api_consumer.rb', line 24

def set(key, val)
  settings[key] = val
end

.settingsObject



28
29
30
# File 'lib/api_consumer.rb', line 28

def settings
  @settings ||= {}
end