Class: NeAPI::Master

Inherits:
Object
  • Object
show all
Includes:
NeAPI
Defined in:
lib/ne_api.rb

Constant Summary collapse

PATH_PREFIX =
"/api_v1_"

Constants included from NeAPI

API_SERVER_HOST, NE_SERVER_HOST

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: "", refresh_token: "") ⇒ Master

Returns a new instance of Master.


43
44
45
46
47
48
49
50
# File 'lib/ne_api.rb', line 43

def initialize access_token: "", refresh_token: ""
  @@params = YAML.load_file(File.join(File.dirname(__FILE__),"../config/api.yaml"))
  @access_token = access_token
  @refresh_token = refresh_token
  @wait_flag = false
  @retry_num = 10
  @wait_interval = 15
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(path, args = {}) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ne_api.rb', line 94

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"
    when "checkconnect"
      fields = nil
      get_key = nil
    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_tokenObject

Returns the value of attribute access_token


40
41
42
# File 'lib/ne_api.rb', line 40

def access_token
  @access_token
end

#access_token_end_dateObject

Returns the value of attribute access_token_end_date


40
41
42
# File 'lib/ne_api.rb', line 40

def access_token_end_date
  @access_token_end_date
end

#refresh_tokenObject

Returns the value of attribute refresh_token


40
41
42
# File 'lib/ne_api.rb', line 40

def refresh_token
  @refresh_token
end

#refresh_token_end_dateObject

Returns the value of attribute refresh_token_end_date


40
41
42
# File 'lib/ne_api.rb', line 40

def refresh_token_end_date
  @refresh_token_end_date
end

#retry_numObject

Returns the value of attribute retry_num


40
41
42
# File 'lib/ne_api.rb', line 40

def retry_num
  @retry_num
end

#wait_flagObject

Returns the value of attribute wait_flag


40
41
42
# File 'lib/ne_api.rb', line 40

def wait_flag
  @wait_flag
end

#wait_intervalObject

Returns the value of attribute wait_interval


40
41
42
# File 'lib/ne_api.rb', line 40

def wait_interval
  @wait_interval
end

Instance Method Details

#force_importObject


52
53
54
# File 'lib/ne_api.rb', line 52

def force_import
  @wait_flag = true
end

#post(method: nil, model: nil, query: nil, fields: nil, get_key: nil, params: {}) ⇒ Object

Raises:


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
# File 'lib/ne_api.rb', line 61

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?
  params = params.merge({wait_flag: 1}) if @wait_flag
  
  if fields.present? && query.present?
    post_args = {access_token: @access_token, refresh_token: @refresh_token, fields: fields}.merge(query).merge(params)

  elsif fields.present?
    post_args =  {access_token: @access_token, refresh_token: @refresh_token, fields: fields}.merge(params)
  elsif query.present?
    post_args = {access_token: @access_token, refresh_token: @refresh_token}.merge(query).merge(params)
  else
    post_args = {access_token: @access_token, refresh_token: @refresh_token}.merge(params)
  end
  res = false
  
  self.retry_num.times do
    res =response(conn.post PATH_PREFIX+model.to_s+ "/" + method, post_args)
    break if res != false
    sleep(self.wait_interval)
  end
  if res == false
    raise NeAPIException,  "003001:Next Engineが大変混み合っています。APIの接続を#{self.retry_num}回、#{self.wait_interval}秒間隔でアクセスを試みましたが、失敗をしました"
    return false
  end
  
  @access_token = res["access_token"] if res["access_token"].present?
  @refresh_token = res["refresh_token"] if res["refresh_token"].present?
  @access_token_end_date  = res["access_token_end_date"] if res["access_token_end_date"].present?
  @refresh_token_end_date  = res["refresh_token_end_date"] if res["refresh_token_end_date"].present?

  get_key.present? ? res[get_key]  : res
end

#update_tokenObject


56
57
58
# File 'lib/ne_api.rb', line 56

def update_token
  
end