lib/centro/client.rb in centro-client-0.0.3 vs lib/centro/client.rb in centro-client-0.0.4
- old
+ new
@@ -10,35 +10,76 @@
class << self
def api_version
return @api_version if defined?(@api_version)
@api_version = 'v1'
end
+
+ def api_version=(api_version)
+ @api_version = api_version
+ end
+
def auth_host
return @auth_host if defined?(@auth_host)
- @auth_host = 'localhost:3001'
+ @auth_host = 'accounts.centro.net'
end
+
+ def auth_host=(auth_host)
+ @auth_host = auth_host
+ end
+
def finance_host
return @finance_host if defined?(@finance_host)
- @finance_host = 'localhost:4000'
+ @finance_host = 'finance.transis.com'
end
+
+ def finance_host=(finance_host)
+ @finance_host = finance_host
+ end
+
def mms_host
return @mms_host if defined?(@mms_host)
- @mms_host = 'localhost:3000'
+ @mms_host = 'the.centro.net'
end
+
+ def mms_host=(mms_host)
+ @mms_host = mms_host
+ end
+
+ def planner_host
+ return @planner_host if defined?(@planner_host)
+ @planner_host = 'planner.centro.net'
+ end
+
+ def planner_host=(planner_host)
+ @planner_host = planner_host
+ end
+
def ssl_enabled?
return @ssl_enabled if defined?(@ssl_enabled)
- @ssl_enaabled = false
+ @ssl_enabled = true
end
+
+ def ssl_enabled=(ssl_enabled)
+ @ssl_enabled = ssl_enabled
+ end
+
def auth_url
url_from_host(auth_host)
end
+
def finance_url
url_from_host(finance_host)
end
+
def mms_url
url_from_host(mms_host)
end
+
+ def planner_url
+ url_from_host(planner_host)
+ end
+
def url_from_host(host)
(ssl_enabled? ? 'https://' : 'http://') +
host + "/api/#{self.api_version}"
end
end
@@ -47,21 +88,34 @@
def initialize(opts={})
@client_id = opts.delete(:client_id) if opts[:client_id]
@client_secret = opts.delete(:client_secret) if opts[:client_secret]
@access_token = opts.delete(:access_token) if opts[:access_token]
+ @basic_auth_username = opts.delete(:basic_auth_username) if opts[:basic_auth_username]
+ @basic_auth_password = opts.delete(:basic_auth_password) if opts[:basic_auth_password]
end
+ def fetch(url)
+ connection(url).get.body
+ end
+
+ # Data will be JSON-ified if it's not already in string-form
+ def put(url, data)
+ connection(url).put("", data).body
+ end
+
def access_token_expired?
access_token_expires_at.nil? || access_token_expires_at < Time.now
end
def retrieve_access_token(username, password)
raise 'client_id and client_secret required' unless @client_id && @client_secret
- connection = Faraday.new self.class.auth_url do |conn|
+
+ connection = Faraday.new(self.class.auth_url) do |conn|
set_default_connection_options(conn)
end
+
response = connection.post('/oauth/token',
:grant_type => 'password',
:client_id => @client_id,
:client_secret => @client_secret,
:username => username,
@@ -75,11 +129,11 @@
auth_connection.get("me.json").body
end
def get_organizations(user_id=nil)
options = {}
- options[:user_id]=user_id if user_id
+ options[:user_id] = user_id if user_id
auth_connection.get("organizations.json", options).body
end
def get_members_of_organization(organization_id)
auth_connection.get("organizations/#{organization_id}/members.json").body
@@ -125,32 +179,37 @@
conn.adapter Faraday.default_adapter
end
def auth_connection
raise 'access_token required' unless @access_token
- @auth_connection ||= begin
- Faraday.new self.class.auth_url do |conn|
- conn.request :oauth2, @access_token
- set_default_connection_options(conn)
- end
- end
+ @auth_connection ||= connection(self.class.auth_url)
end
+
def finance_connection
raise 'access_token required' unless @access_token
- @finance_connection ||= begin
- Faraday.new self.class.finance_url do |conn|
- conn.request :oauth2, @access_token
- set_default_connection_options(conn)
- end
- end
+ @finance_connection ||= connection(self.class.finance_url)
end
+
def mms_connection
raise 'access_token required' unless @access_token
- @mms_connection ||= begin
- Faraday.new self.class.mms_url do |conn|
- conn.request :oauth2, @access_token
- set_default_connection_options(conn)
- end
+ @mms_connection ||= connection(self.class.mms_url)
+ end
+
+ def planner_connection
+ raise 'basic_auth_username required' unless @basic_auth_username
+ raise 'basic_auth_password required' unless @basic_auth_password
+ @planner_connection ||= Faraday.new(self.class.planner_url) do |conn|
+ conn.basic_auth(@basic_auth_username, @basic_auth_password)
+ set_default_connection_options(conn)
end
end
+
+ def connection(url)
+ raise 'access_token required' unless @access_token
+ Faraday.new url do |conn|
+ conn.request :oauth2, @access_token
+ set_default_connection_options(conn)
+ end
+ end
+
end
end