Module: Paid::HeadersBuilder

Defined in:
lib/paid/headers_builder.rb

Class Method Summary collapse

Class Method Details

.basic_auth_header(api_key) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/paid/headers_builder.rb', line 39

def self.basic_auth_header(api_key)
  unless api_key
    raise AuthenticationError.new('No API key provided. Set your API key using "Paid.api_key = <API-KEY>".')
  end

  base_64_key = Base64.encode64("#{api_key}:")
  {
    "Authorization" => "Basic #{base_64_key}",
  }
end

.build(headers, api_key = nil, auth_key = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/paid/headers_builder.rb', line 4

def self.build(headers, api_key=nil, auth_key=nil)
  headers ||= {}
  if auth_key
    temp = default_headers.merge(custom_auth_header(auth_key, api_key))
  else
    temp = default_headers.merge(basic_auth_header(api_key))
  end
  temp.merge(headers)
end

.custom_auth_header(key, api_key) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/paid/headers_builder.rb', line 29

def self.custom_auth_header(key, api_key)
  unless api_key
    raise AuthenticationError.new('No API key provided. Set your API key using "Paid.api_key = <API-KEY>".')
  end

  {
    key => api_key,
  }
end

.default_headersObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/paid/headers_builder.rb', line 14

def self.default_headers
  headers = {
    :user_agent => "Paid/#{Paid.api_version} RubyBindings/#{Paid::VERSION}",
    :content_type => 'application/x-www-form-urlencoded'
  }

  begin
    headers.update(:x_paid_client_user_agent => JSON.generate(user_agent))
  rescue => e
    headers.update(:x_paid_client_raw_user_agent => user_agent.inspect,
                   :error => "#{e} (#{e.class})")
  end
  headers
end

.get_unameObject



63
64
65
66
67
# File 'lib/paid/headers_builder.rb', line 63

def self.get_uname
  `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
rescue Errno::ENOMEM => ex # couldn't create subprocess
  "uname lookup failed"
end

.user_agentObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/paid/headers_builder.rb', line 50

def self.user_agent
  lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"

  {
    :bindings_version => Paid::VERSION,
    :lang => 'ruby',
    :lang_version => lang_version,
    :platform => RUBY_PLATFORM,
    :publisher => 'paid',
    :uname => get_uname
  }
end