Class: NeAPI::Master
Constant Summary collapse
- PATH_PREFIX =
"/api_v1_"
Constants included from NeAPI
API_SERVER_HOST, NE_SERVER_HOST
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
Instance Method Summary collapse
-
#initialize(access_token: access_token, refresh_token: refresh_token) ⇒ Master
constructor
A new instance of Master.
- #method_missing(path, args = {}) ⇒ Object
- #post(method: nil, model: nil, query: nil, fields: nil, get_key: nil, params: {}) ⇒ Object
Constructor Details
#initialize(access_token: access_token, refresh_token: refresh_token) ⇒ Master
Returns a new instance of Master.
36 37 38 39 40 |
# File 'lib/ne_api.rb', line 36 def initialize access_token: access_token, refresh_token: refresh_token @@params = YAML.load_file(File.join(File.dirname(__FILE__),"../config/api.yaml")) @access_token = access_token @refresh_token = refresh_token end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(path, args = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ne_api.rb', line 59 def method_missing(path, args={}) super if @@params.nil? || path.nil? unless models = /^(.*)_.*$/.match(path.to_s) super end model = models.captures.first.to_sym method = path.to_s.split("_").last if @@params.key?(model) && @@params[model][:method].include?(method) get_key = nil query = (args[:query].present? ? args[:query] : nil) fields = (args[:fields].present? ? args[:fields] : nil) params = (args[:params].present? ? args[:params] : {}) case method when "count" get_key = "count" when "search" req= @@params[model] query ||= req[:query] fields ||= req[:fields].gsub(/^\s*/,req[:prefix]+"_").gsub(/,\s*/,","+@@params[model][:prefix]+"_") fields= fields get_key = "data" when "info" query = nil when "update", "upload", "receipted", "shipped", "labelprinted" get_key = "result" when "divide" get_key = "receive_order_id" else super end self.post method: method, model: model, query: query, fields: fields, get_key: get_key, params: params else super end end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
33 34 35 |
# File 'lib/ne_api.rb', line 33 def access_token @access_token end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
33 34 35 |
# File 'lib/ne_api.rb', line 33 def refresh_token @refresh_token end |
Instance Method Details
#post(method: nil, model: nil, query: nil, fields: nil, get_key: nil, params: {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ne_api.rb', line 42 def post method: nil , model: nil, query: nil, fields: nil, get_key: nil, params: {} raise NeAPIException, "no token!" if @access_token.nil? || @refresh_token.nil? if fields.present? && query.present? res =response(conn.post PATH_PREFIX+model.to_s+ "/" + method, {access_token: @access_token, refresh_token: @refresh_token, fields: fields}.merge(query).merge(params)) elsif fields.present? res =response(conn.post PATH_PREFIX+model.to_s+ "/" + method, {access_token: @access_token, refresh_token: @refresh_token, fields: fields}.merge(params)) elsif query.present? res =response(conn.post PATH_PREFIX+model.to_s+ "/" + method, {access_token: @access_token, refresh_token: @refresh_token}.merge(query).merge(params)) else res =response(conn.post PATH_PREFIX+model.to_s+ "/" + method, {access_token: @access_token, refresh_token: @refresh_token}.merge(params)) end @access_token = res["access_token"] if res["access_token"].present? @refresh_token = res["refresh_token"] if res["refresh_token"].present? get_key.present? ? res[get_key] : res end |