Class: Tinder::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/tinder/connection.rb

Constant Summary collapse

HOST =
'campfirenow.com'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subdomain, options = {}) ⇒ Connection

Returns a new instance of Connection.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tinder/connection.rb', line 42

def initialize(subdomain, options = {})
  @subdomain = subdomain
  @options = {:ssl => true, :ssl_options => {:verify => true}, :proxy => ENV['HTTP_PROXY']}
  @options[:ssl_options][:verify] = options.delete(:ssl_verify) unless options[:ssl_verify].nil?
  @options.merge!(options)
  @uri = URI.parse("#{@options[:ssl] ? 'https' : 'http' }://#{subdomain}.#{HOST}")
  @token = options[:token]
  @oauth_token = options[:oauth_token]

  if @oauth_token
    connection.headers["Authorization"] = "Bearer #{@oauth_token}"
    raw_connection.headers["Authorization"] = "Bearer #{@oauth_token}"
  else
    connection.basic_auth token, 'X'
    raw_connection.basic_auth token, 'X'
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/tinder/connection.rb', line 18

def options
  @options
end

#subdomainObject (readonly)

Returns the value of attribute subdomain.



18
19
20
# File 'lib/tinder/connection.rb', line 18

def subdomain
  @subdomain
end

#uriObject (readonly)

Returns the value of attribute uri.



18
19
20
# File 'lib/tinder/connection.rb', line 18

def uri
  @uri
end

Class Method Details

.connectionObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/tinder/connection.rb', line 20

def self.connection
  @connection ||= Faraday.new do |builder|
    builder.use     FaradayMiddleware::EncodeJson
    builder.use     FaradayMiddleware::Mashify
    builder.use     FaradayMiddleware::ParseJson
    builder.use     Faraday::Response::RemoveWhitespace
    builder.use     Faraday::Response::RaiseOnAuthenticationFailure
    builder.adapter Faraday.default_adapter
  end
end

.raw_connectionObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/tinder/connection.rb', line 31

def self.raw_connection
  @raw_connection ||= Faraday.new do |builder|
    builder.use     Faraday::Request::Multipart
    builder.use     FaradayMiddleware::Mashify
    builder.use     FaradayMiddleware::ParseJson
    builder.use     Faraday::Response::RemoveWhitespace
    builder.use     Faraday::Response::RaiseOnAuthenticationFailure
    builder.adapter Faraday.default_adapter
  end
end

Instance Method Details

#basic_auth_settingsObject



60
61
62
# File 'lib/tinder/connection.rb', line 60

def basic_auth_settings
  {:username => token, :password => 'X'}
end

#connectionObject



64
65
66
67
68
69
70
# File 'lib/tinder/connection.rb', line 64

def connection
  @connection ||= begin
    conn = self.class.connection.dup
    set_connection_options(conn)
    conn
  end
end

#get(url, *args) ⇒ Object



87
88
89
90
# File 'lib/tinder/connection.rb', line 87

def get(url, *args)
  response = connection.get(url, *args)
  response.body
end

#post(url, body = nil, *args) ⇒ Object



92
93
94
95
# File 'lib/tinder/connection.rb', line 92

def post(url, body = nil, *args)
  response = connection.post(url, body, *args)
  response.body
end

#put(url, body = nil, *args) ⇒ Object



101
102
103
104
# File 'lib/tinder/connection.rb', line 101

def put(url, body = nil, *args)
  response = connection.put(url, body, *args)
  response.body
end

#raw_connectionObject



72
73
74
75
76
77
78
# File 'lib/tinder/connection.rb', line 72

def raw_connection
  @raw_connection ||= begin
    conn = self.class.raw_connection.dup
    set_connection_options(conn)
    conn
  end
end

#raw_post(url, body = nil, *args) ⇒ Object



97
98
99
# File 'lib/tinder/connection.rb', line 97

def raw_post(url, body = nil, *args)
  response = raw_connection.post(url, body, *args)
end

#ssl?Boolean

Is the connection to campfire using ssl?

Returns:

  • (Boolean)


107
108
109
# File 'lib/tinder/connection.rb', line 107

def ssl?
  uri.scheme == 'https'
end

#tokenObject



80
81
82
83
84
85
# File 'lib/tinder/connection.rb', line 80

def token
  @token ||= begin
    connection.basic_auth(options[:username], options[:password])
    get('/users/me.json')['user']['api_auth_token']
  end
end