From d3f7da9403b4991e8499b3b63c2a7c4967e0f880 Mon Sep 17 00:00:00 2001 From: Ty DeLong Date: Mon, 8 Sep 2014 15:33:56 -0500 Subject: [PATCH] Add support for OAuth token usage in request to Emma API. --- README.md | 13 +++++++++++-- lib/emma.rb | 5 +++-- lib/emma/client.rb | 9 +++++++-- lib/emma/configurable.rb | 1 + 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 84585e0..80f5292 100644 --- a/README.md +++ b/README.md @@ -35,14 +35,23 @@ Or install it yourself as: ## Instantiation ```ruby require 'emma' -em = Emma::Setup.new 'account_id', 'public_key', 'private_key', 'debug_true_or_false' +em = Emma::Setup.new 'account_id', 'public_key', 'private_key', nil, 'debug_true_or_false' ``` -You can also set environment variables and the Emma Wrapper will use them when you create an instance +or + +```ruby +require 'emma' +em = Emma::Setup.new 'account_id', nil, nil, 'oauth_token', 'debug_true_or_false' +``` + +You can also set environment variables and the Emma Wrapper will use them when you create an instance. +The client will utilize the OAuth token for authentication if present and fall back to public_key + private_key ```ruby ENV['EMMA_ACCOUNT_ID'] = 'account_id' ENV['EMMA_PUBLIC_KEY'] = 'public_key' ENV['EMMA_PRIVATE_KEY'] = 'private_key' +ENV['EMMA_OAUTH_TOKEN'] = 'oauth_token' em = Emma::Setup.new ``` diff --git a/lib/emma.rb b/lib/emma.rb index 4b9d89b..8ce3940 100644 --- a/lib/emma.rb +++ b/lib/emma.rb @@ -12,12 +12,13 @@ def available_categories class Setup include Emma::Configurable - attr_accessor :account_id, :public_key, :private_key, :debug + attr_accessor :account_id, :public_key, :private_key, :oauth_token, :debug - def initialize(account_id = nil, public_key = nil, private_key = nil, debug = false) + def initialize(account_id = nil, public_key = nil, private_key = nil, oauth_token = nil, debug = false) @account_id = account_id || ENV['EMMA_ACCOUNT_ID'] @public_key = public_key || ENV['EMMA_PUBLIC_KEY'] @private_key = private_key || ENV['EMMA_PRIVATE_KEY'] + @oauth_token = oauth_token || ENV['EMMA_OAUTH_TOKEN'] @debug = debug end diff --git a/lib/emma/client.rb b/lib/emma/client.rb index 0b0b804..20fb660 100644 --- a/lib/emma/client.rb +++ b/lib/emma/client.rb @@ -27,14 +27,19 @@ class Client base_uri "https://api.e2ma.net" - attr_accessor :account_id, :private_key, :public_key + attr_accessor :account_id, :private_key, :public_key, :oauth_token # On insantiation set instance variables into memory def initialize(options = {}) Emma::Configurable.keys.each do |key| instance_variable_set(:"@#{key}", options[key] || Emma.instance_variable_get(:"@#{key}")) end - @default_params = {:basic_auth => {:username => @public_key, :password => @private_key}} + + if @oauth_token.present? + @default_params = {:headers => { "Authorization" => "Bearer #{@oauth_token}" }} + else + @default_params = {:basic_auth => {:username => @public_key, :password => @private_key}} + end end # HTTP GET Request diff --git a/lib/emma/configurable.rb b/lib/emma/configurable.rb index 7574291..9e81b32 100644 --- a/lib/emma/configurable.rb +++ b/lib/emma/configurable.rb @@ -7,6 +7,7 @@ def keys :account_id, :public_key, :private_key, + :oauth_token, :debug ] end