Skip to content

Commit

Permalink
JM: adding support for specifying auth keys with config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Moffitt committed Jun 12, 2018
1 parent 0726e87 commit 6d5fbeb
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions app/helpers/api_oauth_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 3,38 @@

require 'json'
require 'oauth'
require 'yaml'

class ApiOauthRequest

HEADERS = {"content-type" => "application/json"} #Suggested set? Any?

attr_accessor :keys,
:twitter_api,
:base_url, #Default: 'https://api.twitter.com/'
:uri_path #No default.
:base_url #Default: 'https://api.twitter.com/'

def initialize()

@base_url = 'https://api.twitter.com/'
@uri_path = '' #'/1.1/direct_messages' or '/1.1/account_activity'
def initialize(config=nil)

@base_url = 'https://api.twitter.com'

#'Config Variables' via the ENV{} hash.
@keys = {}

@keys['consumer_key'] = ENV['CONSUMER_KEY']
@keys['consumer_secret'] = ENV['CONSUMER_SECRET']
@keys['access_token'] = ENV['ACCESS_TOKEN']
@keys['access_token_secret'] = ENV['ACCESS_TOKEN_SECRET']
if config.nil?
#Load keys from ENV.
@keys['consumer_key'] = ENV['CONSUMER_KEY']
@keys['consumer_secret'] = ENV['CONSUMER_SECRET']
@keys['access_token'] = ENV['ACCESS_TOKEN']
@keys['access_token_secret'] = ENV['ACCESS_TOKEN_SECRET']
else
#Load from config file.
@keys = YAML::load_file(config)
end

#Adding in Premium/Standard details. URLs reference the dashboard environment name.
#@keys['environment_name'] = ENV['ENVIRONMENT_NAME']
end

end
#API client object is created with the @base_url context, then individual requests are made with specific URI paths passed in.

def get_api_access
consumer = OAuth::Consumer.new(@keys['consumer_key'], @keys['consumer_secret'], {:site => @base_url})
Expand All @@ -39,9 44,9 @@ def get_api_access

@twitter_api = OAuth::AccessToken.from_hash(consumer, token)

end
end

def make_post_request(uri_path, request)
def make_post_request(uri_path, request)
get_api_access if @twitter_api.nil? #token timeout?

response = @twitter_api.post(uri_path, request, HEADERS)
Expand Down

0 comments on commit 6d5fbeb

Please sign in to comment.