Class: Octoplex::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/octoplex/connection.rb', line 11

def initialize(options = {})
  options ||= {}

  @options = {
    :per_page => 100,
    :enable_caching => true,
    :logging => false,
    :token => nil
  }.update(options)

  @conn_options = {
    :logging => @options.delete(:logging),
    :access_token => @options.delete(:token),
    :enable_caching => @options.delete(:enable_caching)
  }

  @token = @conn_options[:access_token]
  @rate_limit = @rate_limit_remaining = 0
  setup
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



9
10
11
# File 'lib/octoplex/connection.rb', line 9

def conn
  @conn
end

#conn_optionsObject (readonly)

Returns the value of attribute conn_options.



9
10
11
# File 'lib/octoplex/connection.rb', line 9

def conn_options
  @conn_options
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/octoplex/connection.rb', line 9

def options
  @options
end

#rate_limitObject (readonly)

Returns the value of attribute rate_limit.



9
10
11
# File 'lib/octoplex/connection.rb', line 9

def rate_limit
  @rate_limit
end

#rate_limit_remainingObject (readonly)

Returns the value of attribute rate_limit_remaining.



9
10
11
# File 'lib/octoplex/connection.rb', line 9

def rate_limit_remaining
  @rate_limit_remaining
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/octoplex/connection.rb', line 9

def token
  @token
end

Instance Method Details

#authentication_paramObject



43
44
45
# File 'lib/octoplex/connection.rb', line 43

def authentication_param
  {:access_token => token}
end

#cache(path, response) ⇒ Object



63
64
65
66
67
68
# File 'lib/octoplex/connection.rb', line 63

def cache(path, response)
  if options[:enable_caching] == true
    (@cache ||= {})[path.to_s] = response
  end
  response
end

#cache_sizeObject



82
83
84
# File 'lib/octoplex/connection.rb', line 82

def cache_size
  (@cache ||= {}).keys.size
end

#cached(path) ⇒ Object



70
71
72
# File 'lib/octoplex/connection.rb', line 70

def cached(path)
  (@cache ||= {})[path.to_s]
end

#clear_cacheObject



78
79
80
# File 'lib/octoplex/connection.rb', line 78

def clear_cache
  @cache = {}
end

#delete(path) ⇒ Object



59
60
61
# File 'lib/octoplex/connection.rb', line 59

def delete(path)
  request(path, :delete)
end

#get(path) ⇒ Object



47
48
49
# File 'lib/octoplex/connection.rb', line 47

def get(path)
  request(path, :get)
end

#is_cached?(path) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/octoplex/connection.rb', line 74

def is_cached?(path)
  (@cache ||= {}).has_key?(path)
end

#post(path, body) ⇒ Object



51
52
53
# File 'lib/octoplex/connection.rb', line 51

def post(path, body)
  request(path, :post, body)
end

#put(path, body) ⇒ Object



55
56
57
# File 'lib/octoplex/connection.rb', line 55

def put(path, body)
  request(path, :put, body)
end

#setupObject



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

def setup
  @conn = Faraday.new(:url => 'https://api.github.com') do |builder|
    builder.use Faraday::Request::JSON
    builder.use Faraday::Response::Logger if conn_options[:logging].eql?(true)
    builder.use Faraday::Adapter::NetHttp
    builder.use Faraday::Response::Hashr
    builder.use Faraday::Response::RaiseOctoplexError
    builder.use Faraday::Response::MultiJson
  end
end