Class: IGist::API
- Inherits:
-
Object
- Object
- IGist::API
- Defined in:
- lib/igist/api.rb
Instance Method Summary collapse
- #create_authorization(username, password) {|result| ... } ⇒ Object
- #each_my_gist(&block) ⇒ Object
- #each_starred_gist(&block) ⇒ Object
- #has_token? ⇒ Boolean
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
- #single_gist(id) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ API
Returns a new instance of API.
4 5 6 7 8 |
# File 'lib/igist/api.rb', line 4 def initialize(={}) @username = [:username] @token = [:token] @api_uri = URI("https://api.github.com") end |
Instance Method Details
#create_authorization(username, password) {|result| ... } ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/igist/api.rb', line 14 def (username, password) request = (username, password) response = send_request(request) response = send_otp(request) if response.is_a?(Net::HTTPUnauthorized) && response["X-GitHub-OTP"] raise "Can not authorize because: #{response.body}" unless response.is_a?(Net::HTTPCreated) result = JSON.parse(response.body) @token = result["token"] @username = username yield result if block_given? end |
#each_my_gist(&block) ⇒ Object
27 28 29 |
# File 'lib/igist/api.rb', line 27 def each_my_gist(&block) each_gist(gists_url, &block) end |
#each_starred_gist(&block) ⇒ Object
31 32 33 |
# File 'lib/igist/api.rb', line 31 def each_starred_gist(&block) each_gist(starred_gists_url, &block) end |
#has_token? ⇒ Boolean
10 11 12 |
# File 'lib/igist/api.rb', line 10 def has_token? !@token.nil? end |
#single_gist(id) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/igist/api.rb', line 35 def single_gist(id) request = Net::HTTP::Get.new(single_gist_url(id)) response = send_request(request) raise "Can not get single gist because: #{response.body}" unless response.is_a?(Net::HTTPOK) JSON.parse(response.body) end |