Class: Octopi::Api

Inherits:
Object
  • Object
show all
Includes:
Self, Singleton
Defined in:
lib/octopi/api.rb,
lib/octopi/api.rb

Overview

This is the real API class.

API requests are limited to 60 per minute.

Sets up basic methods for accessing the API.

Direct Known Subclasses

AnonymousApi, AuthApi

Constant Summary

CONTENT_TYPE =
{
  'yaml' => ['application/x-yaml', 'text/yaml', 'text/x-yaml', 'application/yaml'],
  'json' => 'application/json',
  'xml'  => 'application/xml',
  # Unexpectedly, Github returns resources such as blobs as text/html!
  # Thus, plain == text/html.
  'plain' => ['text/plain', 'text/html']
}
RETRYABLE_STATUS =
[403]
MAX_RETRIES =
10
@@api =
Octopi::AnonymousApi.instance
@@authenticated =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Self

#emails, #follow!, #keys, #unfollow!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



160
161
162
# File 'lib/octopi/api.rb', line 160

def method_missing(method, *args)
  api.send(method, *args)
end

Instance Attribute Details

#formatObject

Returns the value of attribute format



7
8
9
# File 'lib/octopi/api.rb', line 7

def format
  @format
end

#loginObject

Returns the value of attribute login



7
8
9
# File 'lib/octopi/api.rb', line 7

def 
  @login
end

#read_onlyObject

Returns the value of attribute read_only



7
8
9
# File 'lib/octopi/api.rb', line 7

def read_only
  @read_only
end

#tokenObject

Returns the value of attribute token



7
8
9
# File 'lib/octopi/api.rb', line 7

def token
  @token
end

#trace_levelObject

Returns the value of attribute trace_level



7
8
9
# File 'lib/octopi/api.rb', line 7

def trace_level
  @trace_level
end

Class Method Details

.apiObject Also known as: me

The API we're using



72
73
74
# File 'lib/octopi/api.rb', line 72

def self.api
  @@api
end

.api=(value) ⇒ Object

set the API we're using



81
82
83
# File 'lib/octopi/api.rb', line 81

def self.api=(value)
  @@api = value
end

.authenticatedObject

We use this to check if we use the auth or anonymous api



62
63
64
# File 'lib/octopi/api.rb', line 62

def self.authenticated
  @@authenticated
end

.authenticated=(value) ⇒ Object

We set this to true when the user has auth'd.



67
68
69
# File 'lib/octopi/api.rb', line 67

def self.authenticated=(value)
  @@authenticated = value
end

Instance Method Details

#find(path, result_key, resource_id, klass = nil, cache = true) ⇒ Object



99
100
101
102
# File 'lib/octopi/api.rb', line 99

def find(path, result_key, resource_id, klass=nil, cache=true)      
  result = get(path, { :id => resource_id, :cache => cache }, klass) 
  result
end

#find_all(path, result_key, query, klass = nil, cache = true) ⇒ Object



105
106
107
108
# File 'lib/octopi/api.rb', line 105

def find_all(path, result_key, query, klass=nil, cache=true)
  result = get(path, { :query => query, :id => query, :cache => cache }, klass)
  result[result_key]
end

#get(path, params = {}, klass = nil, format = :yaml) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/octopi/api.rb', line 114

def get(path, params = {}, klass=nil, format = :yaml)
  @@retries = 0
  begin
    submit(path, params, klass, format) do |path, params, format, query|
      self.class.get "/#{format}#{path}", { :format => format, :query => query }
    end
  rescue RetryableAPIError => e
    if @@retries < MAX_RETRIES
      $stderr.puts e.message
      if e.code != 403
        @@retries += 1
        sleep 6
        retry
      else
        raise APIError, "Github returned status #{e.code}, you may not have access to this resource."
      end
    else  
      raise APIError, "GitHub returned status #{e.code}, despite" +
       " repeating the request #{MAX_RETRIES} times. Giving up."
    end  
  end  
end

#get_raw(path, params, klass = nil) ⇒ Object



110
111
112
# File 'lib/octopi/api.rb', line 110

def get_raw(path, params, klass=nil)
 get(path, params, klass,  'plain')
end

#post(path, params = {}, klass = nil, format = :yaml) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/octopi/api.rb', line 137

def post(path, params = {}, klass=nil, format = :yaml)
  @@retries = 0
  begin
    trace "POST", "/#{format}#{path}", params
    submit(path, params, klass, format) do |path, params, format, query|
      resp = self.class.post "/#{format}#{path}", { :body => params, :format => format, :query => query }
      resp
    end
  rescue RetryableAPIError => e
    if @@retries < MAX_RETRIES
      $stderr.puts e.message
      @@retries += 1
      sleep 6
      retry
    else
      raise APIError, "GitHub returned status #{e.code}, despite" +
       " repeating the request #{MAX_RETRIES} times. Giving up."
    end
  end
end

#save(resource_path, data) ⇒ Object



92
93
94
95
96
# File 'lib/octopi/api.rb', line 92

def save(resource_path, data)
  traslate resource_path, data
  #still can't figure out on what format values are expected
  post("#{resource_path}", { :query => data })
end

#userObject



86
87
88
89
90
# File 'lib/octopi/api.rb', line 86

def user
  user_data = get("/user/show/#{}")
  raise "Unexpected response for user command" unless user_data and user_data['user']
  User.new(user_data['user'])
end