Module: YandexDelivery
- Defined in:
- lib/yandex_delivery.rb
Defined Under Namespace
Classes: DynamicSetting
Class Method Summary collapse
- .add_reader(name) ⇒ Object
- .add_writer(name, type) ⇒ Object
- .build_params(method_name, params = {}) ⇒ Object
- .create_method(name) ⇒ Object
- .register(name, value, type = nil) ⇒ Object
- .request(method_name, params = {}) ⇒ Object
- .setup {|_self| ... } ⇒ Object
Class Method Details
.add_reader(name) ⇒ Object
36 37 38 39 40 |
# File 'lib/yandex_delivery.rb', line 36 def add_reader(name) define_singleton_method(name) do |*args| send("#{name}_setting").value(*args) end end |
.add_writer(name, type) ⇒ Object
42 43 44 45 46 |
# File 'lib/yandex_delivery.rb', line 42 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
15 16 17 18 19 20 |
# File 'lib/yandex_delivery.rb', line 15 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("#{params.sort.map{|k,v| v}.join('')}#{YandexDelivery.send("#{method_name}_key")}") params end |
.create_method(name) ⇒ Object
22 23 24 25 26 |
# File 'lib/yandex_delivery.rb', line 22 def create_method name define_singleton_method(name) do |params = {}| request name, params end end |
.register(name, value, type = nil) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/yandex_delivery.rb', line 28 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
9 10 11 12 13 |
# File 'lib/yandex_delivery.rb', line 9 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
5 6 7 |
# File 'lib/yandex_delivery.rb', line 5 def setup yield self end |