Class: Leeno::Api::Base
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
Class Method Summary collapse
- .default_options ⇒ Object
- .get(url, throws = false) ⇒ Object
- .model_class ⇒ Object
- .request_index ⇒ Object
- .request_index! ⇒ Object
- .request_show ⇒ Object
- .request_show! ⇒ Object
- .result_set(data) ⇒ Object
- .result_set!(data) ⇒ Object
- .search_index(options, throws = false) ⇒ Object
- .search_show(options, throws = false) ⇒ Object
- .set_options!(options) ⇒ Object
- .success?(data) ⇒ Boolean
- .url ⇒ Object
Class Method Details
.default_options ⇒ Object
18 |
# File 'lib/leeno/api/base.rb', line 18 def ; 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_class ⇒ Object
20 21 22 |
# File 'lib/leeno/api/base.rb', line 20 def model_class eval(self.to_s.gsub(/Api/, "Model")) end |
.request_index ⇒ Object
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_show ⇒ Object
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 , throws=false () 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 , throws=false () throws ? request_show! : request_show end |
.set_options!(options) ⇒ Object
24 25 26 |
# File 'lib/leeno/api/base.rb', line 24 def @@options = .merge() end |
.success?(data) ⇒ Boolean
73 74 75 |
# File 'lib/leeno/api/base.rb', line 73 def success? data data['status'] == 'ok' end |
.url ⇒ Object
17 |
# File 'lib/leeno/api/base.rb', line 17 def url; raise; end |