Class: BitBalloon::Client
- Inherits:
-
Object
- Object
- BitBalloon::Client
- Defined in:
- lib/bitballoon/client.rb
Defined Under Namespace
Classes: AuthenticationError, BitBalloonError, ConnectionError, InternalServerError, NotFoundError
Constant Summary collapse
- ENDPOINT =
ENV['OAUTH_CLIENT_API_URL'] || 'https://www.bitballoon.com'
- API_VERSION =
"v1"
- RETRIES =
3
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#oauth ⇒ Object
Returns the value of attribute oauth.
Instance Method Summary collapse
- #access_tokens ⇒ Object
- #authorize_from_code!(authorization_code, options) ⇒ Object
- #authorize_from_credentials! ⇒ Object
- #authorize_url(options) ⇒ Object
- #dns_zones ⇒ Object
- #forms ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #request(verb, path, opts = {}, &block) ⇒ Object
- #sites ⇒ Object
- #submissions ⇒ Object
- #users ⇒ Object
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/bitballoon/client.rb', line 17 def initialize() self.client_id = [:client_id] self.client_secret = [:client_secret] self.access_token = [:access_token] self.endpoint = [:endpoint] || ENDPOINT self.oauth = OAuth2::Client.new(client_id, client_secret, :site => endpoint, :connection_build => lambda {|f| f.request :multipart f.request :url_encoded f.adapter :net_http }) end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
15 16 17 |
# File 'lib/bitballoon/client.rb', line 15 def access_token @access_token end |
#client_id ⇒ Object
Returns the value of attribute client_id.
15 16 17 |
# File 'lib/bitballoon/client.rb', line 15 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
15 16 17 |
# File 'lib/bitballoon/client.rb', line 15 def client_secret @client_secret end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
15 16 17 |
# File 'lib/bitballoon/client.rb', line 15 def endpoint @endpoint end |
#oauth ⇒ Object
Returns the value of attribute oauth.
15 16 17 |
# File 'lib/bitballoon/client.rb', line 15 def oauth @oauth end |
Instance Method Details
#access_tokens ⇒ Object
63 64 65 |
# File 'lib/bitballoon/client.rb', line 63 def access_tokens AccessTokens.new(self) end |
#authorize_from_code!(authorization_code, options) ⇒ Object
33 34 35 36 |
# File 'lib/bitballoon/client.rb', line 33 def (, ) @oauth_token = oauth.auth_code.get_token(, ) self.access_token = oauth_token.token end |
#authorize_from_credentials! ⇒ Object
38 39 40 41 |
# File 'lib/bitballoon/client.rb', line 38 def @oauth_token = oauth.client_credentials.get_token self.access_token = oauth_token.token end |
#authorize_url(options) ⇒ Object
29 30 31 |
# File 'lib/bitballoon/client.rb', line 29 def () oauth.auth_code.() end |
#dns_zones ⇒ Object
59 60 61 |
# File 'lib/bitballoon/client.rb', line 59 def dns_zones DnsZones.new(self) end |
#request(verb, path, opts = {}, &block) ⇒ Object
67 68 69 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 101 |
# File 'lib/bitballoon/client.rb', line 67 def request(verb, path, opts={}, &block) retries = 0 begin raise AuthenticationError, "Authorize with BitBalloon before making requests" unless oauth_token if ENV['BB_VERBOSE'] puts "#{verb}, #{::File.join("/api", API_VERSION, path)}" end = {:version => :TLSv1_2} oauth_token.request(verb, ::File.join("/api", API_VERSION, path), .merge(opts), &block) rescue OAuth2::Error => e case e.response.status when 401 raise AuthenticationError, (e, "Authentication Error") when 404 raise NotFoundError, (e, "Not Found") when 500 if retry_request?(verb, e.response.status, retries) retries += 1 retry else raise InternalServerError, (e, "Internal Server Error") end else raise BitBalloonError, (e, "OAuth2 Error") end rescue Faraday::Error::ConnectionFailed, Faraday::Error::TimeoutError, Timeout::Error => e if retry_request?(verb, e.response && e.response.status, retries) retries += 1 retry else raise ConnectionError, (e, "Connection Error") end end end |
#submissions ⇒ Object
51 52 53 |
# File 'lib/bitballoon/client.rb', line 51 def submissions Submissions.new(self) end |