Module: YandexDelivery

Defined in:
lib/yandex_delivery.rb,
lib/generators/yandex_delivery/install/install_generator.rb

Defined Under Namespace

Classes: DynamicSetting, InstallGenerator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_reader(name) ⇒ Object



65
66
67
68
69
# File 'lib/yandex_delivery.rb', line 65

def add_reader(name)
  define_singleton_method(name) do |*args|
    send("#{name}_setting").value(*args)
  end
end

.add_writer(name, type) ⇒ Object



71
72
73
74
75
# File 'lib/yandex_delivery.rb', line 71

def add_writer(name, type)
  define_singleton_method("#{name}=") do |value|
    send("#{name}_setting=", DynamicSetting.build(value, type))
  end
end

.build_params(method_name, params = {}) ⇒ Object



28
29
30
31
32
33
# File 'lib/yandex_delivery.rb', line 28

def build_params method_name, params={}
  params[:client_id] = YandexDelivery.client["id"]
  params[:sender_id] = params[:sender_id] || YandexDelivery.senders.first["id"]
  params[:secret_key] = Digest::MD5.hexdigest("#{post_values(params)}#{YandexDelivery.send("#{method_name}_key")}")
  params
end

.create_method(name) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/yandex_delivery.rb', line 48

def create_method name
  define_singleton_method(name) do |params = {}|
    request name, params
  end
  define_method(name) do |params = {}|
    request name, params
  end
end

.post_values(params) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/yandex_delivery.rb', line 35

def post_values params
  return params unless params.kind_of?(Array) || params.kind_of?(Hash)
  if params.kind_of?(Array)
    params.map{|k,v|
      post_values(k)
    }.join('')
  else
    params.sort.map{|k,v|
      post_values(v)
    }.join('')
  end
end

.register(name, value, type = nil) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/yandex_delivery.rb', line 57

def register(name, value, type = nil)
  cattr_accessor "#{name}_setting".to_sym

  add_reader(name)
  add_writer(name, type)
  send "#{name}=", value
end

.request(method_name, params = {}) ⇒ Object



22
23
24
25
26
# File 'lib/yandex_delivery.rb', line 22

def request method_name, params={}
  params = build_params method_name, params
  result = RestClient.post "https://delivery.yandex.ru/api/last/#{method_name.to_s.camelize(:lower)}", params
  JSON.parse(result)
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



18
19
20
# File 'lib/yandex_delivery.rb', line 18

def setup
  yield self
end

Instance Method Details

#build_params(method_name, params = {}) ⇒ Object



9
10
11
12
13
14
# File 'lib/yandex_delivery.rb', line 9

def build_params method_name, params={}
  params[:client_id] = YandexDelivery.client["id"]
  params[:sender_id] = params[:sender_id] || YandexDelivery.senders.first["id"]
  params[:secret_key] = Digest::MD5.hexdigest("#{post_values(params)}#{YandexDelivery.send("#{method_name}_key")}")
  params
end

#request(method_name, params = {}) ⇒ Object



3
4
5
6
7
# File 'lib/yandex_delivery.rb', line 3

def request method_name, params={}
  params = build_params method_name, params
  result = RestClient.post "https://delivery.yandex.ru/api/last/#{method_name.to_s.camelize(:lower)}", params
  JSON.parse(result)
end