Class: Muddyit::Base

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

Direct Known Subclasses

Collections, Generic

Constant Summary collapse

@@http_open_timeout =
120
@@http_read_timeout =
120
@@digest1 =

Set the request signing method

OpenSSL::Digest::Digest.new("sha1")
@@digest256 =

Some installation may not support sha256

OpenSSL::Digest::Digest.new("sha256") rescue nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_hash_or_file = {}) ⇒ Base

create a new muddyit object

You can either pass a hash with the following attributes:

  • :consumer_key (Required)

    the consumer key
    
  • :consumer_secret (Required)

    the consumer secret
    
  • :access_token (Required)

    the token
    
  • :access_token_secret (Required)

    the token secret
    
  • :rest_endpoint (Optional)

    the muddy.it rest service endpoint
    

or:

  • config_file (Required)

    yaml file to load configuration from
    

Config Example (yaml file)


consumer_key: AAA consumer_secret: BBB access_token: CCC access_token_secret: DDD



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
84
85
86
87
88
# File 'lib/muddyit/base.rb', line 58

def initialize(config_hash_or_file = {})

  if config_hash_or_file.is_a? Hash
    config_hash_or_file.nested_symbolize_keys!
    @username = config_hash_or_file[:username]
    @password = config_hash_or_file[:password]
    @consumer_key = config_hash_or_file[:consumer_key]
    @consumer_secret = config_hash_or_file[:consumer_secret]
    @access_token = config_hash_or_file[:access_token]
    @access_token_secret = config_hash_or_file[:access_token_secret]
    @rest_endpoint = config_hash_or_file.key?(:rest_endpoint) ? config_hash_or_file[:rest_endpoint] : Muddyit.REST_ENDPOINT
  else
    config = YAML.load_file(config_hash_or_file)
    config.nested_symbolize_keys!
    @username = config[:username]
    @password = config[:password]
    @consumer_key = config[:consumer_key]
    @consumer_secret = config[:consumer_secret]
    @access_token = config[:access_token]
    @access_token_secret = config[:access_token_secret]
    @rest_endpoint = config.key?(:rest_endpoint) ? config[:rest_endpoint] : Muddyit.REST_ENDPOINT
  end

  if !@consumer_key.nil?
    @auth_type = :oauth
    @consumer = ::OAuth::Consumer.new(@consumer_key, @consumer_secret, {:site=>@rest_endpoint})
    @accesstoken = ::OAuth::AccessToken.new(@consumer, @access_token, @access_token_secret)
  elsif !@username.nil?
    @auth_type = :basic
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



21
22
23
# File 'lib/muddyit/base.rb', line 21

def access_token
  @access_token
end

#access_token_secretObject (readonly)

Returns the value of attribute access_token_secret.



21
22
23
# File 'lib/muddyit/base.rb', line 21

def access_token_secret
  @access_token_secret
end

#auth_typeObject (readonly)

Returns the value of attribute auth_type.



21
22
23
# File 'lib/muddyit/base.rb', line 21

def auth_type
  @auth_type
end

#consumer_keyObject (readonly)

Returns the value of attribute consumer_key.



21
22
23
# File 'lib/muddyit/base.rb', line 21

def consumer_key
  @consumer_key
end

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



21
22
23
# File 'lib/muddyit/base.rb', line 21

def consumer_secret
  @consumer_secret
end

#passwordObject (readonly)

Returns the value of attribute password.



21
22
23
# File 'lib/muddyit/base.rb', line 21

def password
  @password
end

#rest_endpointObject

Returns the value of attribute rest_endpoint.



20
21
22
# File 'lib/muddyit/base.rb', line 20

def rest_endpoint
  @rest_endpoint
end

#usernameObject (readonly)

Returns the value of attribute username.



21
22
23
# File 'lib/muddyit/base.rb', line 21

def username
  @username
end

Instance Method Details

#collectionsObject

creates and/or returns the Muddyit::Collections object



131
# File 'lib/muddyit/base.rb', line 131

def collections() @collections ||= Muddyit::Collections.new(self) end

#extract(doc, options = {}) ⇒ Object

A mirror of the pages.create method, but for one off, non-stored, quick extraction



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/muddyit/base.rb', line 134

def extract(doc, options={})

  document = {}
  if doc.is_a? Hash
    unless doc[:uri] || doc[:text]
      raise
    end
    document = doc
  elsif doc.is_a? String
    if doc =~ /^http:\/\//
      document[:uri] = doc
    else
      document[:text] = doc
    end
  end

  # Ensure we get content_data as well
  options[:include_content] = true

  body = { :page => document.merge!(:options => options) }
  api_url = "/extract"
  response = self.send_request(api_url, :post, {}, body.to_json)
  return Muddyit::Collections::Collection::Pages::Page.new(self, response)
end

#send_request(api_url, http_method = :get, opts = {}, body = nil) ⇒ Object

sends a request to the muddyit REST api

Params

  • api_url (Required)

    the request url (uri.path)
    
  • http_method (Optional)

    choose between GET (default), POST, PUT, DELETE http request.
    
  • options (Optional)

    hash of query parameters, you do not need to include access_key_id, secret_access_key because these are added automatically
    


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/muddyit/base.rb', line 100

def send_request(api_url, http_method = :get, opts = {}, body = nil)

  raise 'no api_url supplied' unless api_url
  
  res = nil
  case @auth_type
  when :oauth
    res = oauth_request_over_http(api_url, http_method, opts, body)
  when :basic, nil
    res = basic_request_over_http(api_url, http_method, opts, body)
  end

  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    case res.body
    when " "
      return res
    when /^.+\((.+)\)$/
      # Strip any js callback method
      return JSON.parse($1)
    else
      return JSON.parse(res.body)
    end
  when Net::HTTPNotFound
    return res
  else
    return res.error!
  end
end