Class: NTimeLine::Article

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

Overview

Ariticle is a class for an event article.

Instance Attribute Summary collapse

Attributes inherited from Base

#timeline_key, #updated

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

bool_data, #initialize, int_data, #refresh, request, #request, request_with_key, #request_with_key, text_data, time_data, url_data

Constructor Details

This class inherits a constructor from NTimeLine::Base

Instance Attribute Details

Returns the value of attribute related_links.



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

def related_links
  @related_links
end

Class Method Details

.create(params) ⇒ Object

Create a new article.

params

See webservice.nifty.com/timeline/v1/articles/create.htm for details.

  • :timeline_key (required)

  • :timeline_id (required)

  • :title (required)

  • :description (required)

  • :start_time (required)

  • :end_time (required)

  • :grade (required)

  • :link (FIXME: URI or array of it)

  • :image (FIXME: file or string)

  • :image_type



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ntimeline/article.rb', line 56

def self.create(params)
  # check params
  unless params.include?(:timeline_id) &&
      params.include?(:title)          &&
      params.include?(:description)    &&
      params.include?(:start_time)     &&
      params.include?(:end_time)       &&
      params.include?(:grade)
    raise ArgumentError, <<-ERR
Required parameters(:title, :description, :start_time, :end_time, :grade) are missing.
    ERR
  end

  # request
  request_with_key("/articles/create", params)
end

.delete(id, timeline_key) ⇒ Object

Delete an article.

id

article id

timeline_key

timeline API key



135
136
137
138
139
# File 'lib/ntimeline/article.rb', line 135

def self.delete(id, timeline_key)
  request_with_key("/articles/delete/#{id}",
                   {:timeline_key => timeline_key},
                   Succeeded)
end

.search(params) ⇒ Object

Search articles.

params

see webservice.nifty.com/timeline/v1/articles/search.htm.

  • :timeline_key

  • :timeline_id

  • :phrase

  • :time_spec

  • :start_time

  • :end_time

  • :page

  • :hits

  • :order

  • :has_image



106
107
108
# File 'lib/ntimeline/article.rb', line 106

def self.search(params)
  request("/articles/search", params, ArticlePager)
end

.search_by_phrase(phrase, params = {}) ⇒ Object

Search articles by phrase.

phrase

query words

params

same as search



119
120
121
# File 'lib/ntimeline/article.rb', line 119

def self.search_by_phrase(phrase, params={})
  search(params.merge(:phrase => phrase))
end

.search_by_time_spec(time_spec, params = {}) ⇒ Object

Search articles by time_spec.

params

same as search



125
126
127
128
129
130
# File 'lib/ntimeline/article.rb', line 125

def self.search_by_time_spec(time_spec, params={})
  unless params.has_key?(:start_time) and params.has_key?(:end_time)
    raise ArgumentError, "There is not :start_time and/or :end_time in params."
  end
  search(params.merge(:time_spec => time_spec))
end

.search_by_timeline_id(timeline_id, params = {}) ⇒ Object

Search articles by timeline id.

timeline_id

timeline id



112
113
114
# File 'lib/ntimeline/article.rb', line 112

def self.search_by_timeline_id(timeline_id, params={})
  search(params.merge(:timeline_id => timeline_id))
end

.show(id, params = {}) ⇒ Object

Fetch an article by id.

id

article id

params

See webservice.nifty.com/timeline/v1/articles/show.htm for details.

  • :timeline_key



35
36
37
38
39
40
41
42
# File 'lib/ntimeline/article.rb', line 35

def self.show(id, params={})
  # check timeline_key
  unless params.has_key?(:timeline_key)
    raise ArgumentError, "There is not :timeline_key in params."
  end
  # send
  request("/articles/show/#{id}", params)
end

.update(id, params, target = self) ⇒ Object

Update an article.

id

article id

params

see webservice.nifty.com/timeline/v1/articles/create.htm for details

  • :title

  • :description

  • :start_time

  • :end_time

  • :grade

  • :link

  • :image

  • :image_type



84
85
86
# File 'lib/ntimeline/article.rb', line 84

def self.update(id, params, target=self)
  request_with_key("/articles/update/#{id}", params, target)
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



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

def ==(other) #:nodoc:
  @id == other.id
end

#deleteObject

Delete the article.



142
143
144
# File 'lib/ntimeline/article.rb', line 142

def delete
  self.class.delete(@id, @timeline_key)
end

#init(doc) ⇒ Object

:nodoc:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ntimeline/article.rb', line 11

def init(doc) #:nodoc:
  elt = nil
  if doc.kind_of?(REXML::Document)
    unless elt = doc.root.elements["/response/result/article"]
      raise ArgumentError, doc
    end
  else
    elt = doc
  end
  super(elt)
  @related_links = []
  elt.elements["related_links"].each_element("url") do |url|
    @related_links << url.text
  end
end

#update(params) ⇒ Object

Update the article.

params

same as update



90
91
92
# File 'lib/ntimeline/article.rb', line 90

def update(params)
  self.class.update(@id, params, self)
end