Class: Yandex::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/yandex_metric.rb

Constant Summary collapse

OAUTH_HOST =
'oauth.yandex.ru'
OAUTH_PORT =
SSL_PORT
OAUTH_PATH =
'/token'
API_HOST =
'api-metrika.yandex.ru'
API_PORT =
SSL_PORT
SSL =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Metric

Returns a new instance of Metric.



72
73
74
# File 'lib/yandex_metric.rb', line 72

def initialize options = {}
  @options = default_options.merge options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



71
72
73
# File 'lib/yandex_metric.rb', line 71

def options
  @options
end

Class Method Details

.api_method(description, path, methods, param_desc = '') ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/yandex_metric.rb', line 118

def self.api_method description, path, methods, param_desc = ''
  #path sometimes starts with '/' but not always
  path = '/' + path unless path[0] == '/'
  methods.split(' ').each do |m|
    define_method method_name(m, path) do |args = {}|
      #/path/{id}/param => /path/id_value/param
      real_path = path.gsub(/\{[a-zA-Z0-9_]+\}/) {|id|
        args.delete(id[1..-2])
      } 
      generic_method m.downcase.to_sym, real_path, args
    end
  end
end

.method_name(method, path) ⇒ Object



111
112
113
114
115
116
# File 'lib/yandex_metric.rb', line 111

def self.method_name method, path
    #/path/{id}/param => path_param
    method_tail = path[1..-1].gsub(/\/{[a-zA-Z0-9_]+}/, '').gsub(/\//, '_')
    #'GET /path/{id}/param' => 'get_path_param'
    method.downcase + '_' + method_tail
end

Instance Method Details

#default_headersObject



67
68
69
# File 'lib/yandex_metric.rb', line 67

def default_headers
  {'Accept' => 'application/x-yametrika+json'}
end

#default_optionsObject



63
64
65
# File 'lib/yandex_metric.rb', line 63

def default_options
  {:timeout => TIMEOUT}
end

#generic_method(method, path, params) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/yandex_metric.rb', line 97

def generic_method method, path, params
  headers = default_headers.merge({'Authorization' => ("OAuth " + get_token)})
  args = {
    :host => API_HOST,
    :port => API_PORT,
    :ssl => SSL,
    :path => path,
    :params => params,
    :headers => headers,
    :timeout => options[:timeout]
  }
  JSON.parse(Client.send method, args)
end

#get_tokenObject



93
94
95
# File 'lib/yandex_metric.rb', line 93

def get_token
  @token ||= init_token
end

#init_tokenObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/yandex_metric.rb', line 76

def init_token
  params = {'grant_type' => 'password'}
  [:username, :password, :client_id, :client_secret].each do |key|
    options.has_key? key or raise "#{key} is not defined"
    params[key.to_s] = options[key]
  end
  args = {
    :host => OAUTH_HOST,
    :port => OAUTH_PORT,
    :ssl => SSL,
    :path => OAUTH_PATH,
    :params => params,
    :timeout => options[:timeout]
  }
  JSON.parse(Client.post args)['access_token']
end