Module: PrintReleaf::API
Constant Summary collapse
- ENDPOINT =
"api.printreleaf.com/v1/"
- PROTOCOL =
"https"
- USER_AGENT =
"PrintReleaf Ruby/#{PrintReleaf::VERSION}"
- MAX_RETRY_COUNT =
2
- RETRY_DELAY_BASE =
Base for exponential delay
1.5
- NETWORK_EXCEPTIONS =
[ SocketError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, RestClient::RequestTimeout ]
- API_EXCEPTIONS =
{ 400 => BadRequest, 401 => , 403 => Forbidden, 404 => NotFound, 429 => RateLimitExceeded, 500 => ServerError }
Instance Attribute Summary collapse
- #api_key ⇒ Object
- #endpoint ⇒ Object
-
#logger ⇒ Object
Returns the value of attribute logger.
- #protocol ⇒ Object
- #user_agent ⇒ Object
Instance Method Summary collapse
- #delete(uri) ⇒ Object
- #get(uri = "/", params = {}) ⇒ Object
- #patch(uri, data = {}) ⇒ Object
- #post(uri, data = {}) ⇒ Object
- #request(verb, uri, params = {}) ⇒ Object
Instance Attribute Details
#api_key ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/printreleaf/api.rb', line 34 def api_key if @api_key.nil? or @api_key.strip.to_s.empty? raise Error, "Missing API Key." else return @api_key end end |
#endpoint ⇒ Object
42 43 44 |
# File 'lib/printreleaf/api.rb', line 42 def endpoint @endpoint || ENDPOINT end |
#logger ⇒ Object
Returns the value of attribute logger.
32 33 34 |
# File 'lib/printreleaf/api.rb', line 32 def logger @logger end |
#protocol ⇒ Object
46 47 48 |
# File 'lib/printreleaf/api.rb', line 46 def protocol @protocol || PROTOCOL end |
#user_agent ⇒ Object
50 51 52 |
# File 'lib/printreleaf/api.rb', line 50 def user_agent @user_agent || USER_AGENT end |
Instance Method Details
#delete(uri) ⇒ Object
66 67 68 |
# File 'lib/printreleaf/api.rb', line 66 def delete(uri) request :delete, uri end |
#get(uri = "/", params = {}) ⇒ Object
54 55 56 |
# File 'lib/printreleaf/api.rb', line 54 def get(uri="/", params={}) request :get, uri, params end |
#patch(uri, data = {}) ⇒ Object
62 63 64 |
# File 'lib/printreleaf/api.rb', line 62 def patch(uri, data={}) request :patch, uri, data end |
#post(uri, data = {}) ⇒ Object
58 59 60 |
# File 'lib/printreleaf/api.rb', line 58 def post(uri, data={}) request :post, uri, data end |
#request(verb, uri, params = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/printreleaf/api.rb', line 70 def request(verb, uri, params={}) perform_request do uri = Util.join_uri(endpoint, uri) url = "#{protocol}://#{uri}" request_params = { method: verb, url: url, headers: { accept: :json, :Authorization => "Bearer #{api_key}", :user_agent => user_agent } } if verb == :get || verb == :delete request_params[:headers][:params] = params unless params.empty? else request_params[:payload] = params.to_json request_params[:headers][:content_type] = :json end unless logger.nil? logger.info "[PrintReleaf] #{verb.upcase} #{uri}" end response = RestClient::Request.execute(request_params) JSON.parse(response.body) end end |