Class: Octopi::Api

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

Direct Known Subclasses

AnonymousApi, AuthApi

Constant Summary collapse

CONTENT_TYPE =
{
  'yaml' => 'application/x-yaml',
  'json' => 'application/json',
  'xml'  => 'application/sml'
}
RETRYABLE_STATUS =
[403]
MAX_RETRIES =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login = nil, token = nil, format = "yaml") ⇒ Api

Returns a new instance of Api.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/octopi.rb', line 70

def initialize( = nil, token = nil, format = "yaml")
  @format = format
  @read_only = true
  
  if 
    @login = 
    @token = token
    @read_only = false
    self.class.default_params :login => , :token => token
  end
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

#loginObject

Returns the value of attribute login.



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

def 
  @login
end

#read_onlyObject

Returns the value of attribute read_only.



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

def read_only
  @read_only
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

#trace_levelObject

Returns the value of attribute trace_level.



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

def trace_level
  @trace_level
end

Instance Method Details

#commits(repo, opts = {}) ⇒ Object



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

def commits(repo,opts={})
  branch = opts[:branch] || "master"
  commits = Commit.find_all(repo, branch, self)
end

#find(path, result_key, resource_id) ⇒ Object



120
121
122
# File 'lib/octopi.rb', line 120

def find(path, result_key, resource_id)
  get(path, { :id => resource_id }) 
end

#find_all(path, result_key, query) ⇒ Object



124
125
126
# File 'lib/octopi.rb', line 124

def find_all(path, result_key, query)
  get(path, { :query => query, :id => query })[result_key]
end

#get(path, params = {}, format = "yaml") ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/octopi.rb', line 132

def get(path, params = {}, format = "yaml")
  @@retries = 0
  begin
    trace "GET [#{format}]", "/#{format}#{path}", params
    submit(path, params, format) do |path, params, format|
      self.class.get "/#{format}#{path}"
    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

#get_raw(path, params) ⇒ Object



128
129
130
# File 'lib/octopi.rb', line 128

def get_raw(path, params)
 get(path, params, 'plain')
end

#open_issue(user, repo, params) ⇒ Object



98
99
100
# File 'lib/octopi.rb', line 98

def open_issue(user, repo, params)
  Issue.open(user, repo, params, self)
end

#post(path, params = {}, format = "yaml") ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/octopi.rb', line 152

def post(path, params = {}, format = "yaml")
  @@retries = 0
  begin
    trace "POST", "/#{format}#{path}", params
    submit(path, params, format) do |path, params, format|
      resp = self.class.post "/#{format}#{path}", :body => params
      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

#read_only?Boolean

Returns:

  • (Boolean)


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

def read_only?
  read_only
end

#repository(name) ⇒ Object Also known as: repo



102
103
104
105
106
# File 'lib/octopi.rb', line 102

def repository(name)
  repo = Repository.find(user, name, self)
  repo.api = self
  repo
end

#save(resource_path, data) ⇒ Object



114
115
116
117
118
# File 'lib/octopi.rb', line 114

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



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

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