Class: Powncer::Authentication::Basic
- Inherits:
-
Connection
- Object
- Connection
- Powncer::Authentication::Basic
- Defined in:
- lib/powncer/authentication.rb
Defined Under Namespace
Classes: Header
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Attributes inherited from Connection
Class Method Summary collapse
- .connect!(options = {}) ⇒ Object
- .env_key(key_name) ⇒ Object
- .open(uri, options, *args, &block) ⇒ Object
Instance Method Summary collapse
- #get(uri) ⇒ Object
-
#initialize(options = {}) ⇒ Basic
constructor
A new instance of Basic.
- #post(uri, params = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Basic
Returns a new instance of Basic.
9 10 11 12 13 14 15 16 17 |
# File 'lib/powncer/authentication.rb', line 9 def initialize(={}) super(true) [:username] ||= Basic.load_username [:password] ||= Basic.load_password [:application_key] ||= Basic.load_application_key [:secret_key] ||= Basic.load_secret_key @options = Powncer.connections << self end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/powncer/authentication.rb', line 7 def @options end |
Class Method Details
.connect!(options = {}) ⇒ Object
53 54 55 |
# File 'lib/powncer/authentication.rb', line 53 def connect!(={}) self.new() end |
.env_key(key_name) ⇒ Object
66 67 68 |
# File 'lib/powncer/authentication.rb', line 66 def env_key(key_name) ENV["POWNCER_#{key_name.to_s.upcase}"] end |
.open(uri, options, *args, &block) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/powncer/authentication.rb', line 57 def open(uri, , *args, &block) query_params = URI.parse(uri).query headers = {} headers["Authorization"] = Header.new([:username], [:password]) uri << "#{query_params.nil? ? "?" : "&"}" uri << "app_key=#{[:application_key]}" Kernel::open(uri, headers, *args, &block) end |
Instance Method Details
#get(uri) ⇒ Object
19 20 21 |
# File 'lib/powncer/authentication.rb', line 19 def get(uri) super(uri, true) end |
#post(uri, params = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/powncer/authentication.rb', line 23 def post(uri, params={}) # TODO: Refactor endpoint = "#{@url}#{uri}" url = URI.parse(endpoint) req = Net::HTTP::Post.new(url.path) req.basic_auth([:username], [:password]) # create hash to add Link, Event params to request form_params = {} form_params.merge!({'note_body' => params[:body]}) if params[:body] form_params.merge!({'url' => params[:url]}) if params[:url] form_params.merge!({'event_name' => params[:name]}) if params[:name] form_params.merge!({'event_location' => params[:location]}) if params[:location] form_params.merge!({'event_date' => params[:date]}) if params[:date] form_params.merge!({'media_file' => params[:data]}) if params[:data] #req.set_content_type('multipart/form-data') req.set_form_data({'app_key' => [:application_key], 'note_to' => "#{params[:to]}"}.merge(form_params)) response = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) } response.body end |