Class: Leeno::Api::Base

Inherits:
Object
  • Object
show all
Includes:
Leeno
Defined in:
lib/leeno/api/base.rb

Direct Known Subclasses

Canvas, History

Constant Summary collapse

DOMAIN =
'http://leeno.jp'
@@conn =
Faraday.new(:url => DOMAIN) do |builder|
  builder.request  :url_encoded
  builder.adapter  :net_http
  builder.response :json
end

Constants included from Leeno

VERSION

Class Method Summary collapse

Class Method Details

.default_optionsObject



18
# File 'lib/leeno/api/base.rb', line 18

def default_options;  raise; end

.get(url, throws = false) ⇒ Object



58
59
60
61
# File 'lib/leeno/api/base.rb', line 58

def get url, throws=false
  data = @@conn.get(url, @@options).body
  return ( throws ? result_set!(data) : result_set(data) )
end

.model_classObject



20
21
22
# File 'lib/leeno/api/base.rb', line 20

def model_class
  eval(self.to_s.gsub(/Api/, "Model"))
end

.request_indexObject



43
44
45
46
47
# File 'lib/leeno/api/base.rb', line 43

def request_index
  results = get(url[:index])
  return Leeno::Array.new if results.nil?
  Leeno::Array.new results.map{|result| model_class.new(result)}
end

.request_index!Object



53
54
55
56
# File 'lib/leeno/api/base.rb', line 53

def request_index!
  results = get(url[:index], true)
  Leeno::Array.new results.map{|result| model_class.new(result)}
end

.request_showObject



38
39
40
41
# File 'lib/leeno/api/base.rb', line 38

def request_show
  result = get(url[:show])
  result.nil? ? nil : model_class.new(result)
end

.request_show!Object



49
50
51
# File 'lib/leeno/api/base.rb', line 49

def request_show!
  model_class.new get(url[:show], true)
end

.result_set(data) ⇒ Object



68
69
70
71
# File 'lib/leeno/api/base.rb', line 68

def result_set data
  return data['result'] if success?(data)
  return nil
end

.result_set!(data) ⇒ Object



63
64
65
66
# File 'lib/leeno/api/base.rb', line 63

def result_set! data
  return data['result'] if success?(data)
  raise Leeno::DocumentNotFound.new("#{model_class}: #{data['message']}")
end

.search_index(options, throws = false) ⇒ Object



33
34
35
36
# File 'lib/leeno/api/base.rb', line 33

def search_index options, throws=false
  set_options!(options)
  throws ? request_index! : request_index
end

.search_show(options, throws = false) ⇒ Object



28
29
30
31
# File 'lib/leeno/api/base.rb', line 28

def search_show options, throws=false
  set_options!(options)
  throws ? request_show! : request_show
end

.set_options!(options) ⇒ Object



24
25
26
# File 'lib/leeno/api/base.rb', line 24

def set_options! options
  @@options = default_options.merge(options)
end

.success?(data) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/leeno/api/base.rb', line 73

def success? data
  data['status'] == 'ok'
end

.urlObject



17
# File 'lib/leeno/api/base.rb', line 17

def url;              raise; end