Class: Me2API::Agent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Agent

options

:protocol - 'http' or 'https'. API 요청을 위한 프로토콜. 디폴트는 'http'
:host - me2API 서버의 호스트명(도메인명) 디폴트는 'me2day.net'
:appkey - me2API appkey. 발급은 http://me2day.net/me2/app/get_appkey
:user_id - 현재 이용자의 미투데이 아이디
:apikey - 사용자 인증키. 인증키를 이용한 인증방식인 경우 사용됨.
:open_timeout - 커넥션 타임아웃. 초단위로 지정함. 지정하지 않는 경우 5초
:read_timeout - 읽기 타임아웃. 초단위로 지정함. 지정하지 않은 경우 5초
:logger - 로그 파일 경로 또는 Logger 객체 지정 지정하지 않는 경우 콘솔


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/me2api/agent.rb', line 36

def initialize(options = {})
  options = {
    :protocol => 'http',
    :host => 'me2day.net',
    :appkey => nil,
    :user_id => nil,
    :apikey => nil,
    :open_timeout => 5,
    :read_timeout => 5,
    :logger => Logger.new(STDOUT)
  }.merge(options)

  @host = options[:host]
  @protocol = options[:protocol]
  @appkey = options[:appkey]
  @user_id = options[:user_id]
  @apikey = options[:apikey]
  @open_timeout = options[:open_timeout]
  @read_timeout = options[:read_timeout]
  @logger = options[:logger]
end

Instance Attribute Details

#apikeyObject

Returns the value of attribute apikey.



24
25
26
# File 'lib/me2api/agent.rb', line 24

def apikey
  @apikey
end

#appkeyObject

Returns the value of attribute appkey.



24
25
26
# File 'lib/me2api/agent.rb', line 24

def appkey
  @appkey
end

#hostObject

Returns the value of attribute host.



24
25
26
# File 'lib/me2api/agent.rb', line 24

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



24
25
26
# File 'lib/me2api/agent.rb', line 24

def logger
  @logger
end

#open_timeoutObject

Returns the value of attribute open_timeout.



24
25
26
# File 'lib/me2api/agent.rb', line 24

def open_timeout
  @open_timeout
end

#protocolObject

Returns the value of attribute protocol.



24
25
26
# File 'lib/me2api/agent.rb', line 24

def protocol
  @protocol
end

#read_timeoutObject

Returns the value of attribute read_timeout.



24
25
26
# File 'lib/me2api/agent.rb', line 24

def read_timeout
  @read_timeout
end

#user_idObject

Returns the value of attribute user_id.



24
25
26
# File 'lib/me2api/agent.rb', line 24

def user_id
  @user_id
end

Instance Method Details

#api_url(url) ⇒ Object



58
59
60
# File 'lib/me2api/agent.rb', line 58

def api_url(url)
  "#{@protocol}://#{@host}/api/#{url}.json"
end

#call(url, params = {}) ⇒ Object



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/me2api/agent.rb', line 62

def call(url, params = {})
  http = Curl::Easy.new(api_url(url))
  http.max_redirects = 2
  http.connect_timeout = @open_timeout
  http.timeout = @read_timeout
  
  http.headers["me2_application_key"] = @appkey
  http.headers["User-Agent"] = "ME2API for ruby " + VERSION
  
  if @user_id && @apikey
    nonce = generate_nonce
    cred = nonce + Digest::MD5.hexdigest(nonce + @apikey)

    http.http_auth_types = :basic
    http.username = @user_id
    http.password = cred
  end

  data = []
  params.each do |k, v| 
    if k == :files
      http.multipart_form_post = true
      v.each do |fk, fv|
        data << Curl::PostField.file(fk, fv.to_s)
      end
    else
      data << Curl::PostField.content(k, v.to_s) 
    end
  end
  
  http.http_post(data)
  
  case http.response_code
  when 200
    return JSON.parse(http.body_str)
  else
    reason = JSON.parse(http.body_str) rescue nil
    if reason
      r = Result.new(reason)
      error(r.code)
      error(r.message)
      error(r.description)
      raise ServerError.new(r)
    else
      error(http.body_str)
      raise FetchError.new("HTTP status code: #{http.response_code}")
    end
  end
end

#debug(msg) ⇒ Object



117
118
119
120
# File 'lib/me2api/agent.rb', line 117

def debug(msg)
  return unless @logger
  @logger.debug(msg)
end

#error(msg) ⇒ Object



127
128
129
130
# File 'lib/me2api/agent.rb', line 127

def error(msg)
  return unless @logger
  @logger.error(msg)
end

#generate_nonceObject

16진수 8자리 문자열 생성



113
114
115
# File 'lib/me2api/agent.rb', line 113

def generate_nonce
  rand(4_294_967_296).to_s(16).downcase.rjust(8, '0')
end

#info(msg) ⇒ Object



122
123
124
125
# File 'lib/me2api/agent.rb', line 122

def info(msg)
  return unless @logger
  @logger.info(msg)
end