api_consumer 0.0.1

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 +15 -0
  2. data/lib/api_consumer.rb +113 -0
  3. metadata +87 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZGJhMWRiZTI1MmQ3NjhjYjdmNjExY2NmYjVjM2ZkMjY0YjY5MGY5OQ==
5
+ data.tar.gz: !binary |-
6
+ NTczNmYyOWVmNzBiZTQ4YmFiMzU2ZjUyZjU2MTBhZTg4M2RiZWI4YQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MDdlM2U0MTA1ZmE4ZmRhZjAwNDk0NjQyN2ViMzhjNmMxNmEyZDY1NmY0ZWEz
10
+ N2U0ZjA2MGIxYmQ3MTkzZTE4N2FjNGFhZDVjMTg0ZWJkYWY1OTkzZWNlMTNj
11
+ MTIxMjBiOTkxYjA2MTM0Zjk2OTI4NzYxYzc3NzM3ZTA5NGZlODM=
12
+ data.tar.gz: !binary |-
13
+ NDM1ZjVkMjg0MWE2YTdmNjg4MzRmOWZmMDc5OTcyZWVlZDQ1ODZmOGI5NGU4
14
+ NDFkNGMzOTQwYzc1Mzc5MGUzZjU5YTJhNzI0NWYxZTQ4MjNhOTg0NTNhMjZi
15
+ NDBkNzI5NGVjYWVkMzkzNDJjMTdmMDNjN2VmOGYwYjFhYWVlNWI=
@@ -0,0 +1,113 @@
1
+ class APIConsumer
2
+ require 'yaml'
3
+ require "net/https"
4
+ require "uri"
5
+ require "json"
6
+ require 'uber_cache'
7
+
8
+ class << self
9
+ @settings = {}
10
+ def inherited(subclass)
11
+ configs = YAML.load_file("config/#{snake_case(subclass)}.yml")
12
+ configs[snake_case(subclass)].each{ |k,v| subclass.set(k.to_sym, v) }
13
+ super
14
+ end
15
+
16
+ def memcache?
17
+ settings[:use_memcache]
18
+ end
19
+
20
+ def memcache_hosts
21
+ settings[:memcache_hosts]
22
+ end
23
+
24
+ def set(key, val)
25
+ settings[key] = val
26
+ end
27
+
28
+ def settings
29
+ @settings ||= {}
30
+ end
31
+
32
+ DEFAULT_REQUEST_OPTS = {:method => :get, :headers => { "Accept" => "application/json", "Content-Type" => "application/json", "User-Agent" => "EME-WEB-STORE-#{ENV['RACK_ENV']|| 'dev'}" }}
33
+ def do_request(path, conn, opts = {})
34
+ opts[:headers] = DEFAULT_REQUEST_OPTS[:headers].merge(opts[:headers] || {})
35
+ opts[:method] = opts[:method] || DEFAULT_REQUEST_OPTS[:method]
36
+
37
+ req = if( opts[:method] == :get)
38
+ Net::HTTP::Get.new(path)
39
+ elsif( opts[:method] == :post)
40
+ Net::HTTP::Post.new(path)
41
+ else
42
+ puts "BUG - method=>(#{opts[:method]})"
43
+ end
44
+ opts[:headers].each { |k,v| req[k] = v }
45
+ req.basic_auth settings[:api_user], settings[:api_password] if settings[:api_user] && settings[:api_password]
46
+ req["connection"] = 'keep-alive'
47
+ req.body = opts[:body] if opts[:body]
48
+ #puts( "REQUEST!!! #{opts[:headers]} #{path};\n#{@uri.host}:::#{@uri.port}")
49
+ #puts("BODY: #{req.body}")
50
+
51
+ response = nil
52
+ begin
53
+ response = conn.request(req)
54
+ if( settings[:type] == "json")
55
+ results = JSON.parse(response.body)
56
+ if ![200, 201].include?(response.code.to_i)
57
+ results = error_code(response.code, opts[:errors])
58
+ end
59
+ return results
60
+ end
61
+ rescue Exception => exception
62
+ puts exception.message
63
+ puts exception.backtrace
64
+ puts "================="
65
+ # Airbrake.notify(exception)
66
+ if( settings[:type] == "json")
67
+ return error_code(response.code, opts[:errors])
68
+ end
69
+ end
70
+ return response.body
71
+ end
72
+
73
+ def connection(connection_flag = :normal)
74
+ @connections ||= {}
75
+ return @connections[connection_flag] if @connections[connection_flag]
76
+ @connections[connection_flag] = create_connection
77
+ end
78
+
79
+ def create_connection(debug = false)
80
+ if @uri.nil? || @uri.port.nil?
81
+ #puts "TRYING TO CONNECT: #{settings[:url]}"
82
+ @uri = URI.parse("#{settings[:url]}/")
83
+ end
84
+ http = Net::HTTP.new(@uri.host, @uri.port)
85
+ if settings[:ssl] == true
86
+ http.use_ssl = true
87
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
88
+ end
89
+ http.set_debug_output $stderr if debug
90
+ http.open_timeout = 7
91
+ http.read_timeout = 15
92
+ http
93
+ end
94
+
95
+ def cache
96
+ @cache ||= UberCache.new(settings[:cache_prefix], settings[:memcache_hosts])
97
+ end
98
+
99
+ private
100
+ def snake_case(camel_cased_word)
101
+ camel_cased_word.to_s.gsub(/::/, '/').
102
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
103
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
104
+ tr("-", "_").
105
+ downcase
106
+ end
107
+
108
+ def error_code(code, errors = nil)
109
+ return {:error => true, :message => errors[code.to_s]} if errors && errors[code.to_s]
110
+ return {:error => true, :message => "API error: #{code}" }
111
+ end
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_consumer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Reister
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: uber_cache
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fakeweb
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ description: Easy to use API consumer - Setup your API connection in a yaml file,
56
+ and use the helper methods to make easy access APIs calls
57
+ email: chris@chrisreister.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/api_consumer.rb
63
+ homepage: https://github.com/chrisftw/api_consumer
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.2.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Consume all the APIs
87
+ test_files: []