Class: Datarank::Client

Inherits:
Object
  • Object
show all
Includes:
Comments, Datasources, Fizzle, Live, Locations, Reach, Retailers, Search, Sentiment, Themes, Topics, Volume, Wordcloud, HTTParty
Defined in:
lib/datarank/live.rb,
lib/datarank/reach.rb,
lib/datarank/client.rb,
lib/datarank/fizzle.rb,
lib/datarank/search.rb,
lib/datarank/themes.rb,
lib/datarank/topics.rb,
lib/datarank/volume.rb,
lib/datarank/comments.rb,
lib/datarank/locations.rb,
lib/datarank/retailers.rb,
lib/datarank/sentiment.rb,
lib/datarank/wordcloud.rb,
lib/datarank/datasources.rb

Defined Under Namespace

Modules: Comments, Datasources, Fizzle, Live, Locations, Reach, Retailers, Search, Sentiment, Themes, Topics, Volume, Wordcloud Classes: Error

Constant Summary collapse

BASE_URI =
'https://api.datarank.com'

Instance Method Summary collapse

Methods included from Search

#search

Methods included from Wordcloud

#wordcloud

Methods included from Volume

#volume_daily

Methods included from Themes

#themes, #themes_correlation, #themes_trend

Methods included from Sentiment

#sentiment_daily

Methods included from Retailers

#retailers

Methods included from Reach

#reach_daily

Methods included from Locations

#location_sentiment, #locations

Methods included from Live

#tweets, #twitter_volume_daily

Methods included from Fizzle

#fizzle_match, #fizzle_search, #fizzle_validate

Methods included from Datasources

#datasources, #datasources_by_type

Methods included from Comments

#comments_search

Methods included from Topics

#all_topics, #find_topic

Constructor Details

#initialize(api_key = nil, api_version = nil, options = {}) ⇒ Client

Returns a new instance of Client.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/datarank/client.rb', line 42

def initialize(api_key=nil, api_version=nil, options={})
  @api_key = api_key
  @api_version = api_version ? api_version : "v1";

  # defaults
  options[:base_uri] ||= BASE_URI
  @base_uri = options[:base_uri]
  options[:format]   ||= :json
  options.each do |k,v|
    self.class.send k, v
  end
end

Instance Method Details

#delete(path, options = {}) ⇒ Object



69
70
71
# File 'lib/datarank/client.rb', line 69

def delete(path, options={})
  http_verb :delete, path, options
end

#get(path, options = {}) ⇒ Object

Wrappers for the main HTTP verbs



57
58
59
# File 'lib/datarank/client.rb', line 57

def get(path, options={})
  http_verb :get, path, options
end

#http_verb(verb, path, options = {}) ⇒ Object

Raises:



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
# File 'lib/datarank/client.rb', line 73

def http_verb(verb, path, options={})

  if [:get, :delete].include? verb
    request_options = {}
    path = "#{path}?#{URI.encode_www_form(options)}" if !options.empty?
  else
    request_options = {body: options.to_json}
  end

  headers = {
    'Authorization' => @api_key,
    "Content-Type" => "application/json",
    "Accept" => "application/vnd.datarank.#{@api_version}+json"
  }

  request_options[:headers] = headers

  r = self.class.send(verb, path, request_options)
  # FIXME: raise errors on actual error packages
  parsed_json = JSON.parse(r.body)
  if parsed_json.is_a? Array
    parsed_json = {objects: parsed_json}
  end

  hash = Hashie::Mash.new(parsed_json)
  raise Error.new(hash.error) if hash.error
  raise Error.new(hash.errors.join(", ")) if hash.errors
  hash
end

#post(path, options = {}) ⇒ Object



61
62
63
# File 'lib/datarank/client.rb', line 61

def post(path, options={})
  http_verb :post, path, options
end

#put(path, options = {}) ⇒ Object



65
66
67
# File 'lib/datarank/client.rb', line 65

def put(path, options={})
  http_verb :put, path, options
end