class VShipping::Ahamove

Attributes

api_key[R]
config[R]

Public Class Methods

new(api_key, config_url) click to toggle source
# File lib/v_shipping/ahamove.rb, line 8
def initialize(api_key, config_url)
  @api_key = api_key
  @config = load_yaml(config_url)
end

Public Instance Methods

cancel_order(params) click to toggle source
# File lib/v_shipping/ahamove.rb, line 42
def cancel_order(params)
  get('cancel_order_endpoint', params)
end
create_order(params) click to toggle source
# File lib/v_shipping/ahamove.rb, line 22
def create_order(params)
  get('create_order_endpoint', params)
end
notify_supplier(params) click to toggle source
# File lib/v_shipping/ahamove.rb, line 58
def notify_supplier(params)
  get('notify_supplier_endpoint', params)
end
order_statuses() click to toggle source
# File lib/v_shipping/ahamove.rb, line 62
def order_statuses
  [
    mapping_status('IDLE', 'confirmed'),
    mapping_status('ASSIGNING', 'assigning'),
    mapping_status('ACCEPTED', 'accepted'),
    mapping_status('IN PROCESS', 'in_progress'),
    mapping_status('COMPLETED', 'completed'),
    mapping_status('CANCELLED', 'cancelled')
  ]
end
retrieved_city(params) click to toggle source
# File lib/v_shipping/ahamove.rb, line 46
def retrieved_city(params)
  get('city_endpoint', params)
end
retrieved_fee(params) click to toggle source
# File lib/v_shipping/ahamove.rb, line 34
def retrieved_fee(params)
  get('order_fee_endpoint', params)
end
retrieved_fees(body) click to toggle source
# File lib/v_shipping/ahamove.rb, line 38
def retrieved_fees(body)
  post('orders_fee_endpoint', body)
end
retrieved_order(params) click to toggle source
# File lib/v_shipping/ahamove.rb, line 30
def retrieved_order(params)
  get('order_endpoint', params)
end
retrieved_orders(params) click to toggle source
# File lib/v_shipping/ahamove.rb, line 26
def retrieved_orders(params)
  get('orders_endpoint', params)
end
retrieved_services(params) click to toggle source
# File lib/v_shipping/ahamove.rb, line 50
def retrieved_services(params)
  get('services_endpoint', params)
end
retrieved_token(params) click to toggle source
# File lib/v_shipping/ahamove.rb, line 13
def retrieved_token(params)
  get(
    'register_endpoint',
    params.merge({
      api_key: api_key
    })
  )
end

Private Instance Methods

base_url() click to toggle source
# File lib/v_shipping/ahamove.rb, line 81
def base_url
  config.try(:fetch, 'base_api_url').to_s
end
formmated(response) click to toggle source
# File lib/v_shipping/ahamove.rb, line 93
def formmated(response)
  [response.try(:code), JSON.parse(response.try(:body))]
end
get(endpoint, params) click to toggle source
# File lib/v_shipping/ahamove.rb, line 97
def get(endpoint, params)
  response = self.class.get(
    mapping_url(endpoint),
    { query: query_string_normalizer(params) }
  )

  formmated(response)
end
load_yaml(url) click to toggle source
# File lib/v_shipping/ahamove.rb, line 85
def load_yaml(url)
  YAML::load(
    ERB.new(
      File.read(url)
    ).result
  )
end
mapping_status(name, value) click to toggle source
# File lib/v_shipping/ahamove.rb, line 120
def mapping_status(name, value)
  {
    name: name,
    value: value
  }
end
mapping_url(name) click to toggle source
# File lib/v_shipping/ahamove.rb, line 77
def mapping_url(name)
  base_url + config.try(:fetch, name).to_s
end
post(endpoint, body) click to toggle source
# File lib/v_shipping/ahamove.rb, line 106
def post(endpoint, body)
  response = self.class.post(
    mapping_url(endpoint),
    {
      headers: {
        'Content-Type': 'application/json'
      },
      body: body.to_json
    }
  )

  formmated(response)
end
query_string_normalizer(query) click to toggle source
# File lib/v_shipping/ahamove.rb, line 127
def query_string_normalizer(query)
  query.as_json.map do |key, value|
    [value].map {|v| "#{key}=#{v}"}.join('&')
  end.join('&').gsub('=>', ':')
end