Class: OctocatHerder::Connection
- Inherits:
-
Object
- Object
- OctocatHerder::Connection
- Includes:
- HTTParty
- Defined in:
- lib/octocat_herder/connection.rb
Overview
This implements some additional functionality around HTTParty to help make working with the GitHub API a little nicer.
Instance Attribute Summary collapse
-
#oauth2_token ⇒ Object
readonly
The OAuth2 token to use when doing OAuth2 authentication.
-
#password ⇒ Object
readonly
Password to use when doing basic HTTP authentication.
-
#user_name ⇒ Object
readonly
User name to use when doing basic HTTP authentication.
Instance Method Summary collapse
-
#authenticated_requests? ⇒ true, false
Are we making authenticated requests?.
-
#get(end_point, options = {}) ⇒ Object
Execute a GET request against the GitHub v3 API.
-
#initialize(options = {}) ⇒ Connection
constructor
If provided a hash of login information, the Connection will attempt to make authenticated requests.
-
#page_from_headers(headers, type) ⇒ Object
Retrieve the page number of a given ‘Link:’ header from a hash of HTTP Headers.
-
#query_string_from_params(params) ⇒ String
Convenience method to generate URL query strings.
-
#raw_get(end_point, options = {}) ⇒ Object
Small wrapper around HTTParty.get, which handles adding authentication information to the API request.
Constructor Details
#initialize(options = {}) ⇒ Connection
If provided a hash of login information, the OctocatHerder::Connection will attempt to make authenticated requests.
If no hash is provided, then unauthenticated requests will be made.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/octocat_herder/connection.rb', line 47 def initialize(={}) raise ArgumentError.new( "OctocatHerder::Connection does not accept: #{.class}" ) unless .is_a? Hash .keys.each do |k| raise ArgumentError.new("Unknown option: '#{k}'") unless [ :user_name, :password, :oauth2_token ].include? k end if .keys.include?(:user_name) or .keys.include?(:password) raise ArgumentError.new("When providing :user_name or :password, both are required") unless .keys.include?(:user_name) and .keys.include?(:password) end if .keys.include?(:oauth2_token) and .keys.include?(:user_name) raise ArgumentError.new('Cannot provide both an OAuth2 token, and a user name and password') end @user_name = [:user_name] @password = [:password] @oauth2_token = [:oauth2_token] if oauth2_token @httparty_options = { :headers => { 'Authorization' => "token #{oauth2_token}" } } elsif user_name @httparty_options = { :basic_auth => { :username => user_name, :password => password } } end end |
Instance Attribute Details
#oauth2_token ⇒ Object (readonly)
The OAuth2 token to use when doing OAuth2 authentication.
24 25 26 |
# File 'lib/octocat_herder/connection.rb', line 24 def oauth2_token @oauth2_token end |
#password ⇒ Object (readonly)
Password to use when doing basic HTTP authentication.
19 20 21 |
# File 'lib/octocat_herder/connection.rb', line 19 def password @password end |
#user_name ⇒ Object (readonly)
User name to use when doing basic HTTP authentication.
14 15 16 |
# File 'lib/octocat_herder/connection.rb', line 14 def user_name @user_name end |
Instance Method Details
#authenticated_requests? ⇒ true, false
Are we making authenticated requests?
138 139 140 141 142 143 144 |
# File 'lib/octocat_herder/connection.rb', line 138 def authenticated_requests? if (user_name and password) or oauth2_token true else false end end |
#get(end_point, options = {}) ⇒ Object
Execute a GET request against the GitHub v3 API.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/octocat_herder/connection.rb', line 94 def get(end_point, ={}) paginated = .delete(:paginated) [:params] ||= {} [:params][:per_page] = 100 if paginated and [:params][:per_page].nil? result = raw_get(end_point, ) raise "Unable to retrieve #{end_point}" unless result full_result = result.parsed_response if paginated if next_page = page_from_headers(result.headers, 'next') [:params][:page] = next_page [:paginated] = true full_result += raw_get(end_point, ) end end full_result end |
#page_from_headers(headers, type) ⇒ Object
Retrieve the page number of a given ‘Link:’ header from a hash of HTTP Headers
type
can be one of:
- ‘
next
’ -
The immediate next page of results.
- ‘
last
’ -
The last page of first.
- ‘
first
’ -
The first page of results.
- ‘
prev
’ -
The immediate previous page of results.
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/octocat_herder/connection.rb', line 162 def page_from_headers(headers, type) raise ArgumentError.new( "Unknown type: #{type}" ) unless ['next', 'last', 'first', 'prev'].include? type link = LinkHeader.parse(headers['link']).find_link(['rel', type]) return unless link CGI.parse(URI.parse(link.href).query)['page'].first end |
#query_string_from_params(params) ⇒ String
Convenience method to generate URL query strings.
183 184 185 186 187 |
# File 'lib/octocat_herder/connection.rb', line 183 def query_string_from_params(params) return '' if params.keys.empty? '?' + params.map {|k,v| "#{URI.escape("#{k}")}=#{URI.escape("#{v}")}"}.join('&') end |
#raw_get(end_point, options = {}) ⇒ Object
Small wrapper around HTTParty.get, which handles adding authentication information to the API request.
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/octocat_herder/connection.rb', line 121 def raw_get(end_point, ={}) = .dup query_params = .delete(:params) || {} query_string = query_string_from_params(query_params) = .merge() if .has_key?(:headers) and .has_key(:headers) [:headers] = [:headers].merge([:headers]) end OctocatHerder::Connection.get(end_point + query_string, ) end |