Class: NTimeLine::Base

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

Direct Known Subclasses

Article, Pager, Succeeded, TimeLine, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res) ⇒ Base

methods



45
46
47
# File 'lib/ntimeline.rb', line 45

def initialize(res) #:nodoc:
  init(res)
end

Instance Attribute Details

#timeline_keyObject

Returns the value of attribute timeline_key.



10
11
12
# File 'lib/ntimeline.rb', line 10

def timeline_key
  @timeline_key
end

#updatedObject

Returns the value of attribute updated.



10
11
12
# File 'lib/ntimeline.rb', line 10

def updated
  @updated
end

Class Method Details

.bool_data(*args) ⇒ Object



26
27
28
29
# File 'lib/ntimeline.rb', line 26

def self.bool_data(*args)
  @bool_data ? @bool_data += args : @bool_data = args
  attr_reader *args
end

.int_data(*args) ⇒ Object



21
22
23
24
# File 'lib/ntimeline.rb', line 21

def self.int_data(*args)
  @int_data ? @int_data += args : @int_data = args
  attr_reader *args
end

.request(path, params = {}, target = self) ⇒ Object

Send a request message to the server.

path

relative path from api.timeline.nifty.com/api/v1

params

parameters hash table

target

class or object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ntimeline.rb', line 89

def self.request(path, params={}, target = self)
  req = Net::HTTP::Post.new(URL.path + path)
  data = params.inject(Hash.new) do |data, (key, val)|
    val.kind_of?(Time) ? data[key.to_s] = val.iso8601 : data[key.to_s] = val
    data
  end
  req.set_form_data data
  case res = Net::HTTP.new(URL.host).request(req)
  when Net::HTTPSuccess
    doc = REXML::Document.new(res.body)
    obj = target.kind_of?(Class) ? target.new(doc) : target.refresh(doc)
    obj.request_params = params if obj.kind_of?(NTimeLine::Pager)
    obj.timeline_key = params[:timeline_key] if params.has_key?(:timeline_key)
    return obj
  when Net::HTTPBadRequest
    raise BadRequestError.new(REXML::Document.new(res.body))
  when Net::HTTPForbidden
    p res.body
    raise ForbiddenError.new(REXML::Document.new(res.body))
  else
    raise ResponseError.new(res)
  end
end

.request_with_key(path, params = {}, target = self) ⇒ Object

Same as request but need timeline_key.

path

relative path from api.timeline.nifty.com/api/v1

params

parameters hash table

target

class or object



117
118
119
120
121
122
123
124
# File 'lib/ntimeline.rb', line 117

def self.request_with_key(path, params={}, target=self)
  # check timeline_key
  unless params.has_key?(:timeline_key)
    raise ArgumentError, "There is not :timeline_key in params"
  end
  # request
  request(path, params, target)
end

.text_data(*args) ⇒ Object

setters



16
17
18
19
# File 'lib/ntimeline.rb', line 16

def self.text_data(*args)
  @text_data ? @text_data += args : @text_data = args
  attr_reader *args
end

.time_data(*args) ⇒ Object



31
32
33
34
# File 'lib/ntimeline.rb', line 31

def self.time_data(*args)
  @time_data ? @time_data += args : @time_data = args
  attr_reader *args
end

.url_data(*args) ⇒ Object



36
37
38
39
# File 'lib/ntimeline.rb', line 36

def self.url_data(*args)
  @url_data ? @url_data += args : @url_data = args
  attr_reader *args
end

Instance Method Details

#init(res) ⇒ Object

:nodoc:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ntimeline.rb', line 54

def init(res) #:nodoc:
  self.class.instance_eval{@text_data||[]}.each do |data|
    if res.elements[data.to_s]
      instance_variable_set("@"+data.to_s, res.elements[data.to_s].text)
    end
  end
  self.class.instance_eval{@int_data||[]}.each do |data|
    if res.elements[data.to_s]
      instance_variable_set("@"+data.to_s, res.elements[data.to_s].text.to_i)
    end
  end
  self.class.instance_eval{@bool_data||[]}.each do |data|
    if res.elements[data.to_s]
      instance_variable_set("@"+data.to_s,
                            (res.elements[data.to_s].text == "true"))
    end
  end
  self.class.instance_eval{@time_data||[]}.each do |data|
    if res.elements[data.to_s]
      instance_variable_set("@"+data.to_s,
                            Time.parse(res.elements[data.to_s].text))
    end
  end
  self.class.instance_eval{@url_data||[]}.each do |data|
    if res.elements[data.to_s].text
      instance_variable_set("@"+data.to_s,
                            URI.parse(res.elements[data.to_s].text))
    end
  end
end

#refresh(res) ⇒ Object

:nodoc:



49
50
51
52
# File 'lib/ntimeline.rb', line 49

def refresh(res) #:nodoc:
  init(res)
  return self
end

#request(path, params = {}, target = self.class) ⇒ Object

Same as request.



127
128
129
# File 'lib/ntimeline.rb', line 127

def request(path, params={}, target = self.class)
  self.class.request(path, params, target)
end

#request_with_key(path, params = {}, target = self.class) ⇒ Object

Same as request_with_key.



132
133
134
# File 'lib/ntimeline.rb', line 132

def request_with_key(path, params={}, target = self.class)
  self.class.request_with_key(path, params, target)
end