api_consumer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +8 -8
  2. data/lib/api_consumer.rb +41 -12
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZGJhMWRiZTI1MmQ3NjhjYjdmNjExY2NmYjVjM2ZkMjY0YjY5MGY5OQ==
4
+ M2M5NDM5ZDgwZDhjZDQyZjM1MTQwZjJkZmE1YWZmZTZiOGM0OGEwNw==
5
5
  data.tar.gz: !binary |-
6
- NTczNmYyOWVmNzBiZTQ4YmFiMzU2ZjUyZjU2MTBhZTg4M2RiZWI4YQ==
6
+ YTUzMDRhNzg3ZGQyMjQyZjZhZTdkNGYwZjIzNTBlZmRkMmYyODQyMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDdlM2U0MTA1ZmE4ZmRhZjAwNDk0NjQyN2ViMzhjNmMxNmEyZDY1NmY0ZWEz
10
- N2U0ZjA2MGIxYmQ3MTkzZTE4N2FjNGFhZDVjMTg0ZWJkYWY1OTkzZWNlMTNj
11
- MTIxMjBiOTkxYjA2MTM0Zjk2OTI4NzYxYzc3NzM3ZTA5NGZlODM=
9
+ YzhlMGUyNGMxOThjZmQ2MmM2NDdkZjhhMTM5ZjQ1OTM0OWExYmYxZGVmYWEw
10
+ NmNiNzcxMjgyN2Q5YWM1YTczNDIyNTdjZjQwYWRmMDZlNDk1ZTNjMDJmYTU2
11
+ MjYyMGY4NmU3ODNiNGFjY2U2NTZjNjU5ZjE1OWM2MjA5YzQ3NzI=
12
12
  data.tar.gz: !binary |-
13
- NDM1ZjVkMjg0MWE2YTdmNjg4MzRmOWZmMDc5OTcyZWVlZDQ1ODZmOGI5NGU4
14
- NDFkNGMzOTQwYzc1Mzc5MGUzZjU5YTJhNzI0NWYxZTQ4MjNhOTg0NTNhMjZi
15
- NDBkNzI5NGVjYWVkMzkzNDJjMTdmMDNjN2VmOGYwYjFhYWVlNWI=
13
+ M2RiZDMwMDBmYjZkZGMyOWIzNWY1YmIxMGQzYTY3MDI1ZTA5YzI4OWY1MzI5
14
+ ZjYxNzk0MzAzY2E4OTc3MzU1MTdiYTQwMGE2ZDE1ZTllOWFjYzE4YWE4M2Vh
15
+ NmM5NWU2NzUzNjY2ZWE2Mzc4OTQxNTY4Yzc2ODRhNGNlNTE1MjY=
data/lib/api_consumer.rb CHANGED
@@ -1,18 +1,51 @@
1
1
  class APIConsumer
2
2
  require 'yaml'
3
- require "net/https"
4
- require "uri"
5
- require "json"
3
+ require 'net/https'
4
+ require 'uri'
5
+ require 'json'
6
6
  require 'uber_cache'
7
+ require 'logger'
7
8
 
8
9
  class << self
9
10
  @settings = {}
10
11
  def inherited(subclass)
11
12
  configs = YAML.load_file("config/#{snake_case(subclass)}.yml")
12
13
  configs[snake_case(subclass)].each{ |k,v| subclass.set(k.to_sym, v) }
14
+ subclass.set_logger(Logger.new(settings[:log_file] || "./log/#{snake_case(subclass)}_api.log"), settings[:log_level])
13
15
  super
14
16
  end
15
17
 
18
+ def set_logger(logger, level=nil)
19
+ @logger = logger.nil? ? Logger.new(STDERR) : logger
20
+ set_log_level(level)
21
+ end
22
+
23
+ def log
24
+ @logger
25
+ end
26
+
27
+ def set_log_level(level=nil)
28
+ if level.nil?
29
+ level = if([nil, "development", "test"].include?(ENV['RACK_ENV']))
30
+ :info
31
+ else
32
+ :warn
33
+ end
34
+ end
35
+ @logger.level = case level.to_sym
36
+ when :debug
37
+ Logger::DEBUG
38
+ when :info
39
+ Logger::INFO
40
+ when :error
41
+ Logger::ERROR
42
+ when :fatal
43
+ Logger::FATAL
44
+ else #warn
45
+ Logger::WARN
46
+ end
47
+ end
48
+
16
49
  def memcache?
17
50
  settings[:use_memcache]
18
51
  end
@@ -39,14 +72,12 @@ class APIConsumer
39
72
  elsif( opts[:method] == :post)
40
73
  Net::HTTP::Post.new(path)
41
74
  else
42
- puts "BUG - method=>(#{opts[:method]})"
75
+ log.error "BUG - method=>(#{opts[:method]})"
43
76
  end
44
77
  opts[:headers].each { |k,v| req[k] = v }
45
78
  req.basic_auth settings[:api_user], settings[:api_password] if settings[:api_user] && settings[:api_password]
46
79
  req["connection"] = 'keep-alive'
47
80
  req.body = opts[:body] if opts[:body]
48
- #puts( "REQUEST!!! #{opts[:headers]} #{path};\n#{@uri.host}:::#{@uri.port}")
49
- #puts("BODY: #{req.body}")
50
81
 
51
82
  response = nil
52
83
  begin
@@ -59,12 +90,10 @@ class APIConsumer
59
90
  return results
60
91
  end
61
92
  rescue Exception => exception
62
- puts exception.message
63
- puts exception.backtrace
64
- puts "================="
65
- # Airbrake.notify(exception)
93
+ log.error exception.message
94
+ log.error exception.backtrace
66
95
  if( settings[:type] == "json")
67
- return error_code(response.code, opts[:errors])
96
+ return error_code(response ? response.code : "NO CODE" , opts[:errors])
68
97
  end
69
98
  end
70
99
  return response.body
@@ -78,7 +107,7 @@ class APIConsumer
78
107
 
79
108
  def create_connection(debug = false)
80
109
  if @uri.nil? || @uri.port.nil?
81
- #puts "TRYING TO CONNECT: #{settings[:url]}"
110
+ log.info "CONNECTING TO: #{settings[:url]}"
82
111
  @uri = URI.parse("#{settings[:url]}/")
83
112
  end
84
113
  http = Net::HTTP.new(@uri.host, @uri.port)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_consumer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Reister
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-29 00:00:00.000000000 Z
11
+ date: 2014-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uber_cache