Module: ThrillcallAPI
- Defined in:
- lib/thrillcall-api/result.rb,
lib/thrillcall-api.rb,
lib/thrillcall-api/version.rb,
lib/thrillcall-api/exceptions.rb
Overview
Defined Under Namespace
Constant Summary collapse
- RETRY_ERRNO =
[Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Errno::EHOSTDOWN, Errno::EINVAL]
- RETRY_NET =
[Net::HTTPBadResponse, Net::HTTPRequestTimeOut, Net::HTTPServerError, # 5xx Net::HTTPInternalServerError, # 500 Net::HTTPNotImplemented, # 501 Net::HTTPBadGateway, # 502 Net::HTTPServiceUnavailable, # 503 Net::HTTPGatewayTimeOut, # 504 Net::HTTPVersionNotSupported]
- RETRY_FARADAY =
[Faraday::Error, Faraday::Error::ClientError]
- RETRY_DEFAULT =
[Timeout::Error, EOFError, SocketError]
- RETRY_ALL =
RETRY_DEFAULT + RETRY_ERRNO + RETRY_NET + RETRY_FARADAY
- VERSION =
"0.1.0"
Class Attribute Summary collapse
-
.base ⇒ Object
Returns the value of attribute base.
-
.conn ⇒ Object
Returns the value of attribute conn.
-
.cur_api_key ⇒ Object
Returns the value of attribute cur_api_key.
-
.result ⇒ Object
Returns the value of attribute result.
Class Method Summary collapse
- .get(endpoint, params) ⇒ Object
- .method_missing(method, *args, &block) ⇒ Object
-
.new(cur_api_key, options = {}) ⇒ Object
Set up the Faraday connection based on configuration.
- .on_retry_exception ⇒ Object
- .post(endpoint, params, method = :post) ⇒ Object
- .put(endpoint, params) ⇒ Object
Class Attribute Details
.base ⇒ Object
Returns the value of attribute base.
33 34 35 |
# File 'lib/thrillcall-api.rb', line 33 def base @base end |
.conn ⇒ Object
Returns the value of attribute conn.
33 34 35 |
# File 'lib/thrillcall-api.rb', line 33 def conn @conn end |
.cur_api_key ⇒ Object
Returns the value of attribute cur_api_key.
33 34 35 |
# File 'lib/thrillcall-api.rb', line 33 def cur_api_key @cur_api_key end |
.result ⇒ Object
Returns the value of attribute result.
33 34 35 |
# File 'lib/thrillcall-api.rb', line 33 def result @result end |
Class Method Details
.get(endpoint, params) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/thrillcall-api.rb', line 97 def get(endpoint, params) r = nil retriable :on => @retry_exceptions, :tries => @retry_tries, :timeout => @retry_timeout, :on_retry => on_retry_exception do r = @conn.get do |req| req.url endpoint, params.merge(:api_key => @cur_api_key) end end JSON.parse(r.body) end |
.method_missing(method, *args, &block) ⇒ Object
122 123 124 125 126 |
# File 'lib/thrillcall-api.rb', line 122 def method_missing(method, *args, &block) r = Result.new r.send(method, args, block) return r end |
.new(cur_api_key, options = {}) ⇒ Object
Set up the Faraday connection based on configuration
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/thrillcall-api.rb', line 36 def new(cur_api_key, = {}) = { :base_url => "https://api.thrillcall.com/api/", :version => 3, :logger => false, :timeout => 20 } opts = .merge() @retry_exceptions = opts[:retry_exceptions] || RETRY_ALL @retry_tries = opts[:retry_tries] || 5 @retry_timeout = opts[:timeout] || 10 @cur_api_key = cur_api_key base_url = opts[:base_url] version = opts[:version] logger = opts[:logger] faraday_opts = { :timeout => opts[:timeout], :open_timeout => opts[:timeout] } # Make sure the base_url is in the form https://.../ unless base_url.match /^(http|https):\/\// base_url = "https://" + base_url end unless base_url.match /\/$/ base_url = base_url + "/" end # Set JSON accept header and custom user agent @headers = { :accept => 'application/json', :user_agent => "Thrillcall API Wrapper version #{ThrillcallAPI::VERSION}" } @base = "#{base_url}v#{version}/" @conn = Faraday.new( :url => @base, :headers => @headers, :options => faraday_opts ) do |builder| builder.adapter Faraday.default_adapter if logger builder.response :logger end # This will raise an exception if an error occurs during a request builder.response :raise_error end @result = Result.new return self end |
.on_retry_exception ⇒ Object
90 91 92 93 94 95 |
# File 'lib/thrillcall-api.rb', line 90 def on_retry_exception Proc.new do |exception, tries| msg = "ThrillcallAPI : #{exception.class}: '#{exception.}' - #{tries} attempts." @conn.send(:warn, msg) end end |
.post(endpoint, params, method = :post) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/thrillcall-api.rb', line 107 def post(endpoint, params, method = :post) r = nil block = lambda do |req| req.url endpoint, params.merge(:api_key => @cur_api_key) end retriable :on => @retry_exceptions, :tries => @retry_tries, :timeout => @retry_timeout, :on_retry => on_retry_exception do r = @conn.send(method, &block) end JSON.parse(r.body) end |
.put(endpoint, params) ⇒ Object
118 119 120 |
# File 'lib/thrillcall-api.rb', line 118 def put(endpoint, params) post(endpoint, params, :put) end |