Class: Kiita::API

Inherits:
Object
  • Object
show all
Extended by:
Errors::HandlerMethods
Defined in:
lib/kiita/api.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



11
12
13
# File 'lib/kiita/api.rb', line 11

def initialize(options = {})
  @token = options[:token]
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/kiita/api.rb', line 9

def token
  @token
end

Class Method Details

.authenticate(url_name, password) ⇒ Object



39
40
41
# File 'lib/kiita/api.rb', line 39

def self.authenticate(url_name, password)
  post("/auth", url_name: url_name, password: password)
end

.delete(path, options = {}) ⇒ Object



95
96
97
# File 'lib/kiita/api.rb', line 95

def self.delete(path, options = {})
  handle_response(connection.delete(qiita_url(path), options))
end

.get(path, options = {}) ⇒ Object



83
84
85
# File 'lib/kiita/api.rb', line 83

def self.get(path, options = {})
  handle_response(connection.get(qiita_url(path), options))
end

.item(uuid) ⇒ Object



47
48
49
# File 'lib/kiita/api.rb', line 47

def self.item(uuid)
  Kiita::Post.lazy_load(uuid)
end

.items(options = {}) ⇒ Object



51
52
53
# File 'lib/kiita/api.rb', line 51

def self.items(options = {})
  Kiita::Post.all(options)
end

.post(path, options = {}) ⇒ Object



87
88
89
# File 'lib/kiita/api.rb', line 87

def self.post(path, options = {})
  handle_response(connection.post(qiita_url(path), options))
end

.put(path, options = {}) ⇒ Object



91
92
93
# File 'lib/kiita/api.rb', line 91

def self.put(path, options = {})
  handle_response(connection.put(qiita_url(path), options))
end

.rate_limitObject



35
36
37
# File 'lib/kiita/api.rb', line 35

def self.rate_limit
  get("/rate_limit")
end

.search(keyword, options = {}) ⇒ Object



55
56
57
# File 'lib/kiita/api.rb', line 55

def self.search(keyword, options = {})
  Kiita::Post.search(keyword, options)
end

.tag(url_name) ⇒ Object



59
60
61
# File 'lib/kiita/api.rb', line 59

def self.tag(url_name)
  Kiita::Tag.new(url_name)
end

.tags(options = {}) ⇒ Object



63
64
65
# File 'lib/kiita/api.rb', line 63

def self.tags(options = {})
  Kiita::Tag.all(options)
end

.user(url_name) ⇒ Object



43
44
45
# File 'lib/kiita/api.rb', line 43

def self.user(url_name)
  Kiita::User.lazy_load(url_name)
end

Instance Method Details

#delete(path, options = {}) ⇒ Object



79
80
81
# File 'lib/kiita/api.rb', line 79

def delete(path, options = {})
  self.class.delete(path_with_credentials(path), options)
end

#get(path, options = {}) ⇒ Object



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

def get(path, options = {})
  self.class.get(path_with_credentials(path), options.merge!(token: token))
end

#meObject



15
16
17
# File 'lib/kiita/api.rb', line 15

def me
  Kiita::User.new(get("/user"))
end

#post(path, options = {}) ⇒ Object



71
72
73
# File 'lib/kiita/api.rb', line 71

def post(path, options = {})
  self.class.post(path_with_credentials(path), options)
end

#put(path, options = {}) ⇒ Object



75
76
77
# File 'lib/kiita/api.rb', line 75

def put(path, options = {})
  self.class.put(path_with_credentials(path), options)
end

#search(keyword, options = {}) ⇒ Object



19
20
21
# File 'lib/kiita/api.rb', line 19

def search(keyword, options = {})
  get("/search", options.merge!(q: keyword)).map{|post| Kiita::Post.new(post) }
end

#stock!(uuid) ⇒ Object



27
28
29
# File 'lib/kiita/api.rb', line 27

def stock!(uuid)
  (response = put("/items/#{uuid}/stock")).nil? ? true : response
end

#stocks(options = {}) ⇒ Object



23
24
25
# File 'lib/kiita/api.rb', line 23

def stocks(options = {})
  get("/stocks", options).map{|post| Kiita::Post.new(post) }
end

#unstock!(uuid) ⇒ Object



31
32
33
# File 'lib/kiita/api.rb', line 31

def unstock!(uuid)
  (response = delete("/items/#{uuid}/stock")).nil? ? true : response
end