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 객체 지정 지정하지 않는 경우 콘솔


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

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.



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

def apikey
  @apikey
end

#appkeyObject

Returns the value of attribute appkey.



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

def appkey
  @appkey
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#open_timeoutObject

Returns the value of attribute open_timeout.



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

def open_timeout
  @open_timeout
end

#protocolObject

Returns the value of attribute protocol.



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

def protocol
  @protocol
end

#read_timeoutObject

Returns the value of attribute read_timeout.



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

def read_timeout
  @read_timeout
end

#user_idObject

Returns the value of attribute user_id.



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

def user_id
  @user_id
end

Instance Method Details

#api_url(url) ⇒ Object



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

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

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



61
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
# File 'lib/me2api/agent.rb', line 61

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



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

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

#error(msg) ⇒ Object



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

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

#generate_nonceObject

16진수 8자리 문자열 생성



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

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

#info(msg) ⇒ Object



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

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