Class: BitBalloon::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options)
  self.client_id     = options[:client_id]
  self.client_secret = options[:client_secret]
  self.access_token  = options[:access_token]
  self.endpoint      = options[: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_tokenObject

Returns the value of attribute access_token.



15
16
17
# File 'lib/bitballoon/client.rb', line 15

def access_token
  @access_token
end

#client_idObject

Returns the value of attribute client_id.



15
16
17
# File 'lib/bitballoon/client.rb', line 15

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



15
16
17
# File 'lib/bitballoon/client.rb', line 15

def client_secret
  @client_secret
end

#endpointObject

Returns the value of attribute endpoint.



15
16
17
# File 'lib/bitballoon/client.rb', line 15

def endpoint
  @endpoint
end

#oauthObject

Returns the value of attribute oauth.



15
16
17
# File 'lib/bitballoon/client.rb', line 15

def oauth
  @oauth
end

Instance Method Details

#access_tokensObject



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 authorize_from_code!(authorization_code, options)
  @oauth_token = oauth.auth_code.get_token(authorization_code, options)
  self.access_token = oauth_token.token
end

#authorize_from_credentials!Object



38
39
40
41
# File 'lib/bitballoon/client.rb', line 38

def authorize_from_credentials!
  @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 authorize_url(options)
  oauth.auth_code.authorize_url(options)
end

#dns_zonesObject



59
60
61
# File 'lib/bitballoon/client.rb', line 59

def dns_zones
  DnsZones.new(self)
end

#formsObject



47
48
49
# File 'lib/bitballoon/client.rb', line 47

def forms
  Forms.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
# 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
    oauth_token.request(verb, ::File.join("/api", API_VERSION, path), opts, &block)
  rescue OAuth2::Error => e
    case e.response.status
    when 401
      raise AuthenticationError, message_for(e, "Authentication Error")
    when 404
      raise NotFoundError, message_for(e, "Not Found")
    when 500
      if retry_request?(verb, e.response.status, retries)
        retries += 1
        retry
      else
        raise InternalServerError, message_for(e, "Internal Server Error")
      end
    else
      raise BitBalloonError, message_for(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, message_for(e, "Connection Error")
    end
  end
end

#sitesObject



43
44
45
# File 'lib/bitballoon/client.rb', line 43

def sites
  Sites.new(self)
end

#submissionsObject



51
52
53
# File 'lib/bitballoon/client.rb', line 51

def submissions
  Submissions.new(self)
end

#usersObject



55
56
57
# File 'lib/bitballoon/client.rb', line 55

def users
  Users.new(self)
end