Module: Betterific::JsonClient

Extended by:
ClientHelpers
Includes:
ClientConstants
Defined in:
lib/betterific/json_client.rb

Constant Summary

Constants included from ClientConstants

ClientConstants::BASE_URL, ClientConstants::BETTERIFS_BASE_URL, ClientConstants::COMMENTS_BASE_URL, ClientConstants::PROTO_PACKAGE_NAME, ClientConstants::SEARCH_BASE_URL, ClientConstants::TAGS_BASE_URL, ClientConstants::TMP_DIR, ClientConstants::USERS_BASE_URL

Class Method Summary collapse

Class Method Details

.betterifs(opts = {}) ⇒ Object

Get a list of betterifs.

Parameters

  • opts - If most_popular, gets the most popular betterifs of the last week.

    If most_recent, gets the most recent betterifs.

    => [id0, id1, …, idx] specifies the ids of the betterif(s) to return.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/betterific/json_client.rb', line 30

def self.betterifs(opts={})
  if [:most_popular, 'most_popular'].include?(opts) || (opts.is_a?(Hash) && [:most_popular, 'most_popular'].include?(opts[:filter]))
    return get_json("#{BETTERIFS_BASE_URL}/most-popular")
  elsif [:most_recent, 'most_recent'].include?(opts) || (opts.is_a?(Hash) && [:most_recent, 'most_recent'].include?(opts[:filter]))
    return get_json("#{BETTERIFS_BASE_URL}/most-recent")
  elsif opts[:ids]
    return get_json("#{BETTERIFS_BASE_URL}?betterifs[ids]=#{Array(opts[:ids]).map(&:to_s).join(',')}")
  else
    raise "No filter and no ids given."
  end
end

.comments(opts = {}) ⇒ Object

Get a list of comments.

Parameters

  • opts - => [id0, id1, …, idx] specifies the ids of the comment(s) to return.

  • opts - => [id0, id1, …, idx] specifies the ids of the betterif(s) for which to return comments. This is ignored if :ids is given.

  • opts - => (least_recent|most_recent) fetches either the least recent or most recent comments. This is ignored unless :betterif_ids is given.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/betterific/json_client.rb', line 54

def self.comments(opts={})
  if opts[:ids].nil? && opts[:betterif_ids].nil?
    raise "No ids or betterif_ids given."
  end
  unless opts[:order].nil?
    unless %w{least_recent most_recent}.include?(opts[:order])
      raise "Invalid order given."
    end
  end
  params = [
    opts[:ids] ? "comments[ids]=#{Array(opts[:ids]).map(&:to_s).join(',')}" : nil,
    opts[:betterif_ids] ? "betterifs[ids]=#{Array(opts[:betterif_ids]).map(&:to_s).join(',')}" : nil,
    opts[:order] ? "order=#{opts[:order]}" : nil
  ].compact
  get_json("#{COMMENTS_BASE_URL}?#{params.join('&')}")
end

.get_json(url, opts = {}, url_params = {}) ⇒ Object

:nodoc:



9
10
11
12
13
14
15
16
# File 'lib/betterific/json_client.rb', line 9

def get_json(url, opts={}, url_params={}) #:nodoc:
  url = add_page_params(url, page_params_from_opts(opts))
  uri = URI(url)
  unless url_params.empty?
    uri.query = URI.encode_www_form(url_params)
  end
  Hashie::Mash.new(JSON.parse(get_http(uri).body))
end

.search(opts = {}) ⇒ Object

Search for betterifs, tags, and users.

Parameters

  • opts - => (all|betterifs|tags|users) specifies the type of object(s) to return.

    => <query> specifies the search query.



107
108
109
110
111
112
113
114
115
116
# File 'lib/betterific/json_client.rb', line 107

def self.search(opts={})
  raise "No namespace given." if opts[:namespace].nil?
  raise "No q given." if opts[:q].nil?
  raise "q is blank." if opts[:q].blank?
  if [:betterifs, 'betterifs', :tags, 'tags', :users, 'users', :all, 'all'].include?(opts[:namespace])
    return get_json("#{SEARCH_BASE_URL}/#{opts[:namespace]}", {}, :q => opts[:q])
  else
    raise "Invalid namespace: #{opts[:namespace]}"
  end
end

.tags(opts = {}) ⇒ Object

Get a list of tags.

Parameters

  • opts - => [id0, id1, …, idx] specifies the ids of the tag(s) to return.



77
78
79
80
81
82
83
# File 'lib/betterific/json_client.rb', line 77

def self.tags(opts={})
  if opts[:ids]
    return get_json("#{TAGS_BASE_URL}?tags[ids]=#{Array(opts[:ids]).map(&:to_s).join(',')}")
  else
    raise "No ids given."
  end
end

.users(opts = {}) ⇒ Object

Get a list of users.

Parameters

  • opts - => [id0, id1, …, idx] specifies the ids of the user(s) to return.



91
92
93
94
95
96
97
# File 'lib/betterific/json_client.rb', line 91

def self.users(opts={})
  if opts[:ids]
    return get_json("#{USERS_BASE_URL}?users[ids]=#{Array(opts[:ids]).map(&:to_s).join(',')}")
  else
    raise "No ids given."
  end
end