Class: Powncer::Authentication::Basic

Inherits:
Connection
  • Object
show all
Defined in:
lib/powncer/authentication.rb

Defined Under Namespace

Classes: Header

Instance Attribute Summary collapse

Attributes inherited from Connection

#authenticated, #url

Class Method Summary collapse

Instance Method Summary collapse

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(options={})
  super(true)
  options[:username]        ||= Basic.load_username
  options[:password]        ||= Basic.load_password
  options[:application_key] ||= Basic.load_application_key
  options[:secret_key]      ||= Basic.load_secret_key
  @options = options
  Powncer.connections << self
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/powncer/authentication.rb', line 7

def options
  @options
end

Class Method Details

.connect!(options = {}) ⇒ Object



53
54
55
# File 'lib/powncer/authentication.rb', line 53

def connect!(options={})
  self.new(options)
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, options, *args, &block)
  query_params = URI.parse(uri).query
  headers = {}
  headers["Authorization"] = Header.new(options[:username], options[:password])
  uri << "#{query_params.nil? ? "?" : "&"}"
  uri << "app_key=#{options[: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(options[:username], options[: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' => options[: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