Sha256: 0c7dc3685178dc2f605b8f45ea85de33318234e848ed9493d9ea31578765fdde

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Twumper
  module Config

    attr_accessor :headers, :consumer_key, :consumer_secret, :bearer

    def config 
     yield self
    end 

    def reset 
      self.headers = nil
      self.consumer_key = nil
      self.consumer_secret = nil
      self.bearer = nil
      self
    end

    def get_bearer
      self.bearer = build_bearer_request
    end

    def build_bearer_request
      credentials = set_credentials
      url = "https://api.twitter.com/oauth2/token"
      body = "grant_type=client_credentials"
      headers = set_headers(credentials)
      request_bearer(url, body, headers)
    end

    def set_credentials
      credentials = Base64.encode64("#{self.consumer_key}:#{self.consumer_secret}").gsub("\n", '')
    end

    def set_headers(credentials=nil)
      if self.bearer.nil? && credentials
        self.headers = { "Authorization" => "Basic #{credentials}" }
      else
        self.headers = { "Authorization" => "Bearer #{self.bearer}" }
      end
    end

    def request_bearer(url, body, headers)
      resp = connection.post(url, body: body, headers: headers)
      bearer_token = resp['access_token']
    end    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twumper-0.1.1 lib/twumper/config.rb